aboutsummaryrefslogtreecommitdiffstats
path: root/src/client.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/client.rs')
-rw-r--r--src/client.rs61
1 files changed, 37 insertions, 24 deletions
diff --git a/src/client.rs b/src/client.rs
index 3a20133..ab021fb 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -15,11 +15,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::{
+ abconfig::ABConfigFetcher,
+ abook::AbookFetcher,
error::Error,
group::{GroupFetcher, GroupMembersFetcher},
item::ItemBuilder,
- abook::AbookFetcher,
- abconfig::ABConfigFetcher,
xchan::XChanFetcher,
};
use reqwest::{
@@ -31,14 +31,14 @@ 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";
+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";
#[derive(Debug)]
pub struct Client {
@@ -91,22 +91,25 @@ impl Client {
}
fn url<T>(&self, path: &str, args: &T) -> String
- where T: Serialize + std::fmt::Debug
+ where
+ T: Serialize + std::fmt::Debug,
{
let r = self.base_url.clone() + path;
if let Ok(a) = serde_qs::to_string(dbg!(args)) {
r + "?" + &a
- }
- else {
+ } else {
r
}
}
pub fn fetch_stream<T>(&self, path: &str, args: &T) -> Result<String, Error>
- where T: Serialize + std::fmt::Debug
+ where
+ T: Serialize + std::fmt::Debug,
{
let url = dbg!(self.url(path, args));
- let res = self.inner.get(&url)
+ let res = self
+ .inner
+ .get(&url)
.header(ACCEPT, "application/json")
.basic_auth(self.user.clone(), Some(self.pw.clone()))
.send()?;
@@ -115,22 +118,31 @@ impl Client {
}
pub fn post_data<T>(&self, path: &str, data: &T) -> Result<String, Error>
- where T: Serialize + std::fmt::Debug,
+ where
+ T: Serialize + std::fmt::Debug,
{
let url = dbg!(self.url(path, &()));
- let res = dbg!(self.inner.post(&url)
+ let res = dbg!(self
+ .inner
+ .post(&url)
.header(ACCEPT, "application/json")
.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
.basic_auth(self.user.clone(), Some(self.pw.clone()))
.body(serde_qs::to_string(&data)?))
- //.form(&data))
- .send()?;
+ //.form(&data))
+ .send()?;
handle_result(res)
}
- pub fn post_multipart<T>(&self, path: &str, data: &BTreeMap<&str, T>, files: &Vec<&str>) -> Result<String, Error>
- where T: ToString,
+ pub fn post_multipart<T>(
+ &self,
+ path: &str,
+ data: &BTreeMap<&str, T>,
+ files: &Vec<&str>,
+ ) -> Result<String, Error>
+ where
+ T: ToString,
{
let url = dbg!(self.url(path, &()));
let mut form = reqwest::multipart::Form::new();
@@ -143,7 +155,9 @@ impl Client {
form = form.file("media", f).unwrap();
}
- let res = self.inner.post(&url)
+ let res = self
+ .inner
+ .post(&url)
.basic_auth(self.user.clone(), Some(self.pw.clone()))
.multipart(form)
.send()?;
@@ -162,11 +176,10 @@ fn handle_result(mut res: reqwest::Response) -> Result<String, Error> {
let mut body = String::new();
res.read_to_string(&mut body)?;
Ok(body)
- },
+ }
_ => {
eprintln!("Received unknown status: {:?}", res.status());
Err(Error::Unknown)
}
}
}
-