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 | |
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')
-rw-r--r-- | src/abconfig.rs | 4 | ||||
-rw-r--r-- | src/abook.rs | 5 | ||||
-rw-r--r-- | src/client.rs | 30 | ||||
-rw-r--r-- | src/group.rs | 9 | ||||
-rw-r--r-- | src/item.rs | 6 | ||||
-rw-r--r-- | src/xchan.rs | 4 |
6 files changed, 25 insertions, 33 deletions
diff --git a/src/abconfig.rs b/src/abconfig.rs index f1622c7..6f9adbe 100644 --- a/src/abconfig.rs +++ b/src/abconfig.rs @@ -15,7 +15,7 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use crate::{ - client::{self, Client}, + client::Client, error::Error, }; @@ -29,6 +29,6 @@ impl<'a> ABConfigFetcher<'a> { } pub fn fetch(&self) -> Result<String, Error> { - self.client.fetch_stream(client::ZOTAPI_ABCONFIG_PATH, &()) + self.client.fetch_stream("abconfig", &()) } } diff --git a/src/abook.rs b/src/abook.rs index bcedf81..5f34ff5 100644 --- a/src/abook.rs +++ b/src/abook.rs @@ -15,10 +15,11 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use crate::{ - client::{self, Client}, + client::Client, error::Error, }; + pub struct AbookFetcher<'a> { client: &'a Client, } @@ -29,6 +30,6 @@ impl<'a> AbookFetcher<'a> { } pub fn fetch(&self) -> Result<String, Error> { - self.client.fetch_stream(client::ZOTAPI_ABOOK_PATH, &()) + self.client.fetch_stream("abook", &()) } } 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> diff --git a/src/group.rs b/src/group.rs index 98d83d2..9135425 100644 --- a/src/group.rs +++ b/src/group.rs @@ -15,7 +15,7 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use crate::{ - client::{self, Client}, + client::Client, error::Error, }; use serde::Serialize; @@ -30,7 +30,7 @@ impl<'a> GroupFetcher<'a> { } pub fn fetch(&self) -> Result<String, Error> { - self.client.fetch_stream(client::ZOTAPI_GROUP_PATH, &()) + self.client.fetch_stream("group", &()) } } @@ -64,9 +64,6 @@ impl<'a> GroupMembersFetcher<'a> { } pub fn fetch(&self) -> Result<String, Error> { - self.client.fetch_stream( - client::ZOTAPI_GROUP_MEMBERS_PATH, - &self.id.as_ref().unwrap(), - ) + self.client.fetch_stream("group_members", &self.id.as_ref().unwrap()) } } diff --git a/src/item.rs b/src/item.rs index 3d9ad1e..532ac16 100644 --- a/src/item.rs +++ b/src/item.rs @@ -15,7 +15,7 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use crate::{ - client::{self, Client}, + client::Client, error::Error, }; use serde::Serialize; @@ -132,10 +132,10 @@ impl<'a> ItemBuilder<'a> { dbg!(self); if self.files.is_empty() { self.client - .post_data(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data) + .post_data("item/update", &self.data) } else { self.client - .post_multipart(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data, &self.files) + .post_multipart("item/update", &self.data, &self.files) } } } diff --git a/src/xchan.rs b/src/xchan.rs index 9b920d5..5d1c17b 100644 --- a/src/xchan.rs +++ b/src/xchan.rs @@ -15,7 +15,7 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use crate::{ - client::{self, Client}, + client::Client, error::Error, }; //use serde::{Serialize, Serializer}; @@ -56,6 +56,6 @@ impl<'a> XChanFetcher<'a> { pub fn fetch(&self) -> Result<String, Error> { self.client - .fetch_stream(client::ZOTAPI_XCHAN_PATH, &self.data.as_ref().unwrap()) + .fetch_stream("xchan", &self.data.as_ref().unwrap()) } } |