golem-client 0.0.26

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

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

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

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

#[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,
}

pub enum UpdateResourceLimitsError {
    RequestFailure(reqwest::Error),
    InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
    UnexpectedStatus(reqwest::StatusCode),
    Status400 {
        errors: Vec<String>,
    },
    Status403 {
        error: String,
    },
    Status500 {
        error: String,
    },
}

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

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

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

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

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

#[async_trait::async_trait]
impl Limits for LimitsLive {
    async fn get_resource_limits(&self, account_id: &str, authorization: &str) -> Result<crate::model::ResourceLimits, GetResourceLimitsError> {
        let mut url = self.base_url.clone();
        url.set_path("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 result = reqwest::Client::builder()
            .build()?
            .get(url)
            .headers(headers)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::ResourceLimits>().await?;
                Ok(body)
            }
            400 => {
                let body = result.json::<LimitsEndpointErrorArgValidationErrorPayload>().await?;
                Err(GetResourceLimitsError::Status400 { errors: body.errors })
            }
            403 => {
                let body = result.json::<LimitsEndpointErrorLimitExceededPayload>().await?;
                Err(GetResourceLimitsError::Status403 { error: body.error })
            }
            500 => {
                let body = result.json::<LimitsEndpointErrorInternalErrorPayload>().await?;
                Err(GetResourceLimitsError::Status500 { error: body.error })
            }
            _ => Err(GetResourceLimitsError::UnexpectedStatus(result.status()))
        }
    }

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

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        let result = reqwest::Client::builder()
            .build()?
            .post(url)
            .headers(headers)
            .json(&field0)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<()>().await?;
                Ok(body)
            }
            400 => {
                let body = result.json::<LimitsEndpointErrorArgValidationErrorPayload>().await?;
                Err(UpdateResourceLimitsError::Status400 { errors: body.errors })
            }
            403 => {
                let body = result.json::<LimitsEndpointErrorLimitExceededPayload>().await?;
                Err(UpdateResourceLimitsError::Status403 { error: body.error })
            }
            500 => {
                let body = result.json::<LimitsEndpointErrorInternalErrorPayload>().await?;
                Err(UpdateResourceLimitsError::Status500 { error: body.error })
            }
            _ => Err(UpdateResourceLimitsError::UnexpectedStatus(result.status()))
        }
    }
}