aboutsummaryrefslogtreecommitdiffstats
path: root/src/client.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/client.rs')
-rw-r--r--src/client.rs30
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>