aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2018-11-19 00:26:38 +0100
committerHarald Eilertsen <haraldei@anduin.net>2018-11-19 00:26:38 +0100
commit8294f640abc38611007cdc2697e5117b6c1663b9 (patch)
tree88052311550567460329862daa110b518d60bec8 /src
parent23286103c0d3a1f9b49432da2b2bb2eadc022243 (diff)
downloadrust-zotapi-8294f640abc38611007cdc2697e5117b6c1663b9.tar.gz
rust-zotapi-8294f640abc38611007cdc2697e5117b6c1663b9.tar.bz2
rust-zotapi-8294f640abc38611007cdc2697e5117b6c1663b9.zip
Begin multipart/form-data upload support.
Diffstat (limited to 'src')
-rw-r--r--src/client.rs18
-rw-r--r--src/item.rs14
2 files changed, 31 insertions, 1 deletions
diff --git a/src/client.rs b/src/client.rs
index bce4d08..374cfb2 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -132,4 +132,22 @@ impl Client {
_ => Err(Error::Unknown)
}
}
+
+ pub fn post_multipart<T>(&self, path: &str, _data: &T, _files: &Vec<&str>) -> Result<String, Error>
+ where T: Serialize,
+ {
+ let url = self.url(path, &());
+ let f = reqwest::multipart::Form::new()
+ .text("balle", "klorin");
+
+ let res = self.inner.post(&url)
+ .basic_auth(self.user.clone(), Some(self.pw.clone()))
+ .header(CONTENT_TYPE, "multipart/form-data")
+ .multipart(f)
+ .send()?;
+
+ match res.status() {
+ _ => Err(Error::Unknown)
+ }
+ }
}
diff --git a/src/item.rs b/src/item.rs
index 3440fd5..326e4e8 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -21,6 +21,7 @@ use std::collections::BTreeMap;
pub struct ItemBuilder<'a> {
client : &'a Client,
data : BTreeMap<&'a str, &'a str>,
+ files: Vec<&'a str>,
}
impl<'a> ItemBuilder<'a> {
@@ -28,6 +29,7 @@ impl<'a> ItemBuilder<'a> {
ItemBuilder {
client: client,
data: BTreeMap::new(),
+ files: vec![],
}
}
@@ -41,7 +43,17 @@ impl<'a> ItemBuilder<'a> {
self
}
+ pub fn file(&mut self, fname: &'a str) -> &mut ItemBuilder<'a> {
+ self.files.push(fname);
+ self
+ }
+
pub fn create(&self) -> Result<String, Error> {
- self.client.post_data(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data)
+ if self.files.is_empty() {
+ self.client.post_data(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data)
+ }
+ else {
+ self.client.post_multipart(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data, &self.files)
+ }
}
}