golem-client 0.0.47

Client for Golem Cloud's REST API
Documentation
#[derive(Debug)]
pub enum LimitsError {
    RequestFailure(reqwest::Error),
    InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
    UnexpectedStatus(reqwest::StatusCode),
    Status401 {
        error: String,
    },
    Status400 {
        errors: Vec<String>,
    },
    Status403 {
        error: String,
    },
    Status500 {
        error: String,
    },
}

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

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

impl LimitsError {
    pub fn to_limits_endpoint_error(&self) -> Option<crate::model::LimitsEndpointError> {
        match self {
            LimitsError::Status401 { error } => Some(crate::model::LimitsEndpointError::Unauthorized { error: error.clone() }), 
            LimitsError::Status400 { errors } => Some(crate::model::LimitsEndpointError::ArgValidationError { errors: errors.clone() }), 
            LimitsError::Status403 { error } => Some(crate::model::LimitsEndpointError::LimitExceeded { error: error.clone() }), 
            LimitsError::Status500 { error } => Some(crate::model::LimitsEndpointError::InternalError { error: error.clone() }), 
            _ => None
        }
    }
}

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

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

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

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

#[async_trait::async_trait]
pub trait Limits {
    async fn get_resource_limits(&self, account_id: &str, authorization: &str) -> Result<crate::model::ResourceLimits, LimitsError>;
    async fn update_resource_limits(&self, field0: crate::model::BatchUpdateResourceLimits, authorization: &str) -> Result<(), LimitsError>;
}

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

#[async_trait::async_trait]
impl Limits for LimitsLive {
    async fn get_resource_limits(&self, account_id: &str, authorization: &str) -> Result<crate::model::ResourceLimits, LimitsError> {
        let mut url = self.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("v1")
            .push("resource-limits");
        url.query_pairs_mut().append_pair("account-id", &format!("{account_id}"));
        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        {
            let headers_vec: Vec<(&str, String)> = headers.iter().map(|(k, v)| crate::hide_authorization(k, v)).collect();
            tracing::info!(method="get", url=url.to_string(), headers=?headers_vec, body="<no_body>", "get_resource_limits");
        }
        let mut builder = reqwest::Client::builder();
        if self.allow_insecure {
            builder = builder.danger_accept_invalid_certs(true);
        }
        let client = builder.build()?;
        let result = client
            .get(url)
            .headers(headers)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::ResourceLimits>().await?;
                Ok(body)
            }
            401 => {
                let body = result.json::<LimitsEndpointErrorUnauthorizedPayload>().await?;
                Err(LimitsError::Status401 { error: body.error })
            }
            400 => {
                let body = result.json::<LimitsEndpointErrorArgValidationErrorPayload>().await?;
                Err(LimitsError::Status400 { errors: body.errors })
            }
            403 => {
                let body = result.json::<LimitsEndpointErrorLimitExceededPayload>().await?;
                Err(LimitsError::Status403 { error: body.error })
            }
            500 => {
                let body = result.json::<LimitsEndpointErrorInternalErrorPayload>().await?;
                Err(LimitsError::Status500 { error: body.error })
            }
            _ => Err(LimitsError::UnexpectedStatus(result.status()))
        }
    }

    async fn update_resource_limits(&self, field0: crate::model::BatchUpdateResourceLimits, authorization: &str) -> Result<(), LimitsError> {
        let mut url = self.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("v1")
            .push("resource-limits");

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        {
            let headers_vec: Vec<(&str, String)> = headers.iter().map(|(k, v)| crate::hide_authorization(k, v)).collect();
            tracing::info!(method="post", url=url.to_string(), headers=?headers_vec, body=serde_json::to_string(&field0).unwrap(), "update_resource_limits");
        }
        let mut builder = reqwest::Client::builder();
        if self.allow_insecure {
            builder = builder.danger_accept_invalid_certs(true);
        }
        let client = builder.build()?;
        let result = client
            .post(url)
            .headers(headers)
            .json(&field0)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = ();
                Ok(body)
            }
            401 => {
                let body = result.json::<LimitsEndpointErrorUnauthorizedPayload>().await?;
                Err(LimitsError::Status401 { error: body.error })
            }
            400 => {
                let body = result.json::<LimitsEndpointErrorArgValidationErrorPayload>().await?;
                Err(LimitsError::Status400 { errors: body.errors })
            }
            403 => {
                let body = result.json::<LimitsEndpointErrorLimitExceededPayload>().await?;
                Err(LimitsError::Status403 { error: body.error })
            }
            500 => {
                let body = result.json::<LimitsEndpointErrorInternalErrorPayload>().await?;
                Err(LimitsError::Status500 { error: body.error })
            }
            _ => Err(LimitsError::UnexpectedStatus(result.status()))
        }
    }
}