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/abconfig.rs | 16 +++++++--------- src/abook.rs | 16 +++++++--------- src/client.rs | 33 +-------------------------------- src/group.rs | 35 ++++++++++++++++------------------- src/item.rs | 29 ++++++++++++----------------- src/lib.rs | 5 +++++ src/xchan.rs | 22 ++++++++++------------ 7 files changed, 58 insertions(+), 98 deletions(-) (limited to 'src') diff --git a/src/abconfig.rs b/src/abconfig.rs index 6f9adbe..36b3427 100644 --- a/src/abconfig.rs +++ b/src/abconfig.rs @@ -19,16 +19,14 @@ use crate::{ error::Error, }; -pub struct ABConfigFetcher<'a> { - client: &'a Client, -} +pub struct ABConfig; -impl<'a> ABConfigFetcher<'a> { - pub fn new(client: &'a Client) -> ABConfigFetcher<'a> { - ABConfigFetcher { client } - } +pub fn abconfig() -> ABConfig { + ABConfig {} +} - pub fn fetch(&self) -> Result { - self.client.fetch_stream("abconfig", &()) +impl ABConfig { + pub fn fetch(&self, client: &Client) -> Result { + client.fetch_stream("abconfig", &()) } } diff --git a/src/abook.rs b/src/abook.rs index 5f34ff5..40ef5a4 100644 --- a/src/abook.rs +++ b/src/abook.rs @@ -20,16 +20,14 @@ use crate::{ }; -pub struct AbookFetcher<'a> { - client: &'a Client, -} +pub struct Abook; -impl<'a> AbookFetcher<'a> { - pub fn new(client: &'a Client) -> AbookFetcher<'a> { - AbookFetcher { client } - } +pub fn abook() -> Abook { + Abook {} +} - pub fn fetch(&self) -> Result { - self.client.fetch_stream("abook", &()) +impl Abook { + pub fn fetch(&self, client: &Client) -> Result { + client.fetch_stream("abook", &()) } } diff --git a/src/client.rs b/src/client.rs index 4c20854..dc6cf43 100644 --- a/src/client.rs +++ b/src/client.rs @@ -14,14 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{ - abconfig::ABConfigFetcher, - abook::AbookFetcher, - error::Error, - group::{GroupFetcher, GroupMembersFetcher}, - item::ItemBuilder, - xchan::XChanFetcher, -}; +use crate::error::Error; use reqwest::{ self, header::{ACCEPT, CONTENT_TYPE}, @@ -50,14 +43,6 @@ impl Client { } } - pub fn abconfig(&self) -> ABConfigFetcher { - ABConfigFetcher::new(self) - } - - pub fn abook(&self) -> AbookFetcher { - AbookFetcher::new(self) - } - pub fn channel_stream(&self) -> Result { self.fetch_stream("channel/stream", &()) } @@ -66,22 +51,6 @@ impl Client { self.fetch_stream("network/stream", &()) } - pub fn group(&self) -> GroupFetcher { - GroupFetcher::new(self) - } - - pub fn group_members(&self) -> GroupMembersFetcher { - GroupMembersFetcher::new(self) - } - - pub fn item(&self) -> ItemBuilder { - ItemBuilder::new(self) - } - - pub fn xchan(&self) -> XChanFetcher { - XChanFetcher::new(self) - } - fn url(&self, path: &str, args: &T) -> String where T: Serialize + std::fmt::Debug, diff --git a/src/group.rs b/src/group.rs index 9135425..e17addb 100644 --- a/src/group.rs +++ b/src/group.rs @@ -20,17 +20,15 @@ use crate::{ }; use serde::Serialize; -pub struct GroupFetcher<'a> { - client: &'a Client, -} +pub struct Group; -impl<'a> GroupFetcher<'a> { - pub fn new(client: &'a Client) -> GroupFetcher<'a> { - GroupFetcher { client } - } +pub fn group() -> Group { + Group { } +} - pub fn fetch(&self) -> Result { - self.client.fetch_stream("group", &()) +impl Group { + pub fn fetch(&self, client: &Client) -> Result { + client.fetch_stream("group", &()) } } @@ -43,27 +41,26 @@ enum GroupSelector<'a> { Name(&'a str), } -pub struct GroupMembersFetcher<'a> { - client: &'a Client, +pub struct GroupMembers<'a> { id: Option>, } -impl<'a> GroupMembersFetcher<'a> { - pub fn new(client: &'a Client) -> GroupMembersFetcher<'a> { - GroupMembersFetcher { client, id: None } - } +pub fn group_members<'a>() -> GroupMembers<'a> { + GroupMembers { id: None } +} - pub fn by_group_id(&mut self, id: u64) -> &GroupMembersFetcher<'a> { +impl<'a> GroupMembers<'a> { + pub fn by_group_id(&mut self, id: u64) -> &GroupMembers<'a> { self.id = Some(GroupSelector::Id(id)); self } - pub fn by_group_name(&mut self, name: &'a str) -> &GroupMembersFetcher<'a> { + pub fn by_group_name(&mut self, name: &'a str) -> &GroupMembers<'a> { self.id = Some(GroupSelector::Name(name)); self } - pub fn fetch(&self) -> Result { - self.client.fetch_stream("group_members", &self.id.as_ref().unwrap()) + pub fn fetch(&self, client: &Client) -> Result { + client.fetch_stream("group_members", &self.id.as_ref().unwrap()) } } 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), diff --git a/src/lib.rs b/src/lib.rs index eb5d897..c4a39c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,8 +22,13 @@ mod group; mod item; mod xchan; +pub use abconfig::abconfig; +pub use abook::abook; pub use client::Client; pub use error::Error; +pub use group::{group, group_members}; +pub use item::item; +pub use xchan::xchan; pub fn client(url: &str, user: &str, pw: &str) -> Client { Client::new(url, user, pw) diff --git a/src/xchan.rs b/src/xchan.rs index 5d1c17b..20bbe8d 100644 --- a/src/xchan.rs +++ b/src/xchan.rs @@ -29,33 +29,31 @@ enum XChanSelector<'a> { GUID(&'a str), } -pub struct XChanFetcher<'a> { - client: &'a Client, +pub struct XChan<'a> { data: Option>, } -impl<'a> XChanFetcher<'a> { - pub fn new(client: &'a Client) -> XChanFetcher<'a> { - XChanFetcher { client, data: None } - } +pub fn xchan<'a>() -> XChan<'a> { + XChan { data: None } +} - pub fn by_address(&mut self, addr: &'a str) -> &mut XChanFetcher<'a> { +impl<'a> XChan<'a> { + pub fn by_address(&mut self, addr: &'a str) -> &mut XChan<'a> { self.data = Some(XChanSelector::Address(addr)); self } - pub fn by_hash(&mut self, hash: &'a str) -> &mut XChanFetcher<'a> { + pub fn by_hash(&mut self, hash: &'a str) -> &mut XChan<'a> { self.data = Some(XChanSelector::Hash(hash)); self } - pub fn by_guid(&mut self, guid: &'a str) -> &mut XChanFetcher<'a> { + pub fn by_guid(&mut self, guid: &'a str) -> &mut XChan<'a> { self.data = Some(XChanSelector::GUID(guid)); self } - pub fn fetch(&self) -> Result { - self.client - .fetch_stream("xchan", &self.data.as_ref().unwrap()) + pub fn fetch(&self, client: &Client) -> Result { + client.fetch_stream("xchan", &self.data.as_ref().unwrap()) } } -- cgit v1.2.3