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