aboutsummaryrefslogtreecommitdiffstats
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
parent23286103c0d3a1f9b49432da2b2bb2eadc022243 (diff)
downloadrust-zotapi-8294f640abc38611007cdc2697e5117b6c1663b9.tar.gz
rust-zotapi-8294f640abc38611007cdc2697e5117b6c1663b9.tar.bz2
rust-zotapi-8294f640abc38611007cdc2697e5117b6c1663b9.zip
Begin multipart/form-data upload support.
-rw-r--r--src/client.rs18
-rw-r--r--src/item.rs14
-rw-r--r--tests/zotapi.rs20
3 files changed, 51 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)
+ }
}
}
diff --git a/tests/zotapi.rs b/tests/zotapi.rs
index 3f889c5..37979fd 100644
--- a/tests/zotapi.rs
+++ b/tests/zotapi.rs
@@ -102,6 +102,26 @@ fn create_new_post_with_title() {
}
#[test]
+fn upload_item_with_media_file() {
+ let m = mock("POST", "/api/z/1.0/item/update")
+ .match_header("authorization", Matcher::Regex(r"Basic \w+".into()))
+ .match_header("content-type", Matcher::Regex("^multipart/form-data; boundary=.+$".into()))
+ .with_status(200)
+ .with_header("content-type", "application/json")
+ .with_body("{}")
+ .create();
+
+ let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
+ let _res = z.item()
+ .title("A title")
+ .body("This is a test")
+ .file("testfile.jpg")
+ .create();
+
+ m.assert();
+}
+
+#[test]
fn fetch_xchan_by_address() {
let m = mock("GET", "/api/z/1.0/xchan?address=test%40test.com")
.match_header("Authorization", Matcher::Regex(r"Basic \w+".into()))