use reqwest;
#[allow(unused_imports)]
use serde::{de::Error as _};
use crate::{apis::ResponseContent, models};
#[allow(unused_imports)]
use super::{Error, ContentType};
use dtz_config::Configuration;
fn build_url(config: &Configuration) -> String {
if let Some(base_path) = &config.base_path {
let base = url::Url::parse(base_path).unwrap();
let mut target_url = url::Url::parse(crate::apis::SVC_URL).unwrap();
let _ = target_url.set_scheme(base.scheme());
let _ = target_url.set_port(base.port());
let _ = target_url.set_host(Some(base.host_str().unwrap()));
format!("{target_url}")
} else {
crate::apis::SVC_URL.to_string()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ChargeStripePostError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CheckFundedError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetStatsError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ListTransactionsError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PostConsumptionError {
UnknownValue(serde_json::Value),
}
pub async fn charge_stripe_post(configuration: &Configuration) -> Result<(), Error<ChargeStripePostError>> {
let uri_str = format!("{}/charge/stripe", build_url(configuration));
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
Ok(())
} else {
let content = resp.text().await?;
let entity: Option<ChargeStripePostError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn check_funded(configuration: &Configuration, identity: Option<&str>) -> Result<models::CheckFunded200Response, Error<CheckFundedError>> {
let p_query_identity = identity;
let uri_str = format!("{}/funded", build_url(configuration));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref param_value) = p_query_identity {
req_builder = req_builder.query(&[("identity", ¶m_value.to_string())]);
}
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CheckFunded200Response`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CheckFunded200Response`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<CheckFundedError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn get_stats(configuration: &Configuration) -> Result<models::GetStats200Response, Error<GetStatsError>> {
let uri_str = format!("{}/stats", build_url(configuration));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetStats200Response`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::GetStats200Response`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<GetStatsError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn list_transactions(configuration: &Configuration, start: Option<String>, end: Option<String>, service: Option<&str>, context_id: Option<&str>) -> Result<Vec<models::Transaction>, Error<ListTransactionsError>> {
let p_query_start = start;
let p_query_end = end;
let p_query_service = service;
let p_query_context_id = context_id;
let uri_str = format!("{}/transaction", build_url(configuration));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref param_value) = p_query_start {
req_builder = req_builder.query(&[("start", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_end {
req_builder = req_builder.query(&[("end", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_service {
req_builder = req_builder.query(&[("service", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_context_id {
req_builder = req_builder.query(&[("contextId", ¶m_value.to_string())]);
}
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec<models::Transaction>`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec<models::Transaction>`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<ListTransactionsError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn post_consumption(configuration: &Configuration, consumption: Option<models::Consumption>) -> Result<(), Error<PostConsumptionError>> {
let p_body_consumption = consumption;
let uri_str = format!("{}/consumption", build_url(configuration));
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
req_builder = req_builder.json(&p_body_consumption);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
Ok(())
} else {
let content = resp.text().await?;
let entity: Option<PostConsumptionError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}