From 5f96d9981a6d041f5c8a464cda10a0dc371060f8 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Fri, 3 Jan 2020 20:00:55 +0100 Subject: Use Url crate and move paths to submodules. --- Cargo.toml | 1 + src/abconfig.rs | 4 ++-- src/abook.rs | 5 +++-- src/client.rs | 30 ++++++++++++------------------ src/group.rs | 9 +++------ src/item.rs | 6 +++--- src/xchan.rs | 4 ++-- 7 files changed, 26 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8d63325..d6d0c43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ edition = "2018" reqwest = "0.9.1" serde = { version = "1.0", features = ["derive"] } serde_qs = "0.5.2" +url = "2.1" [dev-dependencies] base64 = "0.11.0" 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 . use crate::{ - client::{self, Client}, + client::Client, error::Error, }; @@ -29,6 +29,6 @@ impl<'a> ABConfigFetcher<'a> { } pub fn fetch(&self) -> Result { - 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 . 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 { - 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 { - self.fetch_stream(ZOTAPI_CHANNEL_STREAM_PATH, &()) + self.fetch_stream("channel/stream", &()) } pub fn network_stream(&self) -> Result { - 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(&self, path: &str, args: &T) -> Result 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 . 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 { - 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 { - 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 . 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 . 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 { self.client - .fetch_stream(client::ZOTAPI_XCHAN_PATH, &self.data.as_ref().unwrap()) + .fetch_stream("xchan", &self.data.as_ref().unwrap()) } } -- cgit v1.2.3