From 2f600bea194b053b310a181857c5d801471fe7be Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 12 Jun 2019 14:39:04 +0200 Subject: Fix uploading of attachments to posts. The field needs to be named `media`, and we also need to handle the result of the operation. This refactors the result handling into a separate private function. --- src/client.rs | 54 ++++++++++++++++++++++++++---------------------------- tests/zotapi.rs | 6 +++--- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/client.rs b/src/client.rs index 67c153f..e1561bf 100644 --- a/src/client.rs +++ b/src/client.rs @@ -106,50 +106,31 @@ impl Client { where T: Serialize { let url = dbg!(self.url(path, args)); - let mut res = self.inner.get(&url) + let res = self.inner.get(&url) .header(ACCEPT, "application/json") .basic_auth(self.user.clone(), Some(self.pw.clone())) .send()?; - match res.status() { - StatusCode::UNAUTHORIZED => Err(Error::Unauthorized), - StatusCode::OK => { - let mut body = String::new(); - res.read_to_string(&mut body)?; - Ok(body) - }, - _ => { - println!("Received unknown status: {:?}", res.status()); - Err(Error::Unknown) - } - } + handle_result(res) } pub fn post_data(&self, path: &str, data: &T) -> Result where T: Serialize, { - let url = self.url(path, &()); - let mut res = self.inner.post(&url) + let url = dbg!(self.url(path, &())); + let res = self.inner.post(&url) .header(ACCEPT, "application/json") .header(CONTENT_TYPE, "application/x-www-form-urlencoded") .basic_auth(self.user.clone(), Some(self.pw.clone())) .form(&data) .send()?; - match res.status() { - StatusCode::UNAUTHORIZED => Err(Error::Unauthorized), - StatusCode::OK => { - let mut body = String::new(); - res.read_to_string(&mut body)?; - Ok(body) - }, - _ => Err(Error::Unknown) - } + handle_result(res) } pub fn post_multipart(&self, path: &str, data: &BTreeMap<&str, &str>, files: &Vec<&str>) -> Result { - let url = self.url(path, &()); + let url = dbg!(self.url(path, &())); let mut form = reqwest::multipart::Form::new(); for (key, value) in data.iter() { @@ -157,7 +138,7 @@ impl Client { } for f in files.iter() { - form = form.file("files", f).unwrap(); + form = form.file("media", f).unwrap(); } let res = self.inner.post(&url) @@ -166,8 +147,25 @@ impl Client { .multipart(form) .send()?; - match res.status() { - _ => Err(Error::Unknown) + handle_result(res) + } +} + +// 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 { + match res.status() { + StatusCode::UNAUTHORIZED => Err(Error::Unauthorized), + StatusCode::OK => { + let mut body = String::new(); + res.read_to_string(&mut body)?; + Ok(body) + }, + _ => { + eprintln!("Received unknown status: {:?}", res.status()); + Err(Error::Unknown) } } } + diff --git a/tests/zotapi.rs b/tests/zotapi.rs index 9ad5cf9..1a81dc3 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -114,7 +114,7 @@ fn upload_item_with_media_file() { "Content-Disposition: form-data; name=\"title\"\r\n" + "\r\nA title\r\n" + "--.+\r\n" + - "Content-Disposition: form-data; name=\"files\"; filename=\"testfile.txt\"\r\n" + + "Content-Disposition: form-data; name=\"media\"; filename=\"testfile.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\ntestfile contents\n" + "\r\n--.+--\r\n")) @@ -146,11 +146,11 @@ fn upload_item_with_two_files() { "Content-Disposition: form-data; name=\"title\"\r\n" + "\r\nA title\r\n" + "--.+\r\n" + - "Content-Disposition: form-data; name=\"files\"; filename=\"testfile.txt\"\r\n" + + "Content-Disposition: form-data; name=\"media\"; filename=\"testfile.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\ntestfile contents\n" + "\r\n--.+\r\n" + - "Content-Disposition: form-data; name=\"files\"; filename=\"testfile.txt\"\r\n" + + "Content-Disposition: form-data; name=\"media\"; filename=\"testfile.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\ntestfile contents\n" + "\r\n--.+--\r\n")) -- cgit v1.2.3