golem-client 0.0.26

Client for Golem Cloud's REST API
Documentation
pub enum GetTokensError {
    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 GetTokensError {
    fn from(error: reqwest::Error) -> GetTokensError {
        GetTokensError::RequestFailure(error)
    }
}

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

impl GetTokensError {
    pub fn to_account_endpoint_error(&self) -> Option<crate::model::AccountEndpointError> {
        match self {
            GetTokensError::Status404 { message } => Some(crate::model::AccountEndpointError::NotFound { message: message.clone() }), 
            GetTokensError::Status400 { errors } => Some(crate::model::AccountEndpointError::ArgValidation { errors: errors.clone() }), 
            GetTokensError::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 GetTokenError {
    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 GetTokenError {
    fn from(error: reqwest::Error) -> GetTokenError {
        GetTokenError::RequestFailure(error)
    }
}

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

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

pub enum PostTokenError {
    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 PostTokenError {
    fn from(error: reqwest::Error) -> PostTokenError {
        PostTokenError::RequestFailure(error)
    }
}

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

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

pub enum DeleteTokenError {
    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 DeleteTokenError {
    fn from(error: reqwest::Error) -> DeleteTokenError {
        DeleteTokenError::RequestFailure(error)
    }
}

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

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

#[async_trait::async_trait]
pub trait Token {
    async fn get_tokens(&self, account_id: &str, authorization: &str) -> Result<Vec<crate::model::Token>, GetTokensError>;
    async fn get_token(&self, account_id: &str, token_id: uuid::Uuid, authorization: &str) -> Result<crate::model::Token, GetTokenError>;
    async fn post_token(&self, account_id: &str, field0: crate::model::CreateTokenDTO, authorization: &str) -> Result<crate::model::UnsafeToken, PostTokenError>;
    async fn delete_token(&self, account_id: &str, token_id: uuid::Uuid, authorization: &str) -> Result<(), DeleteTokenError>;
}

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

#[async_trait::async_trait]
impl Token for TokenLive {
    async fn get_tokens(&self, account_id: &str, authorization: &str) -> Result<Vec<crate::model::Token>, GetTokensError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{account_id}/tokens"));

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

    async fn get_token(&self, account_id: &str, token_id: uuid::Uuid, authorization: &str) -> Result<crate::model::Token, GetTokenError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{account_id}/tokens/{token_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::Token>().await?;
                Ok(body)
            }
            404 => {
                let body = result.json::<AccountEndpointErrorNotFoundPayload>().await?;
                Err(GetTokenError::Status404 { message: body.message })
            }
            400 => {
                let body = result.json::<AccountEndpointErrorArgValidationPayload>().await?;
                Err(GetTokenError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<AccountEndpointErrorInternalPayload>().await?;
                Err(GetTokenError::Status500 { error: body.error })
            }
            _ => Err(GetTokenError::UnexpectedStatus(result.status()))
        }
    }

    async fn post_token(&self, account_id: &str, field0: crate::model::CreateTokenDTO, authorization: &str) -> Result<crate::model::UnsafeToken, PostTokenError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{account_id}/tokens"));

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

    async fn delete_token(&self, account_id: &str, token_id: uuid::Uuid, authorization: &str) -> Result<(), DeleteTokenError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{account_id}/tokens/{token_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(DeleteTokenError::Status404 { message: body.message })
            }
            400 => {
                let body = result.json::<AccountEndpointErrorArgValidationPayload>().await?;
                Err(DeleteTokenError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<AccountEndpointErrorInternalPayload>().await?;
                Err(DeleteTokenError::Status500 { error: body.error })
            }
            _ => Err(DeleteTokenError::UnexpectedStatus(result.status()))
        }
    }
}