pub enum GetAccountSummaryError {
RequestFailure(reqwest::Error),
InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
UnexpectedStatus(reqwest::StatusCode),
Status500 {
error: String,
},
}
impl From<reqwest::Error> for GetAccountSummaryError {
fn from(error: reqwest::Error) -> GetAccountSummaryError {
GetAccountSummaryError::RequestFailure(error)
}
}
impl From<reqwest::header::InvalidHeaderValue> for GetAccountSummaryError {
fn from(error: reqwest::header::InvalidHeaderValue) -> GetAccountSummaryError {
GetAccountSummaryError::InvalidHeaderValue(error)
}
}
impl GetAccountSummaryError {
pub fn to_account_summary_endpoint_error(&self) -> Option<crate::model::AccountSummaryEndpointError> {
match self {
GetAccountSummaryError::Status500 { error } => Some(crate::model::AccountSummaryEndpointError::Internal { error: error.clone() }),
_ => None
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AccountSummaryEndpointErrorInternalPayload {
pub error: String,
}
pub enum CountAccountSummaryError {
RequestFailure(reqwest::Error),
InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
UnexpectedStatus(reqwest::StatusCode),
Status500 {
error: String,
},
}
impl From<reqwest::Error> for CountAccountSummaryError {
fn from(error: reqwest::Error) -> CountAccountSummaryError {
CountAccountSummaryError::RequestFailure(error)
}
}
impl From<reqwest::header::InvalidHeaderValue> for CountAccountSummaryError {
fn from(error: reqwest::header::InvalidHeaderValue) -> CountAccountSummaryError {
CountAccountSummaryError::InvalidHeaderValue(error)
}
}
impl CountAccountSummaryError {
pub fn to_account_summary_endpoint_error(&self) -> Option<crate::model::AccountSummaryEndpointError> {
match self {
CountAccountSummaryError::Status500 { error } => Some(crate::model::AccountSummaryEndpointError::Internal { error: error.clone() }),
_ => None
}
}
}
#[async_trait::async_trait]
pub trait AccountSummary {
async fn get_account_summary(&self, skip: i32, limit: i32, authorization: &str) -> Result<Vec<crate::model::AccountSummary>, GetAccountSummaryError>;
async fn count_account_summary(&self, authorization: &str) -> Result<i64, CountAccountSummaryError>;
}
#[derive(Clone, Debug)]
pub struct AccountSummaryLive {
pub base_url: reqwest::Url,
}
#[async_trait::async_trait]
impl AccountSummary for AccountSummaryLive {
async fn get_account_summary(&self, skip: i32, limit: i32, authorization: &str) -> Result<Vec<crate::model::AccountSummary>, GetAccountSummaryError> {
let mut url = self.base_url.clone();
url.set_path("admin/accounts");
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::AccountSummary>>().await?;
Ok(body)
}
500 => {
let body = result.json::<AccountSummaryEndpointErrorInternalPayload>().await?;
Err(GetAccountSummaryError::Status500 { error: body.error })
}
_ => Err(GetAccountSummaryError::UnexpectedStatus(result.status()))
}
}
async fn count_account_summary(&self, authorization: &str) -> Result<i64, CountAccountSummaryError> {
let mut url = self.base_url.clone();
url.set_path("admin/accounts/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)
}
500 => {
let body = result.json::<AccountSummaryEndpointErrorInternalPayload>().await?;
Err(CountAccountSummaryError::Status500 { error: body.error })
}
_ => Err(CountAccountSummaryError::UnexpectedStatus(result.status()))
}
}
}