golem-client 0.0.26

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

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

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

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

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

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

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

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

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

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

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

#[async_trait::async_trait]
pub trait Grant {
    async fn get_grants(&self, account_id: &str, authorization: &str) -> Result<Vec<crate::model::Role>, GetGrantsError>;
    async fn get_grant(&self, account_id: &str, role: &str, authorization: &str) -> Result<crate::model::Role, GetGrantError>;
    async fn put_grant(&self, account_id: &str, role: &str, authorization: &str) -> Result<crate::model::Role, PutGrantError>;
    async fn delete_grant(&self, account_id: &str, role: &str, authorization: &str) -> Result<(), DeleteGrantError>;
}

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

#[async_trait::async_trait]
impl Grant for GrantLive {
    async fn get_grants(&self, account_id: &str, authorization: &str) -> Result<Vec<crate::model::Role>, GetGrantsError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{account_id}/grants"));

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

    async fn get_grant(&self, account_id: &str, role: &str, authorization: &str) -> Result<crate::model::Role, GetGrantError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{account_id}/grants/{role}"));

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

    async fn put_grant(&self, account_id: &str, role: &str, authorization: &str) -> Result<crate::model::Role, PutGrantError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{account_id}/grants/{role}"));

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

    async fn delete_grant(&self, account_id: &str, role: &str, authorization: &str) -> Result<(), DeleteGrantError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("accounts/{account_id}/grants/{role}"));

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