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,
}
#[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.set_path("v1/whitelist/enabled");
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::<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.set_path("v1/whitelist/enabled");
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)
.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.set_path("v1/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 result = reqwest::Client::builder()
.build()?
.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.set_path("v1/whitelist/count");
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::<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.set_path("v1/whitelist");
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 = ();
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.set_path("v1/whitelist/remove");
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 = ();
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()))
}
}
}