use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};
use tokio::fs::File as TokioFile;
use tokio_util::codec::{BytesCodec, FramedRead};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ListUploadFileLayersHandlerError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ListUploadFilesHandlerError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum UploadHandlerError {
UnknownValue(serde_json::Value),
}
pub async fn list_upload_file_layers_handler(configuration: &configuration::Configuration, upload_id: &str, file_name: &str) -> Result<models::UploadFileLayersResponse, Error<ListUploadFileLayersHandlerError>> {
let p_path_upload_id = upload_id;
let p_path_file_name = file_name;
let uri_str = format!("{}/uploads/{upload_id}/files/{file_name}/layers", configuration.base_path, upload_id=crate::apis::urlencode(p_path_upload_id), file_name=crate::apis::urlencode(p_path_file_name));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_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::UploadFileLayersResponse`"))),
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::UploadFileLayersResponse`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<ListUploadFileLayersHandlerError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn list_upload_files_handler(configuration: &configuration::Configuration, upload_id: &str) -> Result<models::UploadFilesResponse, Error<ListUploadFilesHandlerError>> {
let p_path_upload_id = upload_id;
let uri_str = format!("{}/uploads/{upload_id}/files", configuration.base_path, upload_id=crate::apis::urlencode(p_path_upload_id));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_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::UploadFilesResponse`"))),
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::UploadFilesResponse`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<ListUploadFilesHandlerError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn upload_handler(configuration: &configuration::Configuration, files_left_square_bracket_right_square_bracket: Vec<std::path::PathBuf>) -> Result<models::IdResponse, Error<UploadHandlerError>> {
let p_form_files_left_square_bracket_right_square_bracket = files_left_square_bracket_right_square_bracket;
let uri_str = format!("{}/upload", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
let mut multipart_form = reqwest::multipart::Form::new();
for value in &p_form_files_left_square_bracket_right_square_bracket {
let file = TokioFile::open(value).await?;
let stream = FramedRead::new(file, BytesCodec::new());
let file_name = value.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
let file_part = reqwest::multipart::Part::stream(reqwest::Body::wrap_stream(stream)).file_name(file_name);
multipart_form = multipart_form.part("files[]", file_part);
}
req_builder = req_builder.multipart(multipart_form);
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::IdResponse`"))),
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::IdResponse`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<UploadHandlerError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}