golem-client 0.0.40

Client for Golem Cloud's REST API
Documentation
pub enum ComponentError {
    RequestFailure(reqwest::Error),
    InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
    UnexpectedStatus(reqwest::StatusCode),
    Status504,
    Status404 {
        message: String,
    },
    Status403 {
        error: String,
    },
    Status400 {
        errors: Vec<String>,
    },
    Status500 {
        error: String,
    },
    Status401 {
        error: String,
    },
    Status409 {
        component_id: uuid::Uuid,
    },
}

impl From<reqwest::Error> for ComponentError {
    fn from(error: reqwest::Error) -> ComponentError {
        ComponentError::RequestFailure(error)
    }
}

impl From<reqwest::header::InvalidHeaderValue> for ComponentError {
    fn from(error: reqwest::header::InvalidHeaderValue) -> ComponentError {
        ComponentError::InvalidHeaderValue(error)
    }
}

impl ComponentError {
    pub fn to_component_endpoint_error(&self) -> Option<crate::model::ComponentEndpointError> {
        match self {
            ComponentError::Status500 { error } => Some(crate::model::ComponentEndpointError::InternalError { error: error.clone() }), 
            ComponentError::Status404 { message } => Some(crate::model::ComponentEndpointError::NotFound { message: message.clone() }), 
            ComponentError::Status504 => Some(crate::model::ComponentEndpointError::GatewayTimeout {}), 
            ComponentError::Status403 { error } => Some(crate::model::ComponentEndpointError::LimitExceeded { error: error.clone() }), 
            ComponentError::Status409 { component_id } => Some(crate::model::ComponentEndpointError::AlreadyExists { component_id: component_id.clone() }), 
            ComponentError::Status401 { error } => Some(crate::model::ComponentEndpointError::Unauthorized { error: error.clone() }), 
            ComponentError::Status400 { errors } => Some(crate::model::ComponentEndpointError::ArgValidationError { errors: errors.clone() }), 
            _ => None
        }
    }
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct ComponentEndpointErrorNotFoundPayload {
    pub message: String,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct ComponentEndpointErrorLimitExceededPayload {
    pub error: String,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct ComponentEndpointErrorArgValidationErrorPayload {
    pub errors: Vec<String>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct ComponentEndpointErrorInternalErrorPayload {
    pub error: String,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct ComponentEndpointErrorUnauthorizedPayload {
    pub error: String,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct ComponentEndpointErrorAlreadyExistsPayload {
    #[serde(rename = "componentId")]
    pub component_id: uuid::Uuid,
}

#[async_trait::async_trait]
pub trait Component {
    async fn get_components_of_id(&self, template_id: &str, authorization: &str) -> Result<Vec<crate::model::Component>, ComponentError>;
    async fn put_component(&self, template_id: &str, template: impl Into<reqwest::Body> + Send, authorization: &str) -> Result<crate::model::Component, ComponentError>;
    async fn get_components(&self, project_id: Option<&str>, template_name: Option<&str>, authorization: &str) -> Result<Vec<crate::model::Component>, ComponentError>;
    async fn post_component(&self, field0: crate::model::ComponentQuery, template: impl Into<reqwest::Body> + Send, authorization: &str) -> Result<crate::model::Component, ComponentError>;
    async fn download_component(&self, template_id: &str, version: &str, authorization: &str) -> Result<Box<dyn futures_core::Stream<Item = reqwest::Result<bytes::Bytes>> + Send + Sync + Unpin>, ComponentError>;
    async fn get_latest_component(&self, template_id: &str, authorization: &str) -> Result<i32, ComponentError>;
}

#[derive(Clone, Debug)]
pub struct ComponentLive {
    pub base_url: reqwest::Url,
}

#[async_trait::async_trait]
impl Component for ComponentLive {
    async fn get_components_of_id(&self, template_id: &str, authorization: &str) -> Result<Vec<crate::model::Component>, ComponentError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("v1/templates/{template_id}"));

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        let result = reqwest::Client::builder()
            .build()?
            .get(url)
            .headers(headers)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<Vec<crate::model::Component>>().await?;
                Ok(body)
            }
            504 => Err(ComponentError::Status504), 
            404 => {
                let body = result.json::<ComponentEndpointErrorNotFoundPayload>().await?;
                Err(ComponentError::Status404 { message: body.message })
            }
            403 => {
                let body = result.json::<ComponentEndpointErrorLimitExceededPayload>().await?;
                Err(ComponentError::Status403 { error: body.error })
            }
            400 => {
                let body = result.json::<ComponentEndpointErrorArgValidationErrorPayload>().await?;
                Err(ComponentError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<ComponentEndpointErrorInternalErrorPayload>().await?;
                Err(ComponentError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<ComponentEndpointErrorUnauthorizedPayload>().await?;
                Err(ComponentError::Status401 { error: body.error })
            }
            409 => {
                let body = result.json::<ComponentEndpointErrorAlreadyExistsPayload>().await?;
                Err(ComponentError::Status409 { component_id: body.component_id })
            }
            _ => Err(ComponentError::UnexpectedStatus(result.status()))
        }
    }

    async fn put_component(&self, template_id: &str, template: impl Into<reqwest::Body> + Send, authorization: &str) -> Result<crate::model::Component, ComponentError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("v1/templates/{template_id}/upload"));

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        let result = reqwest::Client::builder()
            .build()?
            .put(url)
            .headers(headers)
            .body(template)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::Component>().await?;
                Ok(body)
            }
            504 => Err(ComponentError::Status504), 
            404 => {
                let body = result.json::<ComponentEndpointErrorNotFoundPayload>().await?;
                Err(ComponentError::Status404 { message: body.message })
            }
            403 => {
                let body = result.json::<ComponentEndpointErrorLimitExceededPayload>().await?;
                Err(ComponentError::Status403 { error: body.error })
            }
            400 => {
                let body = result.json::<ComponentEndpointErrorArgValidationErrorPayload>().await?;
                Err(ComponentError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<ComponentEndpointErrorInternalErrorPayload>().await?;
                Err(ComponentError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<ComponentEndpointErrorUnauthorizedPayload>().await?;
                Err(ComponentError::Status401 { error: body.error })
            }
            409 => {
                let body = result.json::<ComponentEndpointErrorAlreadyExistsPayload>().await?;
                Err(ComponentError::Status409 { component_id: body.component_id })
            }
            _ => Err(ComponentError::UnexpectedStatus(result.status()))
        }
    }

    async fn get_components(&self, project_id: Option<&str>, template_name: Option<&str>, authorization: &str) -> Result<Vec<crate::model::Component>, ComponentError> {
        let mut url = self.base_url.clone();
        url.set_path("v1/templates");
        if let Some(value) = project_id {
            url.query_pairs_mut().append_pair("project-id", &format!("{value}"));
        }
        if let Some(value) = template_name {
            url.query_pairs_mut().append_pair("template-name", &format!("{value}"));
        }
        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        let result = reqwest::Client::builder()
            .build()?
            .get(url)
            .headers(headers)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<Vec<crate::model::Component>>().await?;
                Ok(body)
            }
            504 => Err(ComponentError::Status504), 
            404 => {
                let body = result.json::<ComponentEndpointErrorNotFoundPayload>().await?;
                Err(ComponentError::Status404 { message: body.message })
            }
            403 => {
                let body = result.json::<ComponentEndpointErrorLimitExceededPayload>().await?;
                Err(ComponentError::Status403 { error: body.error })
            }
            400 => {
                let body = result.json::<ComponentEndpointErrorArgValidationErrorPayload>().await?;
                Err(ComponentError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<ComponentEndpointErrorInternalErrorPayload>().await?;
                Err(ComponentError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<ComponentEndpointErrorUnauthorizedPayload>().await?;
                Err(ComponentError::Status401 { error: body.error })
            }
            409 => {
                let body = result.json::<ComponentEndpointErrorAlreadyExistsPayload>().await?;
                Err(ComponentError::Status409 { component_id: body.component_id })
            }
            _ => Err(ComponentError::UnexpectedStatus(result.status()))
        }
    }

    async fn post_component(&self, field0: crate::model::ComponentQuery, template: impl Into<reqwest::Body> + Send, authorization: &str) -> Result<crate::model::Component, ComponentError> {
        let mut url = self.base_url.clone();
        url.set_path("v1/templates");

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        let form = reqwest::multipart::Form::new()
            .part("field0", reqwest::multipart::Part::text(serde_json::to_string(&field0).expect("Failed to serialize value to JSON")).mime_str("application/json")?)
            .part("template", reqwest::multipart::Part::stream(template).mime_str("application/octet-stream")?);
        let result = reqwest::Client::builder()
            .build()?
            .post(url)
            .headers(headers)
            .multipart(form)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::Component>().await?;
                Ok(body)
            }
            504 => Err(ComponentError::Status504), 
            404 => {
                let body = result.json::<ComponentEndpointErrorNotFoundPayload>().await?;
                Err(ComponentError::Status404 { message: body.message })
            }
            403 => {
                let body = result.json::<ComponentEndpointErrorLimitExceededPayload>().await?;
                Err(ComponentError::Status403 { error: body.error })
            }
            400 => {
                let body = result.json::<ComponentEndpointErrorArgValidationErrorPayload>().await?;
                Err(ComponentError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<ComponentEndpointErrorInternalErrorPayload>().await?;
                Err(ComponentError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<ComponentEndpointErrorUnauthorizedPayload>().await?;
                Err(ComponentError::Status401 { error: body.error })
            }
            409 => {
                let body = result.json::<ComponentEndpointErrorAlreadyExistsPayload>().await?;
                Err(ComponentError::Status409 { component_id: body.component_id })
            }
            _ => Err(ComponentError::UnexpectedStatus(result.status()))
        }
    }

    async fn download_component(&self, template_id: &str, version: &str, authorization: &str) -> Result<Box<dyn futures_core::Stream<Item = reqwest::Result<bytes::Bytes>> + Send + Sync + Unpin>, ComponentError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("v1/templates/{template_id}/download"));
        url.query_pairs_mut().append_pair("version", &format!("{version}"));
        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        let result = reqwest::Client::builder()
            .build()?
            .get(url)
            .headers(headers)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = Box::new(result.bytes_stream());
                Ok(body)
            }
            504 => Err(ComponentError::Status504), 
            404 => {
                let body = result.json::<ComponentEndpointErrorNotFoundPayload>().await?;
                Err(ComponentError::Status404 { message: body.message })
            }
            403 => {
                let body = result.json::<ComponentEndpointErrorLimitExceededPayload>().await?;
                Err(ComponentError::Status403 { error: body.error })
            }
            400 => {
                let body = result.json::<ComponentEndpointErrorArgValidationErrorPayload>().await?;
                Err(ComponentError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<ComponentEndpointErrorInternalErrorPayload>().await?;
                Err(ComponentError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<ComponentEndpointErrorUnauthorizedPayload>().await?;
                Err(ComponentError::Status401 { error: body.error })
            }
            409 => {
                let body = result.json::<ComponentEndpointErrorAlreadyExistsPayload>().await?;
                Err(ComponentError::Status409 { component_id: body.component_id })
            }
            _ => Err(ComponentError::UnexpectedStatus(result.status()))
        }
    }

