aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2020-01-04 13:15:00 +0100
committerHarald Eilertsen <haraldei@anduin.net>2020-01-04 13:15:00 +0100
commit1ba083af3576d20b5c1fc1c6ebacefd91ceb6636 (patch)
tree54a04c60fcd6721ab5c0cb782f1fc2bef38538b7
parent862a3b67d701b3c124e1b682db0cee5fc5b711da (diff)
downloadrust-zotapi-1ba083af3576d20b5c1fc1c6ebacefd91ceb6636.tar.gz
rust-zotapi-1ba083af3576d20b5c1fc1c6ebacefd91ceb6636.tar.bz2
rust-zotapi-1ba083af3576d20b5c1fc1c6ebacefd91ceb6636.zip
abconfig: reorg + parse result into struct.
-rw-r--r--Cargo.toml1
-rw-r--r--examples/zot/abconfig.rs15
-rw-r--r--src/abconfig.rs21
-rw-r--r--src/error.rs7
-rw-r--r--src/lib.rs3
-rw-r--r--tests/zotapi.rs33
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 <https://www.gnu.org/licenses/>.
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<String, Error> {
- client.fetch_stream("abconfig", &())
- }
+pub fn fetch(client: &Client) -> Result<Vec<ABConfig>, 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<std::io::Error> for Error {
}
}
+impl From<serde_json::Error> for Error {
+ fn from(e: serde_json::Error) -> Error {
+ Error::Json(e)
+ }
+}
+
impl From<serde_qs::Error> 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 <https://www.gnu.org/licenses/>.
-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]