From 0c76f0c9727a512475e29b5d099b5b7188f72052 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Jan 2020 23:32:44 +0100 Subject: Move Client out of the api objects. Also make constructor functions in the zotapi namespace. --- src/item.rs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) (limited to 'src/item.rs') diff --git a/src/item.rs b/src/item.rs index 532ac16..52492aa 100644 --- a/src/item.rs +++ b/src/item.rs @@ -75,29 +75,27 @@ fn convert_itemdata_list_with_one_member_to_a_string() { /// /// ```no_run /// let client = zotapi::Client::new("https://myhub.com", "mychannel", "mypw"); -/// let new_post = client.item() +/// let new_post = zotapi::item() /// .title("A title") /// .body("The body of the post") /// .file("/my/photo.jpg") -/// .create()?; +/// .create(&client)?; /// # Ok::<(), zotapi::Error>(()) /// ``` #[derive(Debug)] pub struct ItemBuilder<'a> { - client: &'a Client, data: BTreeMap<&'a str, ItemData<'a>>, files: Vec<&'a str>, } -impl<'a> ItemBuilder<'a> { - pub fn new(client: &'a Client) -> ItemBuilder<'a> { - ItemBuilder { - client: client, - data: BTreeMap::new(), - files: vec![], - } +pub fn item<'a>() -> ItemBuilder<'a> { + ItemBuilder { + data: BTreeMap::new(), + files: vec![], } +} +impl<'a> ItemBuilder<'a> { /// Add a title to the post pub fn title(&mut self, text: &'a str) -> &mut ItemBuilder<'a> { self.data.insert("title", ItemData::Value(text)); @@ -128,22 +126,19 @@ impl<'a> ItemBuilder<'a> { } /// Create the item by poting it to the server - pub fn create(&self) -> Result { + pub fn create(&self, client: &Client) -> Result { dbg!(self); if self.files.is_empty() { - self.client - .post_data("item/update", &self.data) + client.post_data("item/update", &self.data) } else { - self.client - .post_multipart("item/update", &self.data, &self.files) + client.post_multipart("item/update", &self.data, &self.files) } } } #[test] fn add_group_to_list_of_groups_allowed() { - let client = Client::new("https://test.com", "test", "test1234"); - let mut item = ItemBuilder::new(&client); + let mut item = item(); item.group_allow("test"); match item.data.get("groups_allow") { Some(ItemData::List(v)) => assert_eq!(v.len(), 1), -- cgit v1.2.3