golem-client 0.0.40

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

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

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

impl ProjectGrantError {
    pub fn to_project_endpoint_error(&self) -> Option<crate::model::ProjectEndpointError> {
        match self {
            ProjectGrantError::Status500 { error } => Some(crate::model::ProjectEndpointError::Internal { error: error.clone() }), 
            ProjectGrantError::Status404 { message } => Some(crate::model::ProjectEndpointError::NotFound { message: message.clone() }), 
            ProjectGrantError::Status403 { error } => Some(crate::model::ProjectEndpointError::LimitExceeded { error: error.clone() }), 
            ProjectGrantError::Status401 { message } => Some(crate::model::ProjectEndpointError::Unauthorized { message: message.clone() }), 
            ProjectGrantError::Status400 { errors } => Some(crate::model::ProjectEndpointError::ArgValidation { errors: errors.clone() }), 
            _ => None
        }
    }
}

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

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

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

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

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

#[async_trait::async_trait]
pub trait ProjectGrant {
    async fn get_project_grants(&self, project_id: &str, authorization: &str) -> Result<Vec<crate::model::ProjectGrant>, ProjectGrantError>;
    async fn get_project_grant(&self, project_id: &str, grant_id: &str, authorization: &str) -> Result<crate::model::ProjectGrant, ProjectGrantError>;
    async fn post_project_grant(&self, project_id: &str, field0: crate::model::ProjectGrantDataRequest, authorization: &str) -> Result<crate::model::ProjectGrant, ProjectGrantError>;
    async fn post_project_grant_with_actions(&self, project_id: &str, field0: crate::model::ProjectGrantDataWithProjectActions, authorization: &str) -> Result<crate::model::ProjectGrant, ProjectGrantError>;
    async fn delete_grant(&self, project_id: &str, grant_id: &str, authorization: &str) -> Result<(), ProjectGrantError>;
}

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

#[async_trait::async_trait]
impl ProjectGrant for ProjectGrantLive {
    async fn get_project_grants(&self, project_id: &str, authorization: &str) -> Result<Vec<crate::model::ProjectGrant>, ProjectGrantError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("v1/projects/{project_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::ProjectGrant>>().await?;
                Ok(body)
            }
            404 => {
                let body = result.json::<ProjectEndpointErrorNotFoundPayload>().await?;
                Err(ProjectGrantError::Status404 { message: body.message })
            }
            403 => {
                let body = result.json::<ProjectEndpointErrorLimitExceededPayload>().await?;
                Err(ProjectGrantError::Status403 { error: body.error })
            }
            400 => {
                let body = result.json::<ProjectEndpointErrorArgValidationPayload>().await?;
                Err(ProjectGrantError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<ProjectEndpointErrorInternalPayload>().await?;
                Err(ProjectGrantError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<ProjectEndpointErrorUnauthorizedPayload>().await?;
                Err(ProjectGrantError::Status401 { message: body.message })
            }
            _ => Err(ProjectGrantError::UnexpectedStatus(result.status()))
        }
    }

    async fn get_project_grant(&self, project_id: &str, grant_id: &str, authorization: &str) -> Result<crate::model::ProjectGrant, ProjectGrantError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("v1/projects/{project_id}/grants/{grant_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::ProjectGrant>().await?;
                Ok(body)
            }
            404 => {
                let body = result.json::<ProjectEndpointErrorNotFoundPayload>().await?;
                Err(ProjectGrantError::Status404 { message: body.message })
            }
            403 => {
                let body = result.json::<ProjectEndpointErrorLimitExceededPayload>().await?;
                Err(ProjectGrantError::Status403 { error: body.error })
            }
            400 => {
                let body = result.json::<ProjectEndpointErrorArgValidationPayload>().await?;
                Err(ProjectGrantError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<ProjectEndpointErrorInternalPayload>().await?;
                Err(ProjectGrantError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<ProjectEndpointErrorUnauthorizedPayload>().await?;
                Err(ProjectGrantError::Status401 { message: body.message })
            }
            _ => Err(ProjectGrantError::UnexpectedStatus(result.status()))
        }
    }

    async fn post_project_grant(&self, project_id: &str, field0: crate::model::ProjectGrantDataRequest, authorization: &str) -> Result<crate::model::ProjectGrant, ProjectGrantError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("v1/projects/{project_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()?
            .post(url)
            .headers(headers)
            .json(&field0)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::ProjectGrant>().await?;
                Ok(body)
            }
            404 => {
                let body = result.json::<ProjectEndpointErrorNotFoundPayload>().await?;
                Err(ProjectGrantError::Status404 { message: body.message })
            }
            403 => {
                let body = result.json::<ProjectEndpointErrorLimitExceededPayload>().await?;
                Err(ProjectGrantError::Status403 { error: body.error })
            }
            400 => {
                let body = result.json::<ProjectEndpointErrorArgValidationPayload>().await?;
                Err(ProjectGrantError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<ProjectEndpointErrorInternalPayload>().await?;
                Err(ProjectGrantError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<ProjectEndpointErrorUnauthorizedPayload>().await?;
                Err(ProjectGrantError::Status401 { message: body.message })
            }
            _ => Err(ProjectGrantError::UnexpectedStatus(result.status()))
        }
    }

    async fn post_project_grant_with_actions(&self, project_id: &str, field0: crate::model::ProjectGrantDataWithProjectActions, authorization: &str) -> Result<crate::model::ProjectGrant, ProjectGrantError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("v1/projects/{project_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()?
            .post(url)
            .headers(headers)
            .json(&field0)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::ProjectGrant>().await?;
                Ok(body)
            }
            404 => {
                let body = result.json::<ProjectEndpointErrorNotFoundPayload>().await?;
                Err(ProjectGrantError::Status404 { message: body.message })
            }
            403 => {
                let body = result.json::<ProjectEndpointErrorLimitExceededPayload>().await?;
                Err(ProjectGrantError::Status403 { error: body.error })
            }
            400 => {
                let body = result.json::<ProjectEndpointErrorArgValidationPayload>().await?;
                Err(ProjectGrantError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<ProjectEndpointErrorInternalPayload>().await?;
                Err(ProjectGrantError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<ProjectEndpointErrorUnauthorizedPayload>().await?;
                Err(ProjectGrantError::Status401 { message: body.message })
            }
            _ => Err(ProjectGrantError::UnexpectedStatus(result.status()))
        }
    }

    async fn delete_grant(&self, project_id: &str, grant_id: &str, authorization: &str) -> Result<(), ProjectGrantError> {
        let mut url = self.base_url.clone();
        url.set_path(&format!("v1/projects/{project_id}/grants/{grant_id}"));

        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)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = ();
                Ok(body)
            }
            404 => {
                let body = result.json::<ProjectEndpointErrorNotFoundPayload>().await?;
                Err(ProjectGrantError::Status404 { message: body.message })
            }
            403 => {
                let body = result.json::<ProjectEndpointErrorLimitExceededPayload>().await?;
                Err(ProjectGrantError::Status403 { error: body.error })
            }
            400 => {
                let body = result.json::<ProjectEndpointErrorArgValidationPayload>().await?;
                Err(ProjectGrantError::Status400 { errors: body.errors })
            }
            500 => {
                let body = result.json::<ProjectEndpointErrorInternalPayload>().await?;
                Err(ProjectGrantError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<ProjectEndpointErrorUnauthorizedPayload>().await?;
                Err(ProjectGrantError::Status401 { message: body.message })
            }
            _ => Err(ProjectGrantError::UnexpectedStatus(result.status()))
        }
    }
}