Added maybe_async to Request, Download and Upload

main
Mauro D 2023-04-18 15:10:03 +00:00
parent ec202568e1
commit 059a3befae
3 changed files with 16 additions and 117 deletions

View File

@ -15,8 +15,13 @@ use reqwest::header::CONTENT_TYPE;
use crate::{client::Client, core::session::URLPart};
#[cfg(feature = "blocking")]
use reqwest::blocking::Client as HttpClient;
#[cfg(feature = "async")]
use reqwest::Client as HttpClient;
impl Client {
#[cfg(feature = "async")]
#[maybe_async::maybe_async]
pub async fn download(&self, blob_id: &str) -> crate::Result<Vec<u8>> {
let account_id = self.default_account_id();
let mut download_url = String::with_capacity(
@ -49,7 +54,7 @@ impl Client {
headers.remove(CONTENT_TYPE);
Client::handle_error(
reqwest::Client::builder()
HttpClient::builder()
.timeout(Duration::from_millis(self.timeout()))
.redirect(self.redirect_policy())
.default_headers(headers)
@ -64,50 +69,4 @@ impl Client {
.map(|bytes| bytes.to_vec())
.map_err(|err| err.into())
}
#[cfg(feature = "blocking")]
pub fn download(&self, blob_id: &str) -> crate::Result<Vec<u8>> {
let account_id = self.default_account_id();
let mut download_url = String::with_capacity(
self.session().download_url().len() + account_id.len() + blob_id.len(),
);
for part in self.download_url() {
match part {
URLPart::Value(value) => {
download_url.push_str(value);
}
URLPart::Parameter(param) => match param {
super::URLParameter::AccountId => {
download_url.push_str(account_id);
}
super::URLParameter::BlobId => {
download_url.push_str(blob_id);
}
super::URLParameter::Name => {
download_url.push_str("none");
}
super::URLParameter::Type => {
download_url.push_str("application/octet-stream");
}
},
}
}
let mut headers = self.headers().clone();
headers.remove(CONTENT_TYPE);
Client::handle_error(
reqwest::blocking::Client::builder()
.timeout(Duration::from_millis(self.timeout()))
.redirect(self.redirect_policy())
.default_headers(headers)
.build()?
.get(download_url)
.send()?,
)?
.bytes()
.map(|bytes| bytes.to_vec())
.map_err(|err| err.into())
}
}

View File

@ -16,6 +16,11 @@ use serde::Deserialize;
use crate::{client::Client, core::session::URLPart};
#[cfg(feature = "blocking")]
use reqwest::blocking::Client as HttpClient;
#[cfg(feature = "async")]
use reqwest::Client as HttpClient;
#[derive(Debug, Deserialize)]
pub struct UploadResponse {
#[serde(rename = "accountId")]
@ -32,7 +37,7 @@ pub struct UploadResponse {
}
impl Client {
#[cfg(feature = "async")]
#[maybe_async::maybe_async]
pub async fn upload(
&self,
account_id: Option<&str>,
@ -58,7 +63,7 @@ impl Client {
serde_json::from_slice::<UploadResponse>(
&Client::handle_error(
reqwest::Client::builder()
HttpClient::builder()
.timeout(Duration::from_millis(self.timeout()))
.redirect(self.redirect_policy())
.default_headers(self.headers().clone())
@ -78,50 +83,6 @@ impl Client {
)
.map_err(|err| err.into())
}
#[cfg(feature = "blocking")]
pub fn upload(
&self,
account_id: Option<&str>,
blob: Vec<u8>,
content_type: Option<&str>,
) -> crate::Result<UploadResponse> {
let account_id = account_id.unwrap_or_else(|| self.default_account_id());
let mut upload_url =
String::with_capacity(self.session().upload_url().len() + account_id.len());
for part in self.upload_url() {
match part {
URLPart::Value(value) => {
upload_url.push_str(value);
}
URLPart::Parameter(param) => {
if let super::URLParameter::AccountId = param {
upload_url.push_str(account_id);
}
}
}
}
serde_json::from_slice::<UploadResponse>(
&Client::handle_error(
reqwest::blocking::Client::builder()
.timeout(Duration::from_millis(self.timeout()))
.redirect(self.redirect_policy())
.default_headers(self.headers().clone())
.build()?
.post(upload_url)
.header(
CONTENT_TYPE,
content_type.unwrap_or("application/octet-stream"),
)
.body(blob)
.send()?,
)?
.bytes()?,
)
.map_err(|err| err.into())
}
}
impl UploadResponse {

View File

@ -489,22 +489,17 @@ impl<'x> Request<'x> {
self
}
#[cfg(feature = "async")]
#[maybe_async::maybe_async]
pub async fn send(self) -> crate::Result<Response<TaggedMethodResponse>> {
self.client.send(&self).await
}
#[cfg(feature = "blocking")]
pub fn send(self) -> crate::Result<Response<TaggedMethodResponse>> {
self.client.send(&self)
}
#[cfg(feature = "websockets")]
pub async fn send_ws(self) -> crate::Result<String> {
self.client.send_ws(self).await
}
#[cfg(feature = "async")]
#[maybe_async::maybe_async]
pub async fn send_single<T>(self) -> crate::Result<T>
where
T: DeserializeOwned,
@ -520,22 +515,6 @@ impl<'x> Request<'x> {
}
}
#[cfg(feature = "blocking")]
pub fn send_single<T>(self) -> crate::Result<T>
where
T: DeserializeOwned,
{
let response: Response<SingleMethodResponse<T>> = self.client.send(&self)?;
match response
.unwrap_method_responses()
.pop()
.ok_or_else(|| Error::Internal("Server returned no results".to_string()))?
{
SingleMethodResponse::Ok((_, response, _)) => Ok(response),
SingleMethodResponse::Error((_, err, _)) => Err(err.into()),
}
}
pub fn params(&self, method: Method) -> RequestParams {
RequestParams {
account_id: self.account_id.clone(),