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 FilesApiRoutesDeleteFileError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FilesApiRoutesDownloadFileError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FilesApiRoutesGetSignedUrlError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FilesApiRoutesListFilesError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FilesApiRoutesRetrieveFileError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FilesApiRoutesUploadFileError {
UnknownValue(serde_json::Value),
}
pub async fn files_api_routes_delete_file(configuration: &configuration::Configuration, file_id: &str) -> Result<models::DeleteFileOut, Error<FilesApiRoutesDeleteFileError>> {
let p_path_file_id = file_id;
let uri_str = format!("{}/v1/files/{file_id}", configuration.base_path, file_id=crate::apis::urlencode(p_path_file_id));
let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &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::DeleteFileOut`"))),
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::DeleteFileOut`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<FilesApiRoutesDeleteFileError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn files_api_routes_download_file(configuration: &configuration::Configuration, file_id: &str) -> Result<reqwest::Response, Error<FilesApiRoutesDownloadFileError>> {
let p_path_file_id = file_id;
let uri_str = format!("{}/v1/files/{file_id}/content", configuration.base_path, file_id=crate::apis::urlencode(p_path_file_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();
if !status.is_client_error() && !status.is_server_error() {
Ok(resp)
} else {
let content = resp.text().await?;
let entity: Option<FilesApiRoutesDownloadFileError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn files_api_routes_get_signed_url(configuration: &configuration::Configuration, file_id: &str, expiry: Option<i32>) -> Result<models::FileSignedUrl, Error<FilesApiRoutesGetSignedUrlError>> {
let p_path_file_id = file_id;
let p_query_expiry = expiry;
let uri_str = format!("{}/v1/files/{file_id}/url", configuration.base_path, file_id=crate::apis::urlencode(p_path_file_id));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref param_value) = p_query_expiry {
req_builder = req_builder.query(&[("expiry", ¶m_value.to_string())]);
}
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::FileSignedUrl`"))),
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::FileSignedUrl`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<FilesApiRoutesGetSignedUrlError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn files_api_routes_list_files(configuration: &configuration::Configuration, page: Option<i32>, page_size: Option<i32>, include_total: Option<bool>, sample_type: Option<Vec<models::SampleType>>, source: Option<Vec<models::Source>>, search: Option<&str>, purpose: Option<models::FilePurpose>, mimetypes: Option<Vec<String>>) -> Result<models::ListFilesOut, Error<FilesApiRoutesListFilesError>> {
let p_query_page = page;
let p_query_page_size = page_size;
let p_query_include_total = include_total;
let p_query_sample_type = sample_type;
let p_query_source = source;
let p_query_search = search;
let p_query_purpose = purpose;
let p_query_mimetypes = mimetypes;
let uri_str = format!("{}/v1/files", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref param_value) = p_query_page {
req_builder = req_builder.query(&[("page", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_page_size {
req_builder = req_builder.query(&[("page_size", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_include_total {
req_builder = req_builder.query(&[("include_total", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_sample_type {
req_builder = match "multi" {
"multi" => req_builder.query(¶m_value.into_iter().map(|p| ("sample_type".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("sample_type", ¶m_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
}
if let Some(ref param_value) = p_query_source {
req_builder = match "multi" {
"multi" => req_builder.query(¶m_value.into_iter().map(|p| ("source".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("source", ¶m_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
}
if let Some(ref param_value) = p_query_search {
req_builder = req_builder.query(&[("search", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_purpose {
req_builder = req_builder.query(&[("purpose", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_mimetypes {
req_builder = match "multi" {
"multi" => req_builder.query(¶m_value.into_iter().map(|p| ("mimetypes".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("mimetypes", ¶m_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
}
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::ListFilesOut`"))),
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::ListFilesOut`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<FilesApiRoutesListFilesError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn files_api_routes_retrieve_file(configuration: &configuration::Configuration, file_id: &str) -> Result<models::RetrieveFileOut, Error<FilesApiRoutesRetrieveFileError>> {
let p_path_file_id = file_id;
let uri_str = format!("{}/v1/files/{file_id}", configuration.base_path, file_id=crate::apis::urlencode(p_path_file_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::RetrieveFileOut`"))),
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::RetrieveFileOut`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<FilesApiRoutesRetrieveFileError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
pub async fn files_api_routes_upload_file(configuration: &configuration::Configuration, file: std::path::PathBuf, expiry: Option<i32>, visibility: Option<&str>, purpose: Option<models::FilePurpose>) -> Result<models::UploadFileOut, Error<FilesApiRoutesUploadFileError>> {
let p_form_file = file;
let p_form_expiry = expiry;
let p_form_visibility = visibility;
let p_form_purpose = purpose;
let uri_str = format!("{}/v1/files", 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();
if let Some(param_value) = p_form_expiry {
multipart_form = multipart_form.text("expiry", param_value.to_string());
}
if let Some(param_value) = p_form_visibility {
multipart_form = multipart_form.text("visibility", param_value.to_string());
}
if let Some(param_value) = p_form_purpose {
multipart_form = multipart_form.text("purpose", serde_json::to_string(¶m_value)?);
}
let file = TokioFile::open(&p_form_file).await?;
let stream = FramedRead::new(file, BytesCodec::new());
let file_name = p_form_file.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("file", 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::UploadFileOut`"))),
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::UploadFileOut`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<FilesApiRoutesUploadFileError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}