diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-08-19 19:32:49 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-08-19 19:32:49 +0200 |
commit | 9abac137c5282d3bd78776e9da3608c3a32cb49f (patch) | |
tree | d874eafbb19788326e4b9e7c29ddd0bce600cec1 | |
parent | f5bdd3f122138e6ea0a8eafedfaf900862070507 (diff) | |
download | rust-zotapi-9abac137c5282d3bd78776e9da3608c3a32cb49f.tar.gz rust-zotapi-9abac137c5282d3bd78776e9da3608c3a32cb49f.tar.bz2 rust-zotapi-9abac137c5282d3bd78776e9da3608c3a32cb49f.zip |
Add method for fetching network stream.
-rw-r--r-- | examples/hubzilla-network-stream.rs | 38 | ||||
-rw-r--r-- | src/lib.rs | 5 | ||||
-rw-r--r-- | tests/zotapi.rs | 15 |
3 files changed, 58 insertions, 0 deletions
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); + } + } +} @@ -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<String, Error> { + 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 @@ -19,6 +19,21 @@ fn get_channel_stream() { } #[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") .with_status(401) |