From fb3d996d90f9e496654ae4485b9d036dcc7baf8a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 7 Aug 2018 12:11:39 +0200 Subject: First iteration of getting channel stream from server. --- Cargo.toml | 4 ++++ src/lib.rs | 32 ++++++++++++++++++++++++++++++++ tests/zotapi.rs | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 tests/zotapi.rs diff --git a/Cargo.toml b/Cargo.toml index 6b09435..1c2f542 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,7 @@ version = "0.1.0" authors = ["haraldei"] [dependencies] +reqwest = "0.8" + +[dev-dependencies] +mockito = "0.12" diff --git a/src/lib.rs b/src/lib.rs index 31e1bb2..60ac388 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,35 @@ +extern crate reqwest; + +use std::io::Read; + +const ZOTAPI_CHANNEL_STREAM_PATH : &str = "/api/z/1.0/channel/stream"; + +pub struct Client { + base_url: String, +} + +impl Client { + pub fn channel_stream(&self) -> String { + let mut res = reqwest::get(&self.channel_stream_url()).unwrap(); + let mut body = String::new(); + res.read_to_string(&mut body).unwrap(); + body + } + + fn channel_stream_url(&self) -> String { + self.base_url.clone() + ZOTAPI_CHANNEL_STREAM_PATH + } +} + +pub fn client(url: T) -> Client +where + T: Into +{ + Client { + base_url: url.into(), + } +} + #[cfg(test)] mod tests { #[test] diff --git a/tests/zotapi.rs b/tests/zotapi.rs new file mode 100644 index 0000000..c45673f --- /dev/null +++ b/tests/zotapi.rs @@ -0,0 +1,18 @@ +extern crate zotapi; +extern crate mockito; + +use mockito::mock; + +#[test] +fn get_channel_stream() { + let m = mock("GET", "/api/z/1.0/channel/stream") + .with_status(200) + .with_header("content-type", "application/json") + .with_body("{}") + .create(); + + let z = zotapi::client(format!("http://{}", mockito::SERVER_ADDRESS)); + let data = z.channel_stream(); + m.assert(); + assert_eq!(data, "{}"); +} -- cgit v1.2.3