From d08138ea633da76d9ca390e4f9e3c5489aee23d1 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 5 Jul 2021 22:03:47 +0200 Subject: Update reqwest and make async. This means adding the full tokio as a dependency. While there isn't much gain to going async in the current cli demo app, a full fledged app may have more to gain by it. First foray into async rust, so I might not do it right... --- src/client.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'src/client.rs') diff --git a/src/client.rs b/src/client.rs index c681b27..3f43479 100644 --- a/src/client.rs +++ b/src/client.rs @@ -22,6 +22,7 @@ use reqwest::{ }; use serde::Serialize; use std::collections::BTreeMap; +use std::fs::File; use std::io::Read; use url::Url; @@ -56,22 +57,17 @@ impl Client { r.to_string() } - pub fn fetch_stream(&self, path: &str, args: &T) -> Result + pub async fn fetch_stream(&self, path: &str, args: &T) -> Result where T: Serialize + std::fmt::Debug, { let url = dbg!(self.url(path, args)); - let res = self - .inner - .get(&url) - .header(ACCEPT, "application/json") - .basic_auth(self.user.clone(), Some(self.pw.clone())) - .send()?; + let res = self.get(&url).send().await?; - handle_result(res) + handle_result(res).await } - pub fn post_data(&self, path: &str, data: &T) -> Result + pub async fn post_data(&self, path: &str, data: &T) -> Result where T: Serialize + std::fmt::Debug, { @@ -84,12 +80,12 @@ impl Client { .basic_auth(self.user.clone(), Some(self.pw.clone())) .body(serde_qs::to_string(&data)?)) //.form(&data)) - .send()?; + .send().await?; - handle_result(res) + handle_result(res).await } - pub fn post_multipart( + pub async fn post_multipart( &self, path: &str, data: &BTreeMap<&str, T>, @@ -106,7 +102,14 @@ impl Client { } for f in files.iter() { - form = form.file("media", f).unwrap(); + let mut pdata = vec![]; + let path = std::path::Path::new(f); + File::open(path)?.read_to_end(&mut pdata)?; + + let filename = String::from(path.file_name().unwrap().to_str().unwrap()); + let p = reqwest::multipart::Part::bytes(pdata) + .file_name(filename); + form = form.part("media", p); } let res = self @@ -114,9 +117,10 @@ impl Client { .post(&url) .basic_auth(self.user.clone(), Some(self.pw.clone())) .multipart(form) - .send()?; + .send() + .await?; - handle_result(res) + handle_result(res).await } /// Return a RequestBuilder object that's set up with the correct @@ -139,13 +143,11 @@ impl Client { // A common function for handling the response after a request. // // Consumes the response, and return it as a string or an error. -fn handle_result(mut res: reqwest::Response) -> Result { +async fn handle_result(res: reqwest::Response) -> Result { match res.status() { StatusCode::UNAUTHORIZED => Err(Error::Unauthorized), StatusCode::OK => { - let mut body = String::new(); - res.read_to_string(&mut body)?; - Ok(body) + Ok(res.text().await?) } _ => { eprintln!("Received unknown status: {:?}", res.status()); -- cgit v1.2.3