Added maybe_async to Request, Download and Upload
parent
ec202568e1
commit
059a3befae
|
@ -15,8 +15,13 @@ use reqwest::header::CONTENT_TYPE;
|
||||||
|
|
||||||
use crate::{client::Client, core::session::URLPart};
|
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 {
|
impl Client {
|
||||||
#[cfg(feature = "async")]
|
#[maybe_async::maybe_async]
|
||||||
pub async fn download(&self, blob_id: &str) -> crate::Result<Vec<u8>> {
|
pub async fn download(&self, blob_id: &str) -> crate::Result<Vec<u8>> {
|
||||||
let account_id = self.default_account_id();
|
let account_id = self.default_account_id();
|
||||||
let mut download_url = String::with_capacity(
|
let mut download_url = String::with_capacity(
|
||||||
|
@ -49,7 +54,7 @@ impl Client {
|
||||||
headers.remove(CONTENT_TYPE);
|
headers.remove(CONTENT_TYPE);
|
||||||
|
|
||||||
Client::handle_error(
|
Client::handle_error(
|
||||||
reqwest::Client::builder()
|
HttpClient::builder()
|
||||||
.timeout(Duration::from_millis(self.timeout()))
|
.timeout(Duration::from_millis(self.timeout()))
|
||||||
.redirect(self.redirect_policy())
|
.redirect(self.redirect_policy())
|
||||||
.default_headers(headers)
|
.default_headers(headers)
|
||||||
|
@ -64,50 +69,4 @@ impl Client {
|
||||||
.map(|bytes| bytes.to_vec())
|
.map(|bytes| bytes.to_vec())
|
||||||
.map_err(|err| err.into())
|
.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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,11 @@ use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{client::Client, core::session::URLPart};
|
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)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct UploadResponse {
|
pub struct UploadResponse {
|
||||||
#[serde(rename = "accountId")]
|
#[serde(rename = "accountId")]
|
||||||
|
@ -32,7 +37,7 @@ pub struct UploadResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
#[cfg(feature = "async")]
|
#[maybe_async::maybe_async]
|
||||||
pub async fn upload(
|
pub async fn upload(
|
||||||
&self,
|
&self,
|
||||||
account_id: Option<&str>,
|
account_id: Option<&str>,
|
||||||
|
@ -58,7 +63,7 @@ impl Client {
|
||||||
|
|
||||||
serde_json::from_slice::<UploadResponse>(
|
serde_json::from_slice::<UploadResponse>(
|
||||||
&Client::handle_error(
|
&Client::handle_error(
|
||||||
reqwest::Client::builder()
|
HttpClient::builder()
|
||||||
.timeout(Duration::from_millis(self.timeout()))
|
.timeout(Duration::from_millis(self.timeout()))
|
||||||
.redirect(self.redirect_policy())
|
.redirect(self.redirect_policy())
|
||||||
.default_headers(self.headers().clone())
|
.default_headers(self.headers().clone())
|
||||||
|
@ -78,50 +83,6 @@ impl Client {
|
||||||
)
|
)
|
||||||
.map_err(|err| err.into())
|
.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 {
|
impl UploadResponse {
|
||||||
|
|
|
@ -489,22 +489,17 @@ impl<'x> Request<'x> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "async")]
|
#[maybe_async::maybe_async]
|
||||||
pub async fn send(self) -> crate::Result<Response<TaggedMethodResponse>> {
|
pub async fn send(self) -> crate::Result<Response<TaggedMethodResponse>> {
|
||||||
self.client.send(&self).await
|
self.client.send(&self).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "blocking")]
|
|
||||||
pub fn send(self) -> crate::Result<Response<TaggedMethodResponse>> {
|
|
||||||
self.client.send(&self)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "websockets")]
|
#[cfg(feature = "websockets")]
|
||||||
pub async fn send_ws(self) -> crate::Result<String> {
|
pub async fn send_ws(self) -> crate::Result<String> {
|
||||||
self.client.send_ws(self).await
|
self.client.send_ws(self).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "async")]
|
#[maybe_async::maybe_async]
|
||||||
pub async fn send_single<T>(self) -> crate::Result<T>
|
pub async fn send_single<T>(self) -> crate::Result<T>
|
||||||
where
|
where
|
||||||
T: DeserializeOwned,
|
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 {
|
pub fn params(&self, method: Method) -> RequestParams {
|
||||||
RequestParams {
|
RequestParams {
|
||||||
account_id: self.account_id.clone(),
|
account_id: self.account_id.clone(),
|
||||||
|
|
Loading…
Reference in New Issue