diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2020-02-13 12:54:12 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2020-02-13 12:54:12 +0100 |
commit | 732d289605b72260711357b73ba5fa88921d77fd (patch) | |
tree | 4f013cd5ea6a9e10b017c467fbe2732be6774deb | |
parent | 1ba083af3576d20b5c1fc1c6ebacefd91ceb6636 (diff) | |
download | rust-zotapi-732d289605b72260711357b73ba5fa88921d77fd.tar.gz rust-zotapi-732d289605b72260711357b73ba5fa88921d77fd.tar.bz2 rust-zotapi-732d289605b72260711357b73ba5fa88921d77fd.zip |
Add fetching ABConfig for only a given contact.
-rw-r--r-- | src/abconfig.rs | 36 | ||||
-rw-r--r-- | tests/zotapi.rs | 12 |
2 files changed, 48 insertions, 0 deletions
diff --git a/src/abconfig.rs b/src/abconfig.rs index 294467a..c8af6c5 100644 --- a/src/abconfig.rs +++ b/src/abconfig.rs @@ -16,19 +16,55 @@ use crate::{client::Client, error::Error}; use serde::Deserialize; +use std::collections::HashMap; +use std::iter::FromIterator; +/// A struct for storing a key value pair with a category in +/// relation to a contact. Typically used to store permissions +/// a given contact has on a given channel. #[derive(Debug, Deserialize)] pub struct ABConfig { + /// Database ID for this entry pub id: u32, + + /// Channel pub chan: u32, + + /// Unique identifier for the related contact pub xchan: String, + + /// Category pub cat: String, + + /// Key pub k: String, + + /// Value pub v: String, } +/// Fetch ABConfig for all contacts pub fn fetch(client: &Client) -> Result<Vec<ABConfig>, Error> { let payload = client.fetch_stream("abconfig", &())?; Ok(serde_json::from_str(&payload)?) } +#[derive(Default)] +pub struct ABConfigRequest { + abook_id: u32, +} + +impl ABConfigRequest { + pub fn fetch(&self, client: &Client) -> Result<Vec<ABConfig>, Error> { + let data: HashMap<&str, String> = HashMap::from_iter(vec![("abook_id", self.abook_id.to_string())]); + let body = client.fetch_stream("abconfig", &data)?; + Ok(serde_json::from_str(&body)?) + } +} + +/// Fetch ABConfig for a given contact +/// +/// `abook` is the abook id of the given contact +pub fn with_abook_id(abook: u32) -> ABConfigRequest { + ABConfigRequest { abook_id: abook } +} diff --git a/tests/zotapi.rs b/tests/zotapi.rs index 70a9564..9dbf5cf 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -275,6 +275,18 @@ fn fetch_abconfig() { } #[test] +fn fetch_abconfig_for_specific_contact() { + let m = mock_with_authorization("GET", "/api/z/1.0/abconfig") + .match_query(Matcher::UrlEncoded("abook_id".into(), 42.to_string())) + .with_status(200) + .with_body("[]") + .create(); + + zotapi::abconfig::with_abook_id(42).fetch(&client()).unwrap(); + m.assert(); +} + +#[test] fn fetch_privacy_groups() { let m = default_mock("GET", "/api/z/1.0/group"); let _res = zotapi::group().fetch(&client()).unwrap(); |