    async fn get_latest_component(&self, template_id: &str, authorization: &str) -> Result<i32, ComponentError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("v1/templates/{template_id}/latest"));

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        let result = reqwest::Client::builder()
            .build()?
            .get(url)
            .headers(headers)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<i32>().await?;
                Ok(body)
            }
            504 => Err(ComponentError::Status504), 
            404 => {
                let body = result.json::<ComponentEndpointErrorNotFoundPayload>().await?;
                Err(ComponentError::Status404 { message: body.message })
            }
            403 => {
                let body = result.json::<ComponentEndpointErrorLimitExceededPayload>().await?;
                Err(ComponentError::Status403 { error: body.error })
            }
            400 => {
                let body = result.json::<ComponentEndpointErrorArgValidationErrorPayload>().await?;
                Err(ComponentError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<ComponentEndpointErrorInternalErrorPayload>().await?;
                Err(ComponentError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<ComponentEndpointErrorUnauthorizedPayload>().await?;
                Err(ComponentError::Status401 { error: body.error })
            }
            409 => {
                let body = result.json::<ComponentEndpointErrorAlreadyExistsPayload>().await?;
                Err(ComponentError::Status409 { component_id: body.component_id })
            }
            _ => Err(ComponentError::UnexpectedStatus(result.status()))
        }
    }
}