From 970df3ea4169eb2fe935a0f50012503079b8636d Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Jan 2020 23:42:37 +0100 Subject: Make channel and network stream behave like the rest. --- examples/zot/channel_stream.rs | 2 +- examples/zot/network_stream.rs | 2 +- src/channel_stream.rs | 33 +++++++++++++++++++++++++++++++++ src/client.rs | 8 -------- src/lib.rs | 4 ++++ src/network_stream.rs | 33 +++++++++++++++++++++++++++++++++ tests/zotapi.rs | 6 +++--- 7 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 src/channel_stream.rs create mode 100644 src/network_stream.rs diff --git a/examples/zot/channel_stream.rs b/examples/zot/channel_stream.rs index b50f93f..bad7152 100644 --- a/examples/zot/channel_stream.rs +++ b/examples/zot/channel_stream.rs @@ -21,7 +21,7 @@ use serde_json; use std::iter::Iterator; pub fn fetch(client: &zotapi::Client, raw: bool) { - match client.channel_stream() { + match zotapi::channel_stream().fetch(&client) { Ok(payload) => { if raw { println!("{}", payload); diff --git a/examples/zot/network_stream.rs b/examples/zot/network_stream.rs index 6dc1a0c..0e6edb4 100644 --- a/examples/zot/network_stream.rs +++ b/examples/zot/network_stream.rs @@ -21,7 +21,7 @@ use serde_json; use std::iter::Iterator; pub fn fetch(client: &zotapi::Client, raw: bool) { - match client.network_stream() { + match zotapi::network_stream().fetch(&client) { Ok(payload) => { if raw { println!("{}", payload); diff --git a/src/channel_stream.rs b/src/channel_stream.rs new file mode 100644 index 0000000..f99ca1c --- /dev/null +++ b/src/channel_stream.rs @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2020 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::Client, + error::Error, +}; + +pub struct ChannelStream; + +pub fn channel_stream() -> ChannelStream { + ChannelStream {} +} + +impl ChannelStream { + pub fn fetch(&self, client: &Client) -> Result { + client.fetch_stream("channel/stream", &()) + } +} diff --git a/src/client.rs b/src/client.rs index dc6cf43..fcafed3 100644 --- a/src/client.rs +++ b/src/client.rs @@ -43,14 +43,6 @@ impl Client { } } - pub fn channel_stream(&self) -> Result { - self.fetch_stream("channel/stream", &()) - } - - pub fn network_stream(&self) -> Result { - self.fetch_stream("network/stream", &()) - } - fn url(&self, path: &str, args: &T) -> String where T: Serialize + std::fmt::Debug, diff --git a/src/lib.rs b/src/lib.rs index c4a39c8..d89eb46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,18 +16,22 @@ mod abconfig; mod abook; +mod channel_stream; mod client; mod error; mod group; mod item; +mod network_stream; mod xchan; pub use abconfig::abconfig; pub use abook::abook; +pub use channel_stream::channel_stream; pub use client::Client; pub use error::Error; pub use group::{group, group_members}; pub use item::item; +pub use network_stream::network_stream; pub use xchan::xchan; pub fn client(url: &str, user: &str, pw: &str) -> Client { diff --git a/src/network_stream.rs b/src/network_stream.rs new file mode 100644 index 0000000..48a04a7 --- /dev/null +++ b/src/network_stream.rs @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2020 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::Client, + error::Error, +}; + +pub struct NetworkStream; + +pub fn network_stream() -> NetworkStream { + NetworkStream {} +} + +impl NetworkStream { + pub fn fetch(&self, client: &Client) -> Result { + client.fetch_stream("network/stream", &()) + } +} diff --git a/tests/zotapi.rs b/tests/zotapi.rs index 4c0a207..e22ba30 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -38,7 +38,7 @@ fn client() -> zotapi::Client { #[test] fn get_channel_stream() { let m = default_mock("GET", "/api/z/1.0/channel/stream"); - let data = client().channel_stream(); + let data = zotapi::channel_stream().fetch(&client()); m.assert(); assert_eq!(data.unwrap(), "{}"); } @@ -46,7 +46,7 @@ fn get_channel_stream() { #[test] fn get_network_stream() { let m = default_mock("GET", "/api/z/1.0/network/stream"); - let data = client().network_stream(); + let data = zotapi::network_stream().fetch(&client()); m.assert(); assert_eq!(data.unwrap(), "{}"); } @@ -59,7 +59,7 @@ fn return_error_if_invalid_auth_provided() { .with_body("This api requires login") .create(); - let data = client().channel_stream(); + let data = zotapi::channel_stream().fetch(&client()); m.assert(); assert!(data.is_err()); assert_eq!(format!("{:?}", data), "Err(Unauthorized)"); -- cgit v1.2.3