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(Clone, Debug, Default)]
pub struct DeleteObjectHeaders {
pub x_dtz_realm: Option<String>,
}
#[derive(Clone, Debug, Default)]
pub struct GetObjectHeaders {
pub x_dtz_realm: Option<String>,
}
#[derive(Clone, Debug, Default)]
pub struct GetObjectMetadataHeaders {
pub x_dtz_realm: Option<String>,
}
#[derive(Clone, Debug, Default)]
pub struct ListObjectsHeaders {
pub x_dtz_realm: Option<String>,
}
#[derive(Clone, Debug, Default)]
pub struct PutObjectHeaders {
pub x_dtz_expiration: Option<String>,
pub x_dtz_expire_in: Option<String>,
pub x_dtz_expire_at: Option<String>,
pub x_dtz_realm: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DeleteObjectError {
Status401(models::ErrorMessage),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DisableServiceError {
Status401(models::ErrorMessage),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum EnableServiceError {
Status401(models::ErrorMessage),
Status500(models::ErrorMessage),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetObjectError {
Status404(),
Status401(models::ErrorMessage),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetObjectMetadataError {
Status404(),
Status401(models::ErrorMessage),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ListObjectsError {
Status401(models::ErrorMessage),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PutObjectError {
Status400(models::ErrorMessage),
Status401(models::ErrorMessage),
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum StatsError {
Status401(models::ErrorMessage),
UnknownValue(serde_json::Value),
}
pub async fn delete_object(configuration: &Configuration, object_path: &str, headers: Option<DeleteObjectHeaders>) -> Result<(), Error<DeleteObjectError>> {
let p_path_object_path = object_path;
let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
if let Some(h) = &headers {
if let Some(param_value) = h.x_dtz_realm.as_ref() {
req_builder = req_builder.header("X-DTZ-REALM", param_value.to_string());
}
}
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
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<DeleteObjectError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn disable_service(configuration: &Configuration) -> Result<(), Error<DisableServiceError>> {
let uri_str = format!("{}/disable", build_url(configuration));
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
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<DisableServiceError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn enable_service(configuration: &Configuration) -> Result<(), Error<EnableServiceError>> {
let uri_str = format!("{}/enable", build_url(configuration));
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
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<EnableServiceError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn get_object(configuration: &Configuration, object_path: &str, headers: Option<GetObjectHeaders>) -> Result<reqwest::Response, Error<GetObjectError>> {
let p_path_object_path = object_path;
let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(h) = &headers {
if let Some(param_value) = h.x_dtz_realm.as_ref() {
req_builder = req_builder.header("X-DTZ-REALM", param_value.to_string());
}
}
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
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(resp)
} else {
let content = resp.text().await?;
let entity: Option<GetObjectError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn get_object_metadata(configuration: &Configuration, object_path: &str, headers: Option<GetObjectMetadataHeaders>) -> Result<(), Error<GetObjectMetadataError>> {
let p_path_object_path = object_path;
let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
let mut req_builder = configuration.client.request(reqwest::Method::HEAD, &uri_str);
if let Some(h) = &headers {
if let Some(param_value) = h.x_dtz_realm.as_ref() {
req_builder = req_builder.header("X-DTZ-REALM", param_value.to_string());
}
}
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
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<GetObjectMetadataError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn list_objects(configuration: &Configuration, prefix: Option<&str>, headers: Option<ListObjectsHeaders>) -> Result<Vec<models::ObjectMetadata>, Error<ListObjectsError>> {
let p_query_prefix = prefix;
let uri_str = format!("{}/obj/", build_url(configuration));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(h) = &headers {
if let Some(param_value) = h.x_dtz_realm.as_ref() {
req_builder = req_builder.header("X-DTZ-REALM", param_value.to_string());
}
}
if let Some(ref param_value) = p_query_prefix {
req_builder = req_builder.query(&[("prefix", ¶m_value.to_string())]);
}
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
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::ObjectMetadata>`"))),
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::ObjectMetadata>`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<ListObjectsError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn put_object(configuration: &Configuration, object_path: &str, body: Option<Vec<u8>>, headers: Option<PutObjectHeaders>) -> Result<(), Error<PutObjectError>> {
let p_path_object_path = object_path;
let p_body_body = body;
let uri_str = format!("{}/obj/{objectPath}", build_url(configuration), objectPath=crate::apis::urlencode(p_path_object_path));
let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
if let Some(h) = &headers {
if let Some(param_value) = h.x_dtz_expiration.as_ref() {
req_builder = req_builder.header("X-DTZ-EXPIRATION", param_value.to_string());
}
if let Some(param_value) = h.x_dtz_expire_in.as_ref() {
req_builder = req_builder.header("X-DTZ-EXPIRE-IN", param_value.to_string());
}
if let Some(param_value) = h.x_dtz_expire_at.as_ref() {
req_builder = req_builder.header("X-DTZ-EXPIRE-AT", param_value.to_string());
}
if let Some(param_value) = h.x_dtz_realm.as_ref() {
req_builder = req_builder.header("X-DTZ-REALM", param_value.to_string());
}
}
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.body(p_body_body.unwrap_or_default());
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<PutObjectError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn stats(configuration: &Configuration) -> Result<models::Stats, Error<StatsError>> {
let uri_str = format!("{}/stats", build_url(configuration));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref value) = configuration.api_key {
req_builder = req_builder.header("X-API-KEY", value);
};
if let Some(ref token) = configuration.oauth_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
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::Stats`"))),
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::Stats`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<StatsError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}