From 1ba083af3576d20b5c1fc1c6ebacefd91ceb6636 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Jan 2020 13:15:00 +0100 Subject: abconfig: reorg + parse result into struct. --- Cargo.toml | 1 + examples/zot/abconfig.rs | 15 ++++++++++++--- src/abconfig.rs | 21 +++++++++++++-------- src/error.rs | 7 +++++++ src/lib.rs | 3 +-- tests/zotapi.rs | 33 +++++++++++++++++++++++++++++++-- 6 files changed, 65 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d6d0c43..f8bf749 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ edition = "2018" [dependencies] reqwest = "0.9.1" serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" serde_qs = "0.5.2" url = "2.1" diff --git a/examples/zot/abconfig.rs b/examples/zot/abconfig.rs index 2362408..a6454ac 100644 --- a/examples/zot/abconfig.rs +++ b/examples/zot/abconfig.rs @@ -18,9 +18,18 @@ use zotapi; pub fn fetch(client: &zotapi::Client) { - match zotapi::abconfig().fetch(&client) { - Ok(payload) => { - println!("{}", payload); + match zotapi::abconfig::fetch(&client) { + Ok(v) => { + println!("Id: Chan: Cat: Key: Val: xchan:"); + for entry in v { + println!("{:6} {:6} {:15} {:15} {:4} {}", + entry.id, + entry.chan, + entry.cat, + entry.k, + entry.v, + entry.xchan); + } } Err(e) => { println!("{:?}", e); diff --git a/src/abconfig.rs b/src/abconfig.rs index 7957dea..294467a 100644 --- a/src/abconfig.rs +++ b/src/abconfig.rs @@ -15,15 +15,20 @@ // along with this program. If not, see . use crate::{client::Client, error::Error}; +use serde::Deserialize; -pub struct ABConfig; - -pub fn abconfig() -> ABConfig { - ABConfig {} +#[derive(Debug, Deserialize)] +pub struct ABConfig { + pub id: u32, + pub chan: u32, + pub xchan: String, + pub cat: String, + pub k: String, + pub v: String, } -impl ABConfig { - pub fn fetch(&self, client: &Client) -> Result { - client.fetch_stream("abconfig", &()) - } +pub fn fetch(client: &Client) -> Result, Error> { + let payload = client.fetch_stream("abconfig", &())?; + Ok(serde_json::from_str(&payload)?) } + diff --git a/src/error.rs b/src/error.rs index 1e349d9..9559ae6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -21,6 +21,7 @@ use std; pub enum Error { Http(reqwest::Error), Io(std::io::Error), + Json(serde_json::Error), Qs(serde_qs::Error), Unauthorized, Unknown, @@ -38,6 +39,12 @@ impl From for Error { } } +impl From for Error { + fn from(e: serde_json::Error) -> Error { + Error::Json(e) + } +} + impl From for Error { fn from(e: serde_qs::Error) -> Error { Error::Qs(e) diff --git a/src/lib.rs b/src/lib.rs index 12396ca..7a17269 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -mod abconfig; +pub mod abconfig; mod abook; mod channel_stream; mod client; @@ -24,7 +24,6 @@ mod item; mod network_stream; mod xchan; -pub use abconfig::abconfig; pub use abook::abook; pub use channel_stream::channel_stream; pub use client::*; diff --git a/tests/zotapi.rs b/tests/zotapi.rs index 1621af1..70a9564 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -240,9 +240,38 @@ fn fetch_connections() { #[test] fn fetch_abconfig() { - let m = default_mock("GET", "/api/z/1.0/abconfig"); - let _res = zotapi::abconfig().fetch(&client()).unwrap(); + let data = r#" + [ + { + "id": 666, + "chan": 42, + "xchan": "xchanhash1", + "cat": "some_other_cat", + "k": "key1", + "v": "value1" + }, + { + "id": 667, + "chan": 44, + "xchan": "xchanhash2", + "cat": "some_cat", + "k": "key2", + "v": "value2" + } + + ]"#; + + let m = mock_with_authorization("GET", "/api/z/1.0/abconfig") + .with_status(200) + .with_body(&data) + .create(); + + let res = zotapi::abconfig::fetch(&client()).unwrap(); m.assert(); + + assert_eq!(res.len(), 2); + assert_eq!(res[0].id, 666); + assert_eq!(res[1].k, "key2"); } #[test] -- cgit v1.2.3