aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2020-01-03 23:32:44 +0100
committerHarald Eilertsen <haraldei@anduin.net>2020-01-03 23:32:44 +0100
commit0c76f0c9727a512475e29b5d099b5b7188f72052 (patch)
treed1c2b59458c62116447fa2d885113afe6a5378da
parent5f96d9981a6d041f5c8a464cda10a0dc371060f8 (diff)
downloadrust-zotapi-0c76f0c9727a512475e29b5d099b5b7188f72052.tar.gz
rust-zotapi-0c76f0c9727a512475e29b5d099b5b7188f72052.tar.bz2
rust-zotapi-0c76f0c9727a512475e29b5d099b5b7188f72052.zip
Move Client out of the api objects.
Also make constructor functions in the zotapi namespace.
-rw-r--r--examples/zot/abconfig.rs2
-rw-r--r--examples/zot/abook.rs2
-rw-r--r--examples/zot/item.rs4
-rw-r--r--examples/zot/xchan.rs6
-rw-r--r--examples/zotcli.rs12
-rw-r--r--src/abconfig.rs16
-rw-r--r--src/abook.rs16
-rw-r--r--src/client.rs33
-rw-r--r--src/group.rs35
-rw-r--r--src/item.rs29
-rw-r--r--src/lib.rs5
-rw-r--r--src/xchan.rs22
-rw-r--r--tests/zotapi.rs60
13 files changed, 105 insertions, 137 deletions
diff --git a/examples/zot/abconfig.rs b/examples/zot/abconfig.rs
index 54d555c..2362408 100644
--- a/examples/zot/abconfig.rs
+++ b/examples/zot/abconfig.rs
@@ -18,7 +18,7 @@
use zotapi;
pub fn fetch(client: &zotapi::Client) {
- match client.abconfig().fetch() {
+ match zotapi::abconfig().fetch(&client) {
Ok(payload) => {
println!("{}", payload);
}
diff --git a/examples/zot/abook.rs b/examples/zot/abook.rs
index a16f68c..b710fa7 100644
--- a/examples/zot/abook.rs
+++ b/examples/zot/abook.rs
@@ -16,7 +16,7 @@
*/
pub fn fetch(client: &zotapi::Client, raw: bool) {
- match client.abook().fetch() {
+ match zotapi::abook().fetch(&client) {
Ok(payload) => {
if raw {
println!("{}", &payload);
diff --git a/examples/zot/item.rs b/examples/zot/item.rs
index dec6c8d..2e07561 100644
--- a/examples/zot/item.rs
+++ b/examples/zot/item.rs
@@ -19,7 +19,7 @@ use clap::ArgMatches;
use zotapi;
pub fn post(client: &zotapi::Client, args: &ArgMatches) {
- let mut msg = client.item();
+ let mut msg = zotapi::item();
msg.body(args.value_of("BODY").unwrap());
if let Some(title) = dbg!(args.value_of("TITLE")) {
@@ -36,7 +36,7 @@ pub fn post(client: &zotapi::Client, args: &ArgMatches) {
}
}
- match msg.create() {
+ match msg.create(&client) {
Ok(payload) => {
println!("Raw payload: {}", payload);
}
diff --git a/examples/zot/xchan.rs b/examples/zot/xchan.rs
index d2d73d0..2995d0b 100644
--- a/examples/zot/xchan.rs
+++ b/examples/zot/xchan.rs
@@ -25,9 +25,9 @@ pub enum Type {
pub fn fetch(client: &zotapi::Client, _raw: bool, t: Type, id: &str) {
let res = match t {
- Type::Addr => client.xchan().by_address(&id).fetch(),
- Type::Hash => client.xchan().by_hash(&id).fetch(),
- Type::GUID => client.xchan().by_guid(&id).fetch(),
+ Type::Addr => zotapi::xchan().by_address(&id).fetch(&client),
+ Type::Hash => zotapi::xchan().by_hash(&id).fetch(&client),
+ Type::GUID => zotapi::xchan().by_guid(&id).fetch(&client),
};
match res {
diff --git a/examples/zotcli.rs b/examples/zotcli.rs
index 14bb9ad..02f51e6 100644
--- a/examples/zotcli.rs
+++ b/examples/zotcli.rs
@@ -94,10 +94,9 @@ fn main() {
}
("group", Some(m)) => {
if let Some(id) = m.value_of("ID") {
- let res = client
- .group_members()
+ let res = zotapi::group_members()
.by_group_id(u64::from_str(id).unwrap())
- .fetch()
+ .fetch(&client)
.unwrap();
if m.is_present("raw") {
@@ -106,7 +105,10 @@ fn main() {
zot::group::list_members(&res);
}
} else if let Some(gname) = m.value_of("GNAME") {
- let res = client.group_members().by_group_name(gname).fetch().unwrap();
+ let res = zotapi::group_members()
+ .by_group_name(gname)
+ .fetch(&client)
+ .unwrap();
if m.is_present("raw") {
println!("{}", res);
@@ -114,7 +116,7 @@ fn main() {
zot::group::list_members(&res);
}
} else {
- let res = client.group().fetch().unwrap();
+ let res = zotapi::group().fetch(&client).unwrap();
if m.is_present("raw") {
println!("{}", res);
diff --git a/src/abconfig.rs b/src/abconfig.rs
index 6f9adbe..36b3427 100644
--- a/src/abconfig.rs
+++ b/src/abconfig.rs
@@ -19,16 +19,14 @@ use crate::{
error::Error,
};
-pub struct ABConfigFetcher<'a> {
- client: &'a Client,
-}
+pub struct ABConfig;
-impl<'a> ABConfigFetcher<'a> {
- pub fn new(client: &'a Client) -> ABConfigFetcher<'a> {
- ABConfigFetcher { client }
- }
+pub fn abconfig() -> ABConfig {
+ ABConfig {}
+}
- pub fn fetch(&self) -> Result<String, Error> {
- self.client.fetch_stream("abconfig", &())
+impl ABConfig {
+ pub fn fetch(&self, client: &Client) -> Result<String, Error> {
+ client.fetch_stream("abconfig", &())
}
}
diff --git a/src/abook.rs b/src/abook.rs
index 5f34ff5..40ef5a4 100644
--- a/src/abook.rs
+++ b/src/abook.rs
@@ -20,16 +20,14 @@ use crate::{
};
-pub struct AbookFetcher<'a> {
- client: &'a Client,
-}
+pub struct Abook;
-impl<'a> AbookFetcher<'a> {
- pub fn new(client: &'a Client) -> AbookFetcher<'a> {
- AbookFetcher { client }
- }
+pub fn abook() -> Abook {
+ Abook {}
+}
- pub fn fetch(&self) -> Result<String, Error> {
- self.client.fetch_stream("abook", &())
+impl Abook {
+ pub fn fetch(&self, client: &Client) -> Result<String, Error> {
+ client.fetch_stream("abook", &())
}
}
diff --git a/src/client.rs b/src/client.rs
index 4c20854..dc6cf43 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -14,14 +14,7 @@
// You should have received a copy of the GNU General Public License
// 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,
- xchan::XChanFetcher,
-};
+use crate::error::Error;
use reqwest::{
self,
header::{ACCEPT, CONTENT_TYPE},
@@ -50,14 +43,6 @@ impl Client {
}
}
- pub fn abconfig(&self) -> ABConfigFetcher {
- ABConfigFetcher::new(self)
- }
-
- pub fn abook(&self) -> AbookFetcher {
- AbookFetcher::new(self)
- }
-
pub fn channel_stream(&self) -> Result<String, Error> {
self.fetch_stream("channel/stream", &())
}
@@ -66,22 +51,6 @@ impl Client {
self.fetch_stream("network/stream", &())
}
- pub fn group(&self) -> GroupFetcher {
- GroupFetcher::new(self)
- }
-
- pub fn group_members(&self) -> GroupMembersFetcher {
- GroupMembersFetcher::new(self)
- }
-
- pub fn item(&self) -> ItemBuilder {
- ItemBuilder::new(self)
- }
-
- pub fn xchan(&self) -> XChanFetcher {
- XChanFetcher::new(self)
- }
-
fn url<T>(&self, path: &str, args: &T) -> String
where
T: Serialize + std::fmt::Debug,
diff --git a/src/group.rs b/src/group.rs
index 9135425..e17addb 100644
--- a/src/group.rs
+++ b/src/group.rs
@@ -20,17 +20,15 @@ use crate::{
};
use serde::Serialize;
-pub struct GroupFetcher<'a> {
- client: &'a Client,
-}
+pub struct Group;
-impl<'a> GroupFetcher<'a> {
- pub fn new(client: &'a Client) -> GroupFetcher<'a> {
- GroupFetcher { client }
- }
+pub fn group() -> Group {
+ Group { }
+}
- pub fn fetch(&self) -> Result<String, Error> {
- self.client.fetch_stream("group", &())
+impl Group {
+ pub fn fetch(&self, client: &Client) -> Result<String, Error> {
+ client.fetch_stream("group", &())
}
}
@@ -43,27 +41,26 @@ enum GroupSelector<'a> {
Name(&'a str),
}
-pub struct GroupMembersFetcher<'a> {
- client: &'a Client,
+pub struct GroupMembers<'a> {
id: Option<GroupSelector<'a>>,
}
-impl<'a> GroupMembersFetcher<'a> {
- pub fn new(client: &'a Client) -> GroupMembersFetcher<'a> {
- GroupMembersFetcher { client, id: None }
- }
+pub fn group_members<'a>() -> GroupMembers<'a> {
+ GroupMembers { id: None }
+}
- pub fn by_group_id(&mut self, id: u64) -> &GroupMembersFetcher<'a> {
+impl<'a> GroupMembers<'a> {
+ pub fn by_group_id(&mut self, id: u64) -> &GroupMembers<'a> {
self.id = Some(GroupSelector::Id(id));
self
}
- pub fn by_group_name(&mut self, name: &'a str) -> &GroupMembersFetcher<'a> {
+ pub fn by_group_name(&mut self, name: &'a str) -> &GroupMembers<'a> {
self.id = Some(GroupSelector::Name(name));
self
}
- pub fn fetch(&self) -> Result<String, Error> {
- self.client.fetch_stream("group_members", &self.id.as_ref().unwrap())
+ pub fn fetch(&self, client: &Client) -> Result<String, Error> {
+ client.fetch_stream("group_members", &self.id.as_ref().unwrap())
}
}
diff --git a/src/item.rs b/src/item.rs
index 532ac16..52492aa 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -75,29 +75,27 @@ fn convert_itemdata_list_with_one_member_to_a_string() {
///
/// ```no_run
/// let client = zotapi::Client::new("https://myhub.com", "mychannel", "mypw");
-/// let new_post = client.item()
+/// let new_post = zotapi::item()
/// .title("A title")
/// .body("The body of the post")
/// .file("/my/photo.jpg")
-/// .create()?;
+/// .create(&client)?;
/// # Ok::<(), zotapi::Error>(())
/// ```
#[derive(Debug)]
pub struct ItemBuilder<'a> {
- client: &'a Client,
data: BTreeMap<&'a str, ItemData<'a>>,
files: Vec<&'a str>,
}
-impl<'a> ItemBuilder<'a> {
- pub fn new(client: &'a Client) -> ItemBuilder<'a> {
- ItemBuilder {
- client: client,
- data: BTreeMap::new(),
- files: vec![],
- }
+pub fn item<'a>() -> ItemBuilder<'a> {
+ ItemBuilder {
+ data: BTreeMap::new(),
+ files: vec![],
}
+}
+impl<'a> ItemBuilder<'a> {
/// Add a title to the post
pub fn title(&mut self, text: &'a str) -> &mut ItemBuilder<'a> {
self.data.insert("title", ItemData::Value(text));
@@ -128,22 +126,19 @@ impl<'a> ItemBuilder<'a> {
}
/// Create the item by poting it to the server
- pub fn create(&self) -> Result<String, Error> {
+ pub fn create(&self, client: &Client) -> Result<String, Error> {
dbg!(self);
if self.files.is_empty() {
- self.client
- .post_data("item/update", &self.data)
+ client.post_data("item/update", &self.data)
} else {
- self.client
- .post_multipart("item/update", &self.data, &self.files)
+ client.post_multipart("item/update", &self.data, &self.files)
}
}
}
#[test]
fn add_group_to_list_of_groups_allowed() {
- let client = Client::new("https://test.com", "test", "test1234");
- let mut item = ItemBuilder::new(&client);
+ let mut item = item();
item.group_allow("test");
match item.data.get("groups_allow") {
Some(ItemData::List(v)) => assert_eq!(v.len(), 1),
diff --git a/src/lib.rs b/src/lib.rs
index eb5d897..c4a39c8 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -22,8 +22,13 @@ mod group;
mod item;
mod xchan;
+pub use abconfig::abconfig;
+pub use abook::abook;
pub use client::Client;
pub use error::Error;
+pub use group::{group, group_members};
+pub use item::item;
+pub use xchan::xchan;
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 5d1c17b..20bbe8d 100644
--- a/src/xchan.rs
+++ b/src/xchan.rs
@@ -29,33 +29,31 @@ enum XChanSelector<'a> {
GUID(&'a str),
}
-pub struct XChanFetcher<'a> {
- client: &'a Client,
+pub struct XChan<'a> {
data: Option<XChanSelector<'a>>,
}
-impl<'a> XChanFetcher<'a> {
- pub fn new(client: &'a Client) -> XChanFetcher<'a> {
- XChanFetcher { client, data: None }
- }
+pub fn xchan<'a>() -> XChan<'a> {
+ XChan { data: None }
+}
- pub fn by_address(&mut self, addr: &'a str) -> &mut XChanFetcher<'a> {
+impl<'a> XChan<'a> {
+ pub fn by_address(&mut self, addr: &'a str) -> &mut XChan<'a> {
self.data = Some(XChanSelector::Address(addr));
self
}
- pub fn by_hash(&mut self, hash: &'a str) -> &mut XChanFetcher<'a> {
+ pub fn by_hash(&mut self, hash: &'a str) -> &mut XChan<'a> {
self.data = Some(XChanSelector::Hash(hash));
self
}
- pub fn by_guid(&mut self, guid: &'a str) -> &mut XChanFetcher<'a> {
+ pub fn by_guid(&mut self, guid: &'a str) -> &mut XChan<'a> {
self.data = Some(XChanSelector::GUID(guid));
self
}
- pub fn fetch(&self) -> Result<String, Error> {
- self.client
- .fetch_stream("xchan", &self.data.as_ref().unwrap())
+ pub fn fetch(&self, client: &Client) -> Result<String, Error> {
+ client.fetch_stream("xchan", &self.data.as_ref().unwrap())
}
}
diff --git a/tests/zotapi.rs b/tests/zotapi.rs
index 66fe15e..4c0a207 100644
--- a/tests/zotapi.rs
+++ b/tests/zotapi.rs
@@ -74,7 +74,9 @@ fn create_new_post() {
.with_body("{}")
.create();
- let _res = client().item().body("This is a test").create();
+ let _res = zotapi::item()
+ .body("This is a test")
+ .create(&client());
m.assert();
}
@@ -91,11 +93,10 @@ fn create_new_post_with_title() {
.with_body("{}")
.create();
- let _res = client()
- .item()
+ let _res = zotapi::item()
.title("A title")
.body("This is a test")
- .create();
+ .create(&client());
m.assert();
}
@@ -118,11 +119,10 @@ fn create_new_post_limited_to_one_privacy_group() {
.with_body("{}")
.create();
- let _res = client()
- .item()
+ let _res = zotapi::item()
.body("This is a test")
.group_allow("grouphash")
- .create();
+ .create(&client());
//m.assert();
}
@@ -152,12 +152,11 @@ fn upload_item_with_media_file() {
.with_body("{}")
.create();
- let _res = client()
- .item()
+ let _res = zotapi::item()
.title("A title")
.body("This is a test")
.file("tests/fixtures/testfile.txt")
- .create();
+ .create(&client());
m.assert();
}
@@ -191,13 +190,12 @@ fn upload_item_with_two_files() {
.with_body("{}")
.create();
- let _res = client()
- .item()
+ let _res = zotapi::item()
.title("A title")
.body("This is a test")
.file("tests/fixtures/testfile.txt")
.file("tests/fixtures/testfile.txt")
- .create();
+ .create(&client());
m.assert();
}
@@ -205,50 +203,54 @@ fn upload_item_with_two_files() {
#[test]
fn fetch_xchan_by_address() {
let m = default_mock("GET", "/api/z/1.0/xchan?address=test%40test.com");
- let _res = client()
- .xchan()
+ let _res = zotapi::xchan()
.by_address("test@test.com")
- .fetch()
+ .fetch(&client())
.unwrap();
+
m.assert();
}
#[test]
fn fetch_xchan_by_hash() {
let m = default_mock("GET", "/api/z/1.0/xchan?hash=baffebaff");
- let _res = client().xchan().by_hash("baffebaff").fetch().unwrap();
+ let _res = zotapi::xchan()
+ .by_hash("baffebaff")
+ .fetch(&client())
+ .unwrap();
+
m.assert();
}
#[test]
fn fetch_xchan_by_guid() {
let m = default_mock("GET", "/api/z/1.0/xchan?guid=baffebaff-baff-baff");
- let _res = client()
- .xchan()
+ let _res = zotapi::xchan()
.by_guid("baffebaff-baff-baff")
- .fetch()
+ .fetch(&client())
.unwrap();
+
m.assert();
}
#[test]
fn fetch_connections() {
let m = default_mock("GET", "/api/z/1.0/abook");
- let _res = client().abook().fetch().unwrap();
+ let _res = zotapi::abook().fetch(&client()).unwrap();
m.assert();
}
#[test]
fn fetch_abconfig() {
let m = default_mock("GET", "/api/z/1.0/abconfig");
- let _res = client().abconfig().fetch().unwrap();
+ let _res = zotapi::abconfig().fetch(&client()).unwrap();
m.assert();
}
#[test]
fn fetch_privacy_groups() {
let m = default_mock("GET", "/api/z/1.0/group");
- let _res = client().group().fetch().unwrap();
+ let _res = zotapi::group().fetch(&client()).unwrap();
m.assert();
}
@@ -264,7 +266,11 @@ fn fetch_members_of_group_by_group_id() {
.with_body("{}")
.create();
- let _res = client().group_members().by_group_id(42).fetch().unwrap();
+ let _res = zotapi::group_members()
+ .by_group_id(42)
+ .fetch(&client())
+ .unwrap();
+
m.assert();
}
@@ -280,10 +286,10 @@ fn fetch_members_of_group_by_group_name() {
.with_body("{}")
.create();
- let _res = client()
- .group_members()
+ let _res = zotapi::group_members()
.by_group_name("Friends of pain")
- .fetch()
+ .fetch(&client())
.unwrap();
+
m.assert();
}