aboutsummaryrefslogtreecommitdiffstats
path: root/src/item.rs
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2021-07-05 22:03:47 +0200
committerHarald Eilertsen <haraldei@anduin.net>2021-07-05 22:03:47 +0200
commitd08138ea633da76d9ca390e4f9e3c5489aee23d1 (patch)
tree80a682350c75d6e01f05144cf054e9d48edbed07 /src/item.rs
parent098adf3a6895529bbd467f13b55fc241099c2ff7 (diff)
downloadrust-zotapi-d08138ea633da76d9ca390e4f9e3c5489aee23d1.tar.gz
rust-zotapi-d08138ea633da76d9ca390e4f9e3c5489aee23d1.tar.bz2
rust-zotapi-d08138ea633da76d9ca390e4f9e3c5489aee23d1.zip
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...
Diffstat (limited to 'src/item.rs')
-rw-r--r--src/item.rs21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/item.rs b/src/item.rs
index 7c5935d..d61401b 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -21,6 +21,8 @@ use reqwest::{
};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
+use std::fs::File;
+use std::io::Read;
/// Data type for values that an Item can hold.
#[derive(Debug, Serialize)]
@@ -81,13 +83,17 @@ pub struct ItemCreatedResponse {
/// Typical usage:
///
/// ```no_run
+/// # async {
/// let client = zotapi::client("https://myhub.com", "mychannel", "mypw");
/// let new_post = zotapi::item()
/// .title("A title")
/// .body("The body of the post")
/// .file("/my/photo.jpg")
-/// .create(&client)?;
+/// .create(&client)
+/// .await?;
/// # Ok::<(), zotapi::Error>(())
+/// # };
+/// ```
/// ```
#[derive(Debug)]
pub struct ItemBuilder<'a> {
@@ -133,7 +139,7 @@ impl<'a> ItemBuilder<'a> {
}
/// Create the item by poting it to the server
- pub fn create(&self, client: &Client) -> Result<ItemCreatedResponse, Error> {
+ pub async fn create(&self, client: &Client) -> Result<ItemCreatedResponse, Error> {
dbg!(self);
let mut req = client.post("item/update");
@@ -149,13 +155,20 @@ impl<'a> ItemBuilder<'a> {
}
for f in self.files.iter() {
- form = form.file("media", f)?;
+ 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);
}
req = req.multipart(form)
}
- Ok(serde_json::from_str(&req.send()?.text()?)?)
+ Ok(serde_json::from_str(&req.send().await?.text().await?)?)
}
}