diff options
Diffstat (limited to 'src/item.rs')
-rw-r--r-- | src/item.rs | 34 |
1 files changed, 30 insertions, 4 deletions
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()?)?) } } |