From c9ac54435649973c1aa0543434cc344e66cf33f8 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 9 Jun 2019 22:51:27 +0200 Subject: Fetch privacy groups. --- examples/zot/group.rs | 37 +++++++++++++++++++++++++++++++++++++ examples/zot/mod.rs | 1 + examples/zotcli.rs | 13 +++++++++++++ src/client.rs | 6 ++++++ src/group.rs | 34 ++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + tests/zotapi.rs | 14 ++++++++++++++ 7 files changed, 106 insertions(+) create mode 100644 examples/zot/group.rs create mode 100644 src/group.rs diff --git a/examples/zot/group.rs b/examples/zot/group.rs new file mode 100644 index 0000000..71f60d6 --- /dev/null +++ b/examples/zot/group.rs @@ -0,0 +1,37 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use serde_json::{Value, from_str}; + +pub fn list(data: &str) { + if let Ok(Value::Array(groups)) = from_str(&data) { + + println!("Id | Group name | uid | flags | hash"); + println!("----+------------------+-----+-------+-------------------------"); + + for group in groups { + println!("{:4} | {:16} | {:3} | | {}", + group["id"], + group["gname"], + group["uid"], + group["hash"]) + } + } + else { + eprintln!("Invalid data"); + } +} diff --git a/examples/zot/mod.rs b/examples/zot/mod.rs index 3bb3c93..706f496 100644 --- a/examples/zot/mod.rs +++ b/examples/zot/mod.rs @@ -1,6 +1,7 @@ pub mod abconfig; pub mod abook; pub mod channel_stream; +pub mod group; pub mod item; pub mod network_stream; pub mod xchan; diff --git a/examples/zotcli.rs b/examples/zotcli.rs index 986dc85..ceb463b 100644 --- a/examples/zotcli.rs +++ b/examples/zotcli.rs @@ -53,6 +53,10 @@ fn main() { (@subcommand abconfig => (about: "Fetch abconfig") ) + (@subcommand group => + (about: "Fetch privacy groups") + (@arg raw: --raw "Display raw json payload") + ) (@subcommand xchan => (about: "Fetch xchan info") (@arg raw: --raw "Display raw json payload") @@ -87,6 +91,15 @@ fn main() { let raw = m.is_present("raw"); zot::abook::fetch(&client, raw); }, + ("group", Some(m)) => { + let res = client.group().fetch().unwrap(); + if m.is_present("raw") { + println!("{}", res); + } + else { + zot::group::list(&res); + } + }, ("xchan", Some(m)) => { let raw = m.is_present("raw"); let t = if m.is_present("guid") { diff --git a/src/client.rs b/src/client.rs index 138b80c..d5d264a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -16,6 +16,7 @@ use crate::{ error::Error, + group::GroupFetcher, item::ItemBuilder, abook::AbookFetcher, abconfig::ABConfigFetcher, @@ -35,6 +36,7 @@ pub const ZOTAPI_ABOOK_PATH : &str = "/api/z/1.0/abook"; 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_ITEM_UPDATE_PATH : &str = "/api/z/1.0/item/update"; pub const ZOTAPI_XCHAN_PATH : &str = "/api/z/1.0/xchan"; @@ -71,6 +73,10 @@ impl Client { self.fetch_stream(ZOTAPI_NETWORK_STREAM_PATH, &()) } + pub fn group(&self) -> GroupFetcher { + GroupFetcher::new(self) + } + pub fn item(&self) -> ItemBuilder { ItemBuilder::new(self) } diff --git a/src/group.rs b/src/group.rs new file mode 100644 index 0000000..64a6847 --- /dev/null +++ b/src/group.rs @@ -0,0 +1,34 @@ +// zotapi - Rust wrapper for Sot API as implemented by Hubzilla +// Copyright (C) 2018 Harald Eilertsen +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use crate::{ + client::{self, Client}, + error::Error, +}; + +pub struct GroupFetcher<'a> { + client: &'a Client, +} + +impl<'a> GroupFetcher<'a> { + pub fn new(client: &'a Client) -> GroupFetcher<'a> { + GroupFetcher { client } + } + + pub fn fetch(&self) -> Result { + self.client.fetch_stream(client::ZOTAPI_GROUP_PATH, &()) + } +} diff --git a/src/lib.rs b/src/lib.rs index be8cb9c..9ec45e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,7 @@ mod abconfig; mod abook; mod client; mod error; +mod group; mod item; mod xchan; diff --git a/tests/zotapi.rs b/tests/zotapi.rs index 93c9795..7d10c6b 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -239,3 +239,17 @@ fn fetch_abconfig() { let _res = z.abconfig().fetch().unwrap(); m.assert(); } + +#[test] +fn fetch_privacy_groups() { + let m = mock("GET", "/api/z/1.0/group") + .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().fetch().unwrap(); + m.assert(); +} -- cgit v1.2.3