diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2020-02-14 15:00:27 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2020-02-14 15:15:10 +0100 |
commit | cd83b0f4c30f21d23bc9046a73c7058c450007e3 (patch) | |
tree | f37eb9c94d6d6b9ae064f7a978009af43b710595 | |
parent | 036fd6b2ea078205f625595cd481cdead307e79d (diff) | |
download | rust-zotapi-cd83b0f4c30f21d23bc9046a73c7058c450007e3.tar.gz rust-zotapi-cd83b0f4c30f21d23bc9046a73c7058c450007e3.tar.bz2 rust-zotapi-cd83b0f4c30f21d23bc9046a73c7058c450007e3.zip |
Update abook with struct and request object.
-rw-r--r-- | examples/zot/abook.rs | 29 | ||||
-rw-r--r-- | src/abook.rs | 107 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | tests/zotapi.rs | 2 |
4 files changed, 109 insertions, 31 deletions
diff --git a/examples/zot/abook.rs b/examples/zot/abook.rs index b710fa7..97025c3 100644 --- a/examples/zot/abook.rs +++ b/examples/zot/abook.rs @@ -15,13 +15,11 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -pub fn fetch(client: &zotapi::Client, raw: bool) { - match zotapi::abook().fetch(&client) { - Ok(payload) => { - if raw { - println!("{}", &payload); - } else { - process(&payload); +pub fn fetch(client: &zotapi::Client, _raw: bool) { + match zotapi::Abook::z().fetch(&client) { + Ok(abooks) => { + for b in abooks { + println!("{:?}", b); } } Err(e) => { @@ -29,20 +27,3 @@ pub fn fetch(client: &zotapi::Client, raw: bool) { } } } - -fn process(payload: &str) { - let data: serde_json::Value = serde_json::from_str(&payload).unwrap(); - match data { - serde_json::Value::Array(v) => { - for contact in v { - println!( - "{} ({}, {})", - contact["xchan_name"], contact["xchan_addr"], contact["xchan_network"] - ); - } - } - _ => { - println!("Unexpected data:\n{}", payload); - } - }; -} diff --git a/src/abook.rs b/src/abook.rs index 8423872..59981ba 100644 --- a/src/abook.rs +++ b/src/abook.rs @@ -15,15 +15,112 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use crate::{client::Client, error::Error}; +use serde::Deserialize; -pub struct Abook; +#[derive(Debug, Default, Deserialize)] +pub struct Abook { + #[serde(rename = "abook_id")] + id: u32, -pub fn abook() -> Abook { - Abook {} + #[serde(rename = "abook_account")] + account: u32, + + #[serde(rename = "abook_channel")] + channel: u32, + + #[serde(rename = "abook_xchan")] + xchan: String, + + #[serde(rename = "abook_my_perms")] + my_perms: u16, + + #[serde(rename = "abook_their_perms")] + their_perms: u16, + + // Closeness is a number between 0 and 99, but is somehow + // returned as a string. + #[serde(rename = "abook_closeness")] + closeness: String, + + #[serde(rename = "abook_created")] + created: String, + + #[serde(rename = "abook_updated")] + updated: String, + + #[serde(rename = "abook_connected")] + connected: String, + + #[serde(rename = "abook_dob")] + dob: String, + + #[serde(rename = "abook_flags")] + flags: u16, // No longer used + + #[serde(rename = "abook_profile")] + profile: String, + + #[serde(rename = "abook_blocked")] + blocked: u8, + + #[serde(rename = "abook_ignored")] + ignored: u8, + + #[serde(rename = "abook_hidden")] + hidden: u8, + + #[serde(rename = "abook_archived")] + archived: u8, + + #[serde(rename = "abook_pending")] + pending: u8, + + #[serde(rename = "abook_unconnected")] + unconnected: u8, // Currently unused + + #[serde(rename = "abook_self")] + myself: u8, // original name `self` + + #[serde(rename = "abook_feed")] + feed: u8, + + #[serde(rename = "abook_incl")] + incl: String, + + #[serde(rename = "abook_excl")] + excl: String, + + #[serde(rename = "abook_instance")] + instance: String, } impl Abook { - pub fn fetch(&self, client: &Client) -> Result<String, Error> { - client.fetch_stream("abook", &()) + pub fn z() -> AbookRequest { + AbookRequest::default() + } +} + +#[derive(Debug, Default)] +pub struct AbookRequest { + abook_id: Option<u32>, +} + +impl AbookRequest { + /// Limit to ABConfigs for a given contact + /// + /// `abook` is the abook id of the given contact + pub fn with_abook_id(mut self, abook: u32) -> AbookRequest { + self.abook_id = Some(abook); + self + } + + pub fn fetch(&self, client: &Client) -> Result<Vec<Abook>, Error> { + let mut req = client.get("abook"); + + if let Some(id) = self.abook_id { + req = req.query(&[("abook_id", id.to_string())]); + } + + Ok(serde_json::from_str(&req.send()?.text()?)?) } } @@ -25,7 +25,7 @@ mod network_stream; mod xchan; pub use abconfig::ABConfig; -pub use abook::abook; +pub use abook::Abook; pub use channel_stream::channel_stream; pub use client::*; pub use error::Error; diff --git a/tests/zotapi.rs b/tests/zotapi.rs index a405e35..cd127ac 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -232,7 +232,7 @@ fn fetch_xchan_by_guid() { #[test] fn fetch_connections() { let m = default_mock("GET", "/api/z/1.0/abook"); - let _res = zotapi::abook().fetch(&client()).unwrap(); + let _res = zotapi::Abook::z().fetch(&client()).unwrap(); m.assert(); } |