From 8158bee4cbb5cc599c38788f0aa12a489e5261c1 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 10 Jun 2019 17:32:32 +0200 Subject: Implement fetching privacy group members. --- src/client.rs | 9 +++++++-- src/group.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ tests/zotapi.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index d5d264a..67c153f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -16,7 +16,7 @@ use crate::{ error::Error, - group::GroupFetcher, + group::{GroupFetcher, GroupMembersFetcher}, item::ItemBuilder, abook::AbookFetcher, abconfig::ABConfigFetcher, @@ -37,6 +37,7 @@ pub const ZOTAPI_ABCONFIG_PATH : &str = "/api/z/1.0/abconfig"; pub const ZOTAPI_CHANNEL_STREAM_PATH : &str = "/api/z/1.0/channel/stream"; pub const ZOTAPI_NETWORK_STREAM_PATH : &str = "/api/z/1.0/network/stream"; pub const ZOTAPI_GROUP_PATH : &str = "/api/z/1.0/group"; +pub const ZOTAPI_GROUP_MEMBERS_PATH : &str = "/api/z/1.0/group_members"; pub const ZOTAPI_ITEM_UPDATE_PATH : &str = "/api/z/1.0/item/update"; pub const ZOTAPI_XCHAN_PATH : &str = "/api/z/1.0/xchan"; @@ -77,6 +78,10 @@ impl Client { GroupFetcher::new(self) } + pub fn group_members(&self) -> GroupMembersFetcher { + GroupMembersFetcher::new(self) + } + pub fn item(&self) -> ItemBuilder { ItemBuilder::new(self) } @@ -100,7 +105,7 @@ impl Client { pub fn fetch_stream(&self, path: &str, args: &T) -> Result where T: Serialize { - let url = self.url(path, args); + let url = dbg!(self.url(path, args)); let mut res = self.inner.get(&url) .header(ACCEPT, "application/json") .basic_auth(self.user.clone(), Some(self.pw.clone())) diff --git a/src/group.rs b/src/group.rs index 64a6847..c01d5ae 100644 --- a/src/group.rs +++ b/src/group.rs @@ -18,6 +18,7 @@ use crate::{ client::{self, Client}, error::Error, }; +use serde::{Serialize, Serializer}; pub struct GroupFetcher<'a> { client: &'a Client, @@ -32,3 +33,48 @@ impl<'a> GroupFetcher<'a> { self.client.fetch_stream(client::ZOTAPI_GROUP_PATH, &()) } } + + +enum GroupSelector<'a> { + Id(u64), + Name(&'a str), +} + +impl<'a> Serialize for GroupSelector<'a> { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + match *self { + GroupSelector::Id(id) => [("group_id", id)].serialize(serializer), + GroupSelector::Name(n) => [("group_name", n)].serialize(serializer), + } + } +} + +pub struct GroupMembersFetcher<'a> { + client: &'a Client, + id: Option>, +} + +impl<'a> GroupMembersFetcher<'a> { + pub fn new(client: &'a Client) -> GroupMembersFetcher<'a> { + GroupMembersFetcher { client, id: None } + } + + pub fn by_group_id(&mut self, id: u64) -> &GroupMembersFetcher<'a> { + self.id = Some(GroupSelector::Id(id)); + self + } + + pub fn by_group_name(&mut self, name: &'a str) -> &GroupMembersFetcher<'a> { + self.id = Some(GroupSelector::Name(name)); + self + } + + pub fn fetch(&self) -> Result { + self.client.fetch_stream( + client::ZOTAPI_GROUP_MEMBERS_PATH, + &self.id.as_ref().unwrap()) + } +} diff --git a/tests/zotapi.rs b/tests/zotapi.rs index 7d10c6b..9ad5cf9 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -253,3 +253,33 @@ fn fetch_privacy_groups() { let _res = z.group().fetch().unwrap(); m.assert(); } + +#[test] +fn fetch_members_of_group_by_group_id() { + let m = mock("GET", Matcher::Regex( + r"^/api/z/1.0/group_members\?([^&]*&)*group_id=42".into())) + .match_header("Authorization", Matcher::Regex(r"Basic \w+".into())) + .with_status(200) + .with_header("content-type", "application/json") + .with_body("{}") + .create(); + + let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234"); + let _res = z.group_members().by_group_id(42).fetch().unwrap(); + m.assert(); +} + +#[test] +fn fetch_members_of_group_by_group_name() { + let m = mock("GET", Matcher::Regex( + r"^/api/z/1.0/group_members\?([^&]*&)*group_name=Friends\+of\+pain".into())) + .match_header("Authorization", Matcher::Regex(r"Basic \w+".into())) + .with_status(200) + .with_header("content-type", "application/json") + .with_body("{}") + .create(); + + let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234"); + let _res = z.group_members().by_group_name("Friends of pain").fetch().unwrap(); + m.assert(); +} -- cgit v1.2.3