golem-client 0.0.26

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

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

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

impl GetAccountError {
    pub fn to_account_endpoint_error(&self) -> Option<crate::model::AccountEndpointError> {
        match self {
            GetAccountError::Status404 { message } => Some(crate::model::AccountEndpointError::NotFound { message: message.clone() }), 
            GetAccountError::Status400 { errors } => Some(crate::model::AccountEndpointError::ArgValidation { errors: errors.clone() }), 
            GetAccountError::Status500 { error } => Some(crate::model::AccountEndpointError::Internal { error: error.clone() }), 
            _ => None
        }
    }
}

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

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

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

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

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

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

impl GetAccountPlanError {
    pub fn to_account_endpoint_error(&self) -> Option<crate::model::AccountEndpointError> {
        match self {
            GetAccountPlanError::Status404 { message } => Some(crate::model::AccountEndpointError::NotFound { message: message.clone() }), 
            GetAccountPlanError::Status400 { errors } => Some(crate::model::AccountEndpointError::ArgValidation { errors: errors.clone() }), 
            GetAccountPlanError::Status500 { error } => Some(crate::model::AccountEndpointError::Internal { error: error.clone() }), 
            _ => None
        }
    }
}

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

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

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

impl PutAccountError {
    pub fn to_account_endpoint_error(&self) -> Option<crate::model::AccountEndpointError> {
        match self {
            PutAccountError::Status404 { message } => Some(crate::model::AccountEndpointError::NotFound { message: message.clone() }), 
            PutAccountError::Status400 { errors } => Some(crate::model::AccountEndpointError::ArgValidation { errors: errors.clone() }), 
            PutAccountError::Status500 { error } => Some(crate::model::AccountEndpointError::Internal { error: error.clone() }), 
            _ => None
        }
    }
}

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

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

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

impl PostAccountError {
    pub fn to_account_endpoint_error(&self) -> Option<crate::model::AccountEndpointError> {
        match self {
            PostAccountError::Status404 { message } => Some(crate::model::AccountEndpointError::NotFound { message: message.clone() }), 
            PostAccountError::Status400 { errors } => Some(crate::model::AccountEndpointError::ArgValidation { errors: errors.clone() }), 
            PostAccountError::Status500 { error } => Some(crate::model::AccountEndpointError::Internal { error: error.clone() }), 
            _ => None
        }
    }
}

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

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

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

impl DeleteAccountError {
    pub fn to_account_endpoint_error(&self) -> Option<crate::model::AccountEndpointError> {
        match self {
            DeleteAccountError::Status404 { message } => Some(crate::model::AccountEndpointError::NotFound { message: message.clone() }), 
            DeleteAccountError::Status400 { errors } => Some(crate::model::AccountEndpointError::ArgValidation { errors: errors.clone() }), 
            DeleteAccountError::Status500 { error } => Some(crate::model::AccountEndpointError::Internal { error: error.clone() }), 
            _ => None
        }
    }
}

#[async_trait::async_trait]
pub trait Account {
    async fn get_account(&self, account_id: &str, authorization: &str) -> Result<crate::model::Account, GetAccountError>;
    async fn get_account_plan(&self, account_id: &str, authorization: &str) -> Result<crate::model::Plan, GetAccountPlanError>;
    async fn put_account(&self, account_id: &str, field0: crate::model::AccountData, authorization: &str) -> Result<crate::model::Account, PutAccountError>;
    async fn post_account(&self, field0: crate::model::AccountData, authorization: &str) -> Result<crate::model::Account, PostAccountError>;
    async fn delete_account(&self, account_id: &str, authorization: &str) -> Result<(), DeleteAccountError>;
}

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

#[async_trait::async_trait]
impl Account for AccountLive {
    async fn get_account(&self, account_id: &str, authorization: &str) -> Result<crate::model::Account, GetAccountError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{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::Account>().await?;
                Ok(body)
            }
            404 => {
                let body = result.json::<AccountEndpointErrorNotFoundPayload>().await?;
                Err(GetAccountError::Status404 { message: body.message })
            }
            400 => {
                let body = result.json::<AccountEndpointErrorArgValidationPayload>().await?;
                Err(GetAccountError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<AccountEndpointErrorInternalPayload>().await?;
                Err(GetAccountError::Status500 { error: body.error })
            }
            _ => Err(GetAccountError::UnexpectedStatus(result.status()))
        }
    }

    async fn get_account_plan(&self, account_id: &str, authorization: &str) -> Result<crate::model::Plan, GetAccountPlanError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{account_id}/plan"));

        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::Plan>().await?;
                Ok(body)
            }
            404 => {
                let body = result.json::<AccountEndpointErrorNotFoundPayload>().await?;
                Err(GetAccountPlanError::Status404 { message: body.message })
            }
            400 => {
                let body = result.json::<AccountEndpointErrorArgValidationPayload>().await?;
                Err(GetAccountPlanError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<AccountEndpointErrorInternalPayload>().await?;
                Err(GetAccountPlanError::Status500 { error: body.error })
            }
            _ => Err(GetAccountPlanError::UnexpectedStatus(result.status()))
        }
    }

    async fn put_account(&self, account_id: &str, field0: crate::model::AccountData, authorization: &str) -> Result<crate::model::Account, PutAccountError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{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()?
            .put(url)
            .headers(headers)
            .json(&field0)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::Account>().await?;
                Ok(body)
            }
            404 => {
                let body = result.json::<AccountEndpointErrorNotFoundPayload>().await?;
                Err(PutAccountError::Status404 { message: body.message })
            }
            400 => {
                let body = result.json::<AccountEndpointErrorArgValidationPayload>().await?;
                Err(PutAccountError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<AccountEndpointErrorInternalPayload>().await?;
                Err(PutAccountError::Status500 { error: body.error })
            }
            _ => Err(PutAccountError::UnexpectedStatus(result.status()))
        }
    }

    async fn post_account(&self, field0: crate::model::AccountData, authorization: &str) -> Result<crate::model::Account, PostAccountError> {
        let mut url = self.base_url.clone();
        url.set_path("accounts");

        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::<crate::model::Account>().await?;
                Ok(body)
            }
            404 => {
                let body = result.json::<AccountEndpointErrorNotFoundPayload>().await?;
                Err(PostAccountError::Status404 { message: body.message })
            }
            400 => {
                let body = result.json::<AccountEndpointErrorArgValidationPayload>().await?;
                Err(PostAccountError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<AccountEndpointErrorInternalPayload>().await?;
                Err(PostAccountError::Status500 { error: body.error })
            }
            _ => Err(PostAccountError::UnexpectedStatus(result.status()))
        }
    }

    async fn delete_account(&self, account_id: &str, authorization: &str) -> Result<(), DeleteAccountError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{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()?
            .delete(url)
            .headers(headers)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<()>().await?;
                Ok(body)
            }
            404 => {
                let body = result.json::<AccountEndpointErrorNotFoundPayload>().await?;
                Err(DeleteAccountError::Status404 { message: body.message })
            }
            400 => {
                let body = result.json::<AccountEndpointErrorArgValidationPayload>().await?;
                Err(DeleteAccountError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<AccountEndpointErrorInternalPayload>().await?;
                Err(DeleteAccountError::Status500 { error: body.error })
            }
            _ => Err(DeleteAccountError::UnexpectedStatus(result.status()))
        }
    }
}