From 9abac137c5282d3bd78776e9da3608c3a32cb49f Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 19 Aug 2018 19:32:49 +0200 Subject: Add method for fetching network stream. --- examples/hubzilla-network-stream.rs | 38 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +++++ tests/zotapi.rs | 15 +++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 examples/hubzilla-network-stream.rs diff --git a/examples/hubzilla-network-stream.rs b/examples/hubzilla-network-stream.rs new file mode 100644 index 0000000..29b6a28 --- /dev/null +++ b/examples/hubzilla-network-stream.rs @@ -0,0 +1,38 @@ +extern crate zotapi; +extern crate dotenv; +extern crate serde_json; + +use dotenv::dotenv; +use serde_json::Value; +use std::env; + +fn main() { + dotenv().ok(); + let site = env::var("SITE").expect("SITE variable expected"); + let user = env::var("USER").expect("USER variable expected"); + let password = env::var("PASSWORD").expect("PASSWORD variable expected"); + let client = zotapi::client(&site, &user, &password); + + match client.network_stream() { + Ok(payload) => { + println!("Raw payload: {}", payload); + + match serde_json::from_str(&payload) { + Ok(Value::Array(v)) => { + for item in v.into_iter() { + println!("{} {} {}", item["title"], item["type"], item["author"]["name"]); + } + }, + Ok(_) => { + println!("Wrong type returned, expected an array.") + }, + Err(e) => { + println!("Error: {}", e); + } + } + }, + Err(e) => { + println!("Error getting channel stream: {:?}", e); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index d87e6d0..84c1340 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ use reqwest::{ use std::io::Read; const ZOTAPI_CHANNEL_STREAM_PATH : &str = "/api/z/1.0/channel/stream"; +const ZOTAPI_NETWORK_STREAM_PATH : &str = "/api/z/1.0/network/stream"; #[derive(Debug)] pub enum Error { @@ -41,6 +42,10 @@ impl Client { self.fetch_stream(ZOTAPI_CHANNEL_STREAM_PATH) } + pub fn network_stream(&self) -> Result { + self.fetch_stream(ZOTAPI_NETWORK_STREAM_PATH) + } + fn url(&self, path: &str) -> String { self.base_url.clone() + path } diff --git a/tests/zotapi.rs b/tests/zotapi.rs index 2a376a2..1030b19 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -18,6 +18,21 @@ fn get_channel_stream() { assert_eq!(data.unwrap(), "{}"); } +#[test] +fn get_network_stream() { + let m = mock("GET", "/api/z/1.0/network/stream") + .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 data = z.network_stream(); + m.assert(); + assert_eq!(data.unwrap(), "{}"); +} + #[test] fn return_error_if_invalid_auth_provided() { let m = mock("GET", "/api/z/1.0/channel/stream") -- cgit v1.2.3