golem-client 0.0.47

Client for Golem Cloud's REST API
Documentation
#[derive(Debug)]
pub enum WhitelistError {
    RequestFailure(reqwest::Error),
    InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
    UnexpectedStatus(reqwest::StatusCode),
    Status401 {
        message: String,
    },
    Status500 {
        error: String,
    },
}

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

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

impl WhitelistError {
    pub fn to_whitelist_endpoint_error(&self) -> Option<crate::model::WhitelistEndpointError> {
        match self {
            WhitelistError::Status401 { message } => Some(crate::model::WhitelistEndpointError::Unauthorized { message: message.clone() }), 
            WhitelistError::Status500 { error } => Some(crate::model::WhitelistEndpointError::Internal { error: error.clone() }), 
            _ => None
        }
    }
}

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

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

#[async_trait::async_trait]
pub trait Whitelist {
    async fn is_enabled(&self, authorization: &str) -> Result<bool, WhitelistError>;
    async fn set_enabled(&self, field0: bool, authorization: &str) -> Result<(), WhitelistError>;
    async fn get_whitelist(&self, skip: i32, limit: i32, authorization: &str) -> Result<Vec<crate::model::WhitelistEmail>, WhitelistError>;
    async fn count_whitelist(&self, authorization: &str) -> Result<i64, WhitelistError>;
    async fn add_to_whitelist(&self, field0: Vec<String>, authorization: &str) -> Result<(), WhitelistError>;
    async fn remove_from_whitelist(&self, field0: Vec<String>, authorization: &str) -> Result<(), WhitelistError>;
}

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

#[async_trait::async_trait]
impl Whitelist for WhitelistLive {
    async fn is_enabled(&self, authorization: &str) -> Result<bool, WhitelistError> {
        let mut url = self.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("v1")
            .push("whitelist")
            .push("enabled");

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        {
            let headers_vec: Vec<(&str, String)> = headers.iter().map(|(k, v)| crate::hide_authorization(k, v)).collect();
            tracing::info!(method="get", url=url.to_string(), headers=?headers_vec, body="<no_body>", "is_enabled");
        }
        let mut builder = reqwest::Client::builder();
        if self.allow_insecure {
            builder = builder.danger_accept_invalid_certs(true);
        }
        let client = builder.build()?;
        let result = client
            .get(url)
            .headers(headers)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<bool>().await?;
                Ok(body)
            }
            401 => {
                let body = result.json::<WhitelistEndpointErrorUnauthorizedPayload>().await?;
                Err(WhitelistError::Status401 { message: body.message })
            }
            500 => {
                let body = result.json::<WhitelistEndpointErrorInternalPayload>().await?;
                Err(WhitelistError::Status500 { error: body.error })
            }
            _ => Err(WhitelistError::UnexpectedStatus(result.status()))
        }
    }

    async fn set_enabled(&self, field0: bool, authorization: &str) -> Result<(), WhitelistError> {
        let mut url = self.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("v1")
            .push("whitelist")
            .push("enabled");

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        {
            let headers_vec: Vec<(&str, String)> = headers.iter().map(|(k, v)| crate::hide_authorization(k, v)).collect();
            tracing::info!(method="put", url=url.to_string(), headers=?headers_vec, body=serde_json::to_string(&field0).unwrap(), "set_enabled");
        }
        let mut builder = reqwest::Client::builder();
        if self.allow_insecure {
            builder = builder.danger_accept_invalid_certs(true);
        }
        let client = builder.build()?;
        let result = client
            .put(url)
            .headers(headers)
            .json(&field0)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = ();
                Ok(body)
            }
            401 => {
                let body = result.json::<WhitelistEndpointErrorUnauthorizedPayload>().await?;
                Err(WhitelistError::Status401 { message: body.message })
            }
            500 => {
                let body = result.json::<WhitelistEndpointErrorInternalPayload>().await?;
                Err(WhitelistError::Status500 { error: body.error })
            }
            _ => Err(WhitelistError::UnexpectedStatus(result.status()))
        }
    }

    async fn get_whitelist(&self, skip: i32, limit: i32, authorization: &str) -> Result<Vec<crate::model::WhitelistEmail>, WhitelistError> {
        let mut url = self.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("v1")
            .push("whitelist");
        url.query_pairs_mut().append_pair("skip", &format!("{skip}"));
        url.query_pairs_mut().append_pair("limit", &format!("{limit}"));
        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        {
            let headers_vec: Vec<(&str, String)> = headers.iter().map(|(k, v)| crate::hide_authorization(k, v)).collect();
            tracing::info!(method="get", url=url.to_string(), headers=?headers_vec, body="<no_body>", "get_whitelist");
        }
        let mut builder = reqwest::Client::builder();
        if self.allow_insecure {
            builder = builder.danger_accept_invalid_certs(true);
        }
        let client = builder.build()?;
        let result = client
            .get(url)
            .headers(headers)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<Vec<crate::model::WhitelistEmail>>().await?;
                Ok(body)
            }
            401 => {
                let body = result.json::<WhitelistEndpointErrorUnauthorizedPayload>().await?;
                Err(WhitelistError::Status401 { message: body.message })
            }
            500 => {
                let body = result.json::<WhitelistEndpointErrorInternalPayload>().await?;
                Err(WhitelistError::Status500 { error: body.error })
            }
            _ => Err(WhitelistError::UnexpectedStatus(result.status()))
        }
    }

    async fn count_whitelist(&self, authorization: &str) -> Result<i64, WhitelistError> {
        let mut url = self.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("v1")
            .push("whitelist")
            .push("count");

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        {
            let headers_vec: Vec<(&str, String)> = headers.iter().map(|(k, v)| crate::hide_authorization(k, v)).collect();
            tracing::info!(method="get", url=url.to_string(), headers=?headers_vec, body="<no_body>", "count_whitelist");
        }
        let mut builder = reqwest::Client::builder();
        if self.allow_insecure {
            builder = builder.danger_accept_invalid_certs(true);
        }
        let client = builder.build()?;
        let result = client
            .get(url)
            .headers(headers)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<i64>().await?;
                Ok(body)
            }
            401 => {
                let body = result.json::<WhitelistEndpointErrorUnauthorizedPayload>().await?;
                Err(WhitelistError::Status401 { message: body.message })
            }
            500 => {
                let body = result.json::<WhitelistEndpointErrorInternalPayload>().await?;
                Err(WhitelistError::Status500 { error: body.error })
            }
            _ => Err(WhitelistError::UnexpectedStatus(result.status()))
        }
    }

    async fn add_to_whitelist(&self, field0: Vec<String>, authorization: &str) -> Result<(), WhitelistError> {
        let mut url = self.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("v1")
            .push("whitelist");

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        {
            let headers_vec: Vec<(&str, String)> = headers.iter().map(|(k, v)| crate::hide_authorization(k, v)).collect();
            tracing::info!(method="post", url=url.to_string(), headers=?headers_vec, body=serde_json::to_string(&field0).unwrap(), "add_to_whitelist");
        }
        let mut builder = reqwest::Client::builder();
        if self.allow_insecure {
            builder = builder.danger_accept_invalid_certs(true);
        }
        let client = builder.build()?;
        let result = client
            .post(url)
            .headers(headers)
            .json(&field0)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = ();
                Ok(body)
            }
            401 => {
                let body = result.json::<WhitelistEndpointErrorUnauthorizedPayload>().await?;
                Err(WhitelistError::Status401 { message: body.message })
            }
            500 => {
                let body = result.json::<WhitelistEndpointErrorInternalPayload>().await?;
                Err(WhitelistError::Status500 { error: body.error })
            }
            _ => Err(WhitelistError::UnexpectedStatus(result.status()))
        }
    }

    async fn remove_from_whitelist(&self, field0: Vec<String>, authorization: &str) -> Result<(), WhitelistError> {
        let mut url = self.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("v1")
            .push("whitelist")
            .push("remove");

        let mut headers = reqwest::header::HeaderMap::new();
        headers.append("authorization", reqwest::header::HeaderValue::from_str(&format!("{authorization}"))?);
        {
            let headers_vec: Vec<(&str, String)> = headers.iter().map(|(k, v)| crate::hide_authorization(k, v)).collect();
            tracing::info!(method="post", url=url.to_string(), headers=?headers_vec, body=serde_json::to_string(&field0).unwrap(), "remove_from_whitelist");
        }
        let mut builder = reqwest::Client::builder();
        if self.allow_insecure {
            builder = builder.danger_accept_invalid_certs(true);
        }
        let client = builder.build()?;
        let result = client
            .post(url)
            .headers(headers)
            .json(&field0)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = ();
                Ok(body)
            }
            401 => {
                let body = result.json::<WhitelistEndpointErrorUnauthorizedPayload>().await?;
                Err(WhitelistError::Status401 { message: body.message })
            }
            500 => {
                let body = result.json::<WhitelistEndpointErrorInternalPayload>().await?;
                Err(WhitelistError::Status500 { error: body.error })
            }
            _ => Err(WhitelistError::UnexpectedStatus(result.status()))
        }
    }
}