aboutsummaryrefslogtreecommitdiffstats
path: root/src/item.rs
diff options
context:
space:
mode:
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?)?)
}
}