aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYour Name <you@example.com>2020-05-02 16:01:19 +0200
committerYour Name <you@example.com>2020-05-02 16:01:19 +0200
commiteb917c35008d32b3b27bf132b5f831f05888be54 (patch)
tree9e4373290471bc535a1bcdc23322cf290a30f456
parente95d9f86d2d9f2e689393f1e486b6250b2b075db (diff)
downloadrust-zotapi-eb917c35008d32b3b27bf132b5f831f05888be54.tar.gz
rust-zotapi-eb917c35008d32b3b27bf132b5f831f05888be54.tar.bz2
rust-zotapi-eb917c35008d32b3b27bf132b5f831f05888be54.zip
zotcli: Don't dump raw json response after post item.
-rw-r--r--examples/zot/item.rs8
-rw-r--r--src/client.rs8
-rw-r--r--src/item.rs34
3 files changed, 44 insertions, 6 deletions
diff --git a/examples/zot/item.rs b/examples/zot/item.rs
index 9869492..648a9f0 100644
--- a/examples/zot/item.rs
+++ b/examples/zot/item.rs
@@ -43,8 +43,12 @@ pub fn post(client: &zotapi::Client, args: &ArgMatches) {
}
match msg.create(&client) {
- Ok(payload) => {
- println!("Raw payload: {}", payload);
+ Ok(res) => {
+ if res.success {
+ println!("New item with id {} posted successfully!", res.item_id);
+ } else {
+ println!("The item could not be posted.");
+ }
}
Err(e) => {
println!("Error posting message: {:?}", e);
diff --git a/src/client.rs b/src/client.rs
index 7a54825..c681b27 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -126,6 +126,14 @@ impl Client {
.header(ACCEPT, "application/json")
.basic_auth(self.user.clone(), Some(self.pw.clone()))
}
+
+ /// Return a RequestBuilder object that's set up with the correct
+ /// path and headers for performing a zot api post request.
+ pub fn post(&self, path: &str) -> reqwest::RequestBuilder {
+ self.inner.post(&self.url(path, &()))
+ .header(ACCEPT, "application/json")
+ .basic_auth(self.user.clone(), Some(self.pw.clone()))
+ }
}
// A common function for handling the response after a request.
diff --git a/src/item.rs b/src/item.rs
index cc0c143..361b327 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -15,7 +15,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::{client::Client, error::Error};
-use serde::Serialize;
+use reqwest::{
+ self,
+ header::{CONTENT_TYPE},
+};
+use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
/// Data type for values that an Item can hold.
@@ -66,6 +70,12 @@ fn convert_itemdata_list_with_one_member_to_a_string() {
assert_eq!(l.to_string(), "one");
}
+#[derive(Debug, Deserialize)]
+pub struct ItemCreatedResponse {
+ pub success: bool,
+ pub item_id: u32,
+}
+
/// A structure to help you create an item in a declarative way.
///
/// Typical usage:
@@ -123,13 +133,29 @@ impl<'a> ItemBuilder<'a> {
}
/// Create the item by poting it to the server
- pub fn create(&self, client: &Client) -> Result<String, Error> {
+ pub fn create(&self, client: &Client) -> Result<ItemCreatedResponse, Error> {
dbg!(self);
+ let mut req = client.post("item/update");
+
if self.files.is_empty() {
- client.post_data("item/update", &self.data)
+ req = req
+ .header(CONTENT_TYPE, "application/x-www-form-urlencoded")
+ .body(serde_qs::to_string(&self.data)?)
} else {
- client.post_multipart("item/update", &self.data, &self.files)
+ let mut form = reqwest::multipart::Form::new();
+
+ for (key, value) in self.data.iter() {
+ form = form.text(key.to_string(), value.to_string());
+ }
+
+ for f in self.files.iter() {
+ form = form.file("media", f)?;
+ }
+
+ req = req.multipart(form)
}
+
+ Ok(serde_json::from_str(&req.send()?.text()?)?)
}
}