From 059a3befaea2046f68cec3680d3f801645bdb940 Mon Sep 17 00:00:00 2001 From: Mauro D Date: Tue, 18 Apr 2023 15:10:03 +0000 Subject: [PATCH] Added maybe_async to Request, Download and Upload --- src/blob/download.rs | 55 ++++++-------------------------------------- src/blob/upload.rs | 53 ++++++------------------------------------ src/core/request.rs | 25 ++------------------ 3 files changed, 16 insertions(+), 117 deletions(-) diff --git a/src/blob/download.rs b/src/blob/download.rs index 5bf2cd4..3a53ab0 100644 --- a/src/blob/download.rs +++ b/src/blob/download.rs @@ -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> { 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> { - 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()) - } } diff --git a/src/blob/upload.rs b/src/blob/upload.rs index 4447dda..74b6a69 100644 --- a/src/blob/upload.rs +++ b/src/blob/upload.rs @@ -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::( &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, - content_type: Option<&str>, - ) -> crate::Result { - 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::( - &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 { diff --git a/src/core/request.rs b/src/core/request.rs index 70fc796..724f59a 100644 --- a/src/core/request.rs +++ b/src/core/request.rs @@ -489,22 +489,17 @@ impl<'x> Request<'x> { self } - #[cfg(feature = "async")] + #[maybe_async::maybe_async] pub async fn send(self) -> crate::Result> { self.client.send(&self).await } - #[cfg(feature = "blocking")] - pub fn send(self) -> crate::Result> { - self.client.send(&self) - } - #[cfg(feature = "websockets")] pub async fn send_ws(self) -> crate::Result { self.client.send_ws(self).await } - #[cfg(feature = "async")] + #[maybe_async::maybe_async] pub async fn send_single(self) -> crate::Result where T: DeserializeOwned, @@ -520,22 +515,6 @@ impl<'x> Request<'x> { } } - #[cfg(feature = "blocking")] - pub fn send_single(self) -> crate::Result - where - T: DeserializeOwned, - { - let response: Response> = 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(),