diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2020-01-03 20:00:55 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2020-01-03 20:00:55 +0100 |
commit | 5f96d9981a6d041f5c8a464cda10a0dc371060f8 (patch) | |
tree | e4c3bae7ff9a02108a979e9840c3aa134fbc0425 /src/client.rs | |
parent | 16a56f12cd2e66e10f8bb5e2c4026d3a7148e680 (diff) | |
download | rust-zotapi-5f96d9981a6d041f5c8a464cda10a0dc371060f8.tar.gz rust-zotapi-5f96d9981a6d041f5c8a464cda10a0dc371060f8.tar.bz2 rust-zotapi-5f96d9981a6d041f5c8a464cda10a0dc371060f8.zip |
Use Url crate and move paths to submodules.
Diffstat (limited to 'src/client.rs')
-rw-r--r-- | src/client.rs | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/src/client.rs b/src/client.rs index ab021fb..4c20854 100644 --- a/src/client.rs +++ b/src/client.rs @@ -30,29 +30,21 @@ use reqwest::{ use serde::Serialize; use std::collections::BTreeMap; use std::io::Read; - -pub const ZOTAPI_ABOOK_PATH: &str = "/api/z/1.0/abook"; -pub const ZOTAPI_ABCONFIG_PATH: &str = "/api/z/1.0/abconfig"; -pub const ZOTAPI_CHANNEL_STREAM_PATH: &str = "/api/z/1.0/channel/stream"; -pub const ZOTAPI_NETWORK_STREAM_PATH: &str = "/api/z/1.0/network/stream"; -pub const ZOTAPI_GROUP_PATH: &str = "/api/z/1.0/group"; -pub const ZOTAPI_GROUP_MEMBERS_PATH: &str = "/api/z/1.0/group_members"; -pub const ZOTAPI_ITEM_UPDATE_PATH: &str = "/api/z/1.0/item/update"; -pub const ZOTAPI_XCHAN_PATH: &str = "/api/z/1.0/xchan"; +use url::Url; #[derive(Debug)] pub struct Client { inner: reqwest::Client, - base_url: String, + base_url: Url, user: String, pw: String, } impl Client { - pub fn new(url: &str, user: &str, pw: &str) -> Client { + pub fn new(base_url: &str, user: &str, pw: &str) -> Client { Client { inner: reqwest::Client::new(), - base_url: String::from(url), + base_url: Url::parse(base_url).unwrap().join("api/z/1.0/").unwrap(), user: String::from(user), pw: String::from(pw), } @@ -67,11 +59,11 @@ impl Client { } pub fn channel_stream(&self) -> Result<String, Error> { - self.fetch_stream(ZOTAPI_CHANNEL_STREAM_PATH, &()) + self.fetch_stream("channel/stream", &()) } pub fn network_stream(&self) -> Result<String, Error> { - self.fetch_stream(ZOTAPI_NETWORK_STREAM_PATH, &()) + self.fetch_stream("network/stream", &()) } pub fn group(&self) -> GroupFetcher { @@ -94,12 +86,14 @@ impl Client { where T: Serialize + std::fmt::Debug, { - let r = self.base_url.clone() + path; + let mut r = self.base_url.clone() + .join(path).unwrap(); + if let Ok(a) = serde_qs::to_string(dbg!(args)) { - r + "?" + &a - } else { - r + r.set_query(Some(&a)); } + + r.to_string() } pub fn fetch_stream<T>(&self, path: &str, args: &T) -> Result<String, Error> |