aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2020-01-02 21:10:55 +0100
committerHarald Eilertsen <haraldei@anduin.net>2020-01-02 21:10:55 +0100
commit16a56f12cd2e66e10f8bb5e2c4026d3a7148e680 (patch)
tree913bd32a563b32302c2e14304460a3cbc9a88a39 /src
parent7f306445cb78d52f598eca1810dc2c5002e34a4a (diff)
downloadrust-zotapi-16a56f12cd2e66e10f8bb5e2c4026d3a7148e680.tar.gz
rust-zotapi-16a56f12cd2e66e10f8bb5e2c4026d3a7148e680.tar.bz2
rust-zotapi-16a56f12cd2e66e10f8bb5e2c4026d3a7148e680.zip
Cargo fmt
Diffstat (limited to 'src')
-rw-r--r--src/abconfig.rs4
-rw-r--r--src/abook.rs4
-rw-r--r--src/client.rs61
-rw-r--r--src/group.rs4
-rw-r--r--src/item.rs18
-rw-r--r--src/lib.rs3
-rw-r--r--src/xchan.rs8
7 files changed, 55 insertions, 47 deletions
diff --git a/src/abconfig.rs b/src/abconfig.rs
index 78eec4b..f1622c7 100644
--- a/src/abconfig.rs
+++ b/src/abconfig.rs
@@ -25,9 +25,7 @@ pub struct ABConfigFetcher<'a> {
impl<'a> ABConfigFetcher<'a> {
pub fn new(client: &'a Client) -> ABConfigFetcher<'a> {
- ABConfigFetcher {
- client,
- }
+ ABConfigFetcher { client }
}
pub fn fetch(&self) -> Result<String, Error> {
diff --git a/src/abook.rs b/src/abook.rs
index f7469e3..bcedf81 100644
--- a/src/abook.rs
+++ b/src/abook.rs
@@ -25,9 +25,7 @@ pub struct AbookFetcher<'a> {
impl<'a> AbookFetcher<'a> {
pub fn new(client: &'a Client) -> AbookFetcher<'a> {
- AbookFetcher {
- client,
- }
+ AbookFetcher { client }
}
pub fn fetch(&self) -> Result<String, Error> {
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)
}
}
}
-
diff --git a/src/group.rs b/src/group.rs
index 43c4d65..98d83d2 100644
--- a/src/group.rs
+++ b/src/group.rs
@@ -34,7 +34,6 @@ impl<'a> GroupFetcher<'a> {
}
}
-
#[derive(Debug, Serialize)]
enum GroupSelector<'a> {
#[serde(rename(serialize = "group_id"))]
@@ -67,6 +66,7 @@ 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.id.as_ref().unwrap(),
+ )
}
}
diff --git a/src/item.rs b/src/item.rs
index 5507577..3d9ad1e 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -21,7 +21,6 @@ use crate::{
use serde::Serialize;
use std::collections::BTreeMap;
-
/// Data type for values that an Item can hold.
#[derive(Debug, Serialize)]
#[serde(untagged)]
@@ -85,8 +84,8 @@ fn convert_itemdata_list_with_one_member_to_a_string() {
/// ```
#[derive(Debug)]
pub struct ItemBuilder<'a> {
- client : &'a Client,
- data : BTreeMap<&'a str, ItemData<'a>>,
+ client: &'a Client,
+ data: BTreeMap<&'a str, ItemData<'a>>,
files: Vec<&'a str>,
}
@@ -119,7 +118,9 @@ impl<'a> ItemBuilder<'a> {
/// Set groups allowed to access item
pub fn group_allow(&mut self, group: &'a str) -> &mut ItemBuilder<'a> {
- let groups_allow = self.data.entry("groups_allow")
+ let groups_allow = self
+ .data
+ .entry("groups_allow")
.or_insert(ItemData::List(Vec::<&str>::new()));
groups_allow.push(group);
@@ -130,10 +131,11 @@ impl<'a> ItemBuilder<'a> {
pub fn create(&self) -> Result<String, Error> {
dbg!(self);
if self.files.is_empty() {
- self.client.post_data(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data)
- }
- else {
- self.client.post_multipart(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data, &self.files)
+ self.client
+ .post_data(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data)
+ } else {
+ self.client
+ .post_multipart(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data, &self.files)
}
}
}
diff --git a/src/lib.rs b/src/lib.rs
index e9b6bf9..eb5d897 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -25,8 +25,7 @@ mod xchan;
pub use client::Client;
pub use error::Error;
-pub fn client(url: &str, user: &str, pw: &str) -> Client
-{
+pub fn client(url: &str, user: &str, pw: &str) -> Client {
Client::new(url, user, pw)
}
diff --git a/src/xchan.rs b/src/xchan.rs
index 68f7622..9b920d5 100644
--- a/src/xchan.rs
+++ b/src/xchan.rs
@@ -36,10 +36,7 @@ pub struct XChanFetcher<'a> {
impl<'a> XChanFetcher<'a> {
pub fn new(client: &'a Client) -> XChanFetcher<'a> {
- XChanFetcher {
- client,
- data: None,
- }
+ XChanFetcher { client, data: None }
}
pub fn by_address(&mut self, addr: &'a str) -> &mut XChanFetcher<'a> {
@@ -58,6 +55,7 @@ impl<'a> XChanFetcher<'a> {
}
pub fn fetch(&self) -> Result<String, Error> {
- self.client.fetch_stream(client::ZOTAPI_XCHAN_PATH, &self.data.as_ref().unwrap())
+ self.client
+ .fetch_stream(client::ZOTAPI_XCHAN_PATH, &self.data.as_ref().unwrap())
}
}