aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2020-02-13 14:45:41 +0100
committerHarald Eilertsen <haraldei@anduin.net>2020-02-13 14:45:41 +0100
commitd99083c47ddd31288ed1b688002388f6e68c5e04 (patch)
tree08e90a0da26512fe19470c8bd7ff0d81d3dffd4c
parentda461fc867b7c9211116fe7d0c23afbb96f6aaa4 (diff)
downloadrust-zotapi-d99083c47ddd31288ed1b688002388f6e68c5e04.tar.gz
rust-zotapi-d99083c47ddd31288ed1b688002388f6e68c5e04.tar.bz2
rust-zotapi-d99083c47ddd31288ed1b688002388f6e68c5e04.zip
Refactor how ABConfigs are fetched.
Introduce a `z()` menber function that creates the request object that we use for further configuring the requset. This eliminates the need to two fetch functions, and is meant to provide a consistent way of doing these requests.
-rw-r--r--examples/zot/abconfig.rs2
-rw-r--r--src/abconfig.rs36
-rw-r--r--src/lib.rs3
-rw-r--r--tests/zotapi.rs4
4 files changed, 25 insertions, 20 deletions
diff --git a/examples/zot/abconfig.rs b/examples/zot/abconfig.rs
index a6454ac..2e357e4 100644
--- a/examples/zot/abconfig.rs
+++ b/examples/zot/abconfig.rs
@@ -18,7 +18,7 @@
use zotapi;
pub fn fetch(client: &zotapi::Client) {
- match zotapi::abconfig::fetch(&client) {
+ match zotapi::ABConfig::z().fetch(&client) {
Ok(v) => {
println!("Id: Chan: Cat: Key: Val: xchan:");
for entry in v {
diff --git a/src/abconfig.rs b/src/abconfig.rs
index 723747c..6348030 100644
--- a/src/abconfig.rs
+++ b/src/abconfig.rs
@@ -20,7 +20,7 @@ use serde::Deserialize;
/// A struct for storing a key value pair with a category in
/// relation to a contact. Typically used to store permissions
/// a given contact has on a given channel.
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Default, Deserialize)]
pub struct ABConfig {
/// Database ID for this entry
pub id: u32,
@@ -41,30 +41,34 @@ pub struct ABConfig {
pub v: String,
}
-/// Fetch ABConfig for all contacts
-pub fn fetch(client: &Client) -> Result<Vec<ABConfig>, Error> {
- let payload = client.fetch_stream("abconfig", &())?;
- Ok(serde_json::from_str(&payload)?)
+impl ABConfig {
+ pub fn z() -> ABConfigRequest {
+ ABConfigRequest::default()
+ }
}
#[derive(Default)]
pub struct ABConfigRequest {
- abook_id: u32,
+ abook_id: Option<u32>,
}
impl ABConfigRequest {
+ /// Limit to ABConfigs for a given contact
+ ///
+ /// `abook` is the abook id of the given contact
+ pub fn with_abook_id(mut self, abook: u32) -> ABConfigRequest {
+ self.abook_id = Some(abook);
+ self
+ }
+
pub fn fetch(&self, client: &Client) -> Result<Vec<ABConfig>, Error> {
- let mut res = client.get("abconfig")
- .query(&[("abook_id", self.abook_id.to_string())])
- .send()?;
+ let mut req = client.get("abconfig");
- Ok(serde_json::from_str(&res.text()?)?)
+ if let Some(id) = self.abook_id {
+ req = req.query(&[("abook_id", id.to_string())]);
+ }
+
+ Ok(serde_json::from_str(&req.send()?.text()?)?)
}
}
-/// Fetch ABConfig for a given contact
-///
-/// `abook` is the abook id of the given contact
-pub fn with_abook_id(abook: u32) -> ABConfigRequest {
- ABConfigRequest { abook_id: abook }
-}
diff --git a/src/lib.rs b/src/lib.rs
index 7a17269..1be4c92 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -14,7 +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/>.
-pub mod abconfig;
+mod abconfig;
mod abook;
mod channel_stream;
mod client;
@@ -24,6 +24,7 @@ mod item;
mod network_stream;
mod xchan;
+pub use abconfig::ABConfig;
pub use abook::abook;
pub use channel_stream::channel_stream;
pub use client::*;
diff --git a/tests/zotapi.rs b/tests/zotapi.rs
index 9dbf5cf..b5c1652 100644
--- a/tests/zotapi.rs
+++ b/tests/zotapi.rs
@@ -266,7 +266,7 @@ fn fetch_abconfig() {
.with_body(&data)
.create();
- let res = zotapi::abconfig::fetch(&client()).unwrap();
+ let res = zotapi::ABConfig::z().fetch(&client()).unwrap();
m.assert();
assert_eq!(res.len(), 2);
@@ -282,7 +282,7 @@ fn fetch_abconfig_for_specific_contact() {
.with_body("[]")
.create();
- zotapi::abconfig::with_abook_id(42).fetch(&client()).unwrap();
+ zotapi::ABConfig::z().with_abook_id(42).fetch(&client()).unwrap();
m.assert();
}