squarecloud 0.2.0

Async Rust client for the SquareCloud API
Documentation
use super::Endpoint;
use reqwest::Method;
use serde_json::json;

#[cfg(feature = "test-utils")]
inventory::submit! {
    crate::EndpointSpec {
    method: "get",
    path: "/apps/{app_id}/files",
    domain: "FileErrorCode",
    known_code: crate::errors::code_is_known::<crate::errors::FileErrorCode>,
    }
}

#[cfg(feature = "test-utils")]
inventory::submit! {
    crate::EndpointSpec {
    method: "get",
    path: "/apps/{app_id}/files/content",
    domain: "FileErrorCode",
    known_code: crate::errors::code_is_known::<crate::errors::FileErrorCode>,
    }
}

#[cfg(feature = "test-utils")]
inventory::submit! {
    crate::EndpointSpec {
    method: "put",
    path: "/apps/{app_id}/files",
    domain: "FileErrorCode",
    known_code: crate::errors::code_is_known::<crate::errors::FileErrorCode>,
    }
}

#[cfg(feature = "test-utils")]
inventory::submit! {
    crate::EndpointSpec {
    method: "patch",
    path: "/apps/{app_id}/files",
    domain: "FileErrorCode",
    known_code: crate::errors::code_is_known::<crate::errors::FileErrorCode>,
    }
}

#[cfg(feature = "test-utils")]
inventory::submit! {
    crate::EndpointSpec {
    method: "delete",
    path: "/apps/{app_id}/files",
    domain: "FileErrorCode",
    known_code: crate::errors::code_is_known::<crate::errors::FileErrorCode>,
    }
}

impl Endpoint {
    pub(crate) fn read_app_file(app_id: &str, path: &str) -> Endpoint {
        Self::builder("/apps/{app_id}/files/content", Method::GET)
            .param("app_id", app_id)
            .query("path", path)
            .build()
    }

    pub(crate) fn list_app_files(app_id: &str, path: &str) -> Endpoint {
        Self::builder("/apps/{app_id}/files", Method::GET)
            .param("app_id", app_id)
            .query("path", path)
            .build()
    }

    pub(crate) fn put_app_file(
        app_id: &str,
        path: &str,
        content: &str,
    ) -> Endpoint {
        let json_body = json!({
            "path": path,
            "content": content,
        });
        Self::builder("/apps/{app_id}/files", Method::PUT)
            .param("app_id", app_id)
            .json(serde_json::to_value(json_body).unwrap())
            .build()
    }

    pub(crate) fn move_app_file(
        app_id: &str,
        source_path: &str,
        destination_path: &str,
    ) -> Endpoint {
        let json_body = json!({
            "path": source_path,
            "to": destination_path,
        });
        Self::builder("/apps/{app_id}/files", Method::PATCH)
            .param("app_id", app_id)
            .json(serde_json::to_value(json_body).unwrap())
            .build()
    }

    pub(crate) fn delete_app_file(app_id: &str, path: &str) -> Endpoint {
        Self::builder("/apps/{app_id}/files", Method::DELETE)
            .param("app_id", app_id)
            .json(json!({ "path": path }))
            .build()
    }
}