golem-client 0.0.40

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

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

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

impl LoginError {
    pub fn to_login_endpoint_error(&self) -> Option<crate::model::LoginEndpointError> {
        match self {
            LoginError::Status400 { errors } => Some(crate::model::LoginEndpointError::ArgValidation { errors: errors.clone() }), 
            LoginError::Status403 { error } => Some(crate::model::LoginEndpointError::NotWhitelisted { error: error.clone() }), 
            LoginError::Status500 { error } => Some(crate::model::LoginEndpointError::Internal { error: error.clone() }), 
            LoginError::Status401 { error } => Some(crate::model::LoginEndpointError::External { error: error.clone() }), 
            _ => None
        }
    }
}

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

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

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

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

#[async_trait::async_trait]
pub trait Login {
    async fn oauth2(&self, provider: &str, access_token: &str) -> Result<crate::model::UnsafeToken, LoginError>;
    async fn current_token(&self, authorization: &str) -> Result<crate::model::Token, LoginError>;
    async fn start_o_auth2(&self, ) -> Result<crate::model::OAuth2Data, LoginError>;
    async fn complete_o_auth2(&self, field0: String) -> Result<crate::model::UnsafeToken, LoginError>;
}

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

#[async_trait::async_trait]
impl Login for LoginLive {
    async fn oauth2(&self, provider: &str, access_token: &str) -> Result<crate::model::UnsafeToken, LoginError> {
        let mut url = self.base_url.clone();
        url.set_path("v1/oauth2");
        url.query_pairs_mut().append_pair("provider", &format!("{provider}"));
        url.query_pairs_mut().append_pair("access_token", &format!("{access_token}"));
        let result = reqwest::Client::builder()
            .build()?
            .post(url)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::UnsafeToken>().await?;
                Ok(body)
            }
            400 => {
                let body = result.json::<LoginEndpointErrorArgValidationPayload>().await?;
                Err(LoginError::Status400 { errors: body.errors })
            }
            403 => {
                let body = result.json::<LoginEndpointErrorNotWhitelistedPayload>().await?;
                Err(LoginError::Status403 { error: body.error })
            }
            500 => {
                let body = result.json::<LoginEndpointErrorInternalPayload>().await?;
                Err(LoginError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<LoginEndpointErrorExternalPayload>().await?;
                Err(LoginError::Status401 { error: body.error })
            }
            _ => Err(LoginError::UnexpectedStatus(result.status()))
        }
    }

    async fn current_token(&self, authorization: &str) -> Result<crate::model::Token, LoginError> {
        let mut url = self.base_url.clone();
        url.set_path("v1/login/token");

        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)
            }
            400 => {
                let body = result.json::<LoginEndpointErrorArgValidationPayload>().await?;
                Err(LoginError::Status400 { errors: body.errors })
            }
            403 => {
                let body = result.json::<LoginEndpointErrorNotWhitelistedPayload>().await?;
                Err(LoginError::Status403 { error: body.error })
            }
            500 => {
                let body = result.json::<LoginEndpointErrorInternalPayload>().await?;
                Err(LoginError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<LoginEndpointErrorExternalPayload>().await?;
                Err(LoginError::Status401 { error: body.error })
            }
            _ => Err(LoginError::UnexpectedStatus(result.status()))
        }
    }

    async fn start_o_auth2(&self, ) -> Result<crate::model::OAuth2Data, LoginError> {
        let mut url = self.base_url.clone();
        url.set_path("login/oauth2/device/start");

        let result = reqwest::Client::builder()
            .build()?
            .post(url)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::OAuth2Data>().await?;
                Ok(body)
            }
            400 => {
                let body = result.json::<LoginEndpointErrorArgValidationPayload>().await?;
                Err(LoginError::Status400 { errors: body.errors })
            }
            403 => {
                let body = result.json::<LoginEndpointErrorNotWhitelistedPayload>().await?;
                Err(LoginError::Status403 { error: body.error })
            }
            500 => {
                let body = result.json::<LoginEndpointErrorInternalPayload>().await?;
                Err(LoginError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<LoginEndpointErrorExternalPayload>().await?;
                Err(LoginError::Status401 { error: body.error })
            }
            _ => Err(LoginError::UnexpectedStatus(result.status()))
        }
    }

    async fn complete_o_auth2(&self, field0: String) -> Result<crate::model::UnsafeToken, LoginError> {
        let mut url = self.base_url.clone();
        url.set_path("login/oauth2/device/complete");

        let result = reqwest::Client::builder()
            .build()?
            .post(url)
            .json(&field0)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::UnsafeToken>().await?;
                Ok(body)
            }
            400 => {
                let body = result.json::<LoginEndpointErrorArgValidationPayload>().await?;
                Err(LoginError::Status400 { errors: body.errors })
            }
            403 => {
                let body = result.json::<LoginEndpointErrorNotWhitelistedPayload>().await?;
                Err(LoginError::Status403 { error: body.error })
            }
            500 => {
                let body = result.json::<LoginEndpointErrorInternalPayload>().await?;
                Err(LoginError::Status500 { error: body.error })
            }
            401 => {
                let body = result.json::<LoginEndpointErrorExternalPayload>().await?;
                Err(LoginError::Status401 { error: body.error })
            }
            _ => Err(LoginError::UnexpectedStatus(result.status()))
        }
    }
}