From 16a56f12cd2e66e10f8bb5e2c4026d3a7148e680 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 2 Jan 2020 21:10:55 +0100 Subject: Cargo fmt --- src/client.rs | 61 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 24 deletions(-) (limited to 'src/client.rs') 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 . 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(&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(&self, path: &str, args: &T) -> Result - 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(&self, path: &str, data: &T) -> Result - 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(&self, path: &str, data: &BTreeMap<&str, T>, files: &Vec<&str>) -> Result - where T: ToString, + pub fn post_multipart( + &self, + path: &str, + data: &BTreeMap<&str, T>, + files: &Vec<&str>, + ) -> Result + 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 { let mut body = String::new(); res.read_to_string(&mut body)?; Ok(body) - }, + } _ => { eprintln!("Received unknown status: {:?}", res.status()); Err(Error::Unknown) } } } - -- cgit v1.2.3