aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/zot/xchan.rs8
-rw-r--r--src/lib.rs2
-rw-r--r--src/xchan.rs60
-rw-r--r--tests/zotapi.rs56
4 files changed, 105 insertions, 21 deletions
diff --git a/examples/zot/xchan.rs b/examples/zot/xchan.rs
index 2995d0b..088d49c 100644
--- a/examples/zot/xchan.rs
+++ b/examples/zot/xchan.rs
@@ -25,14 +25,14 @@ pub enum Type {
pub fn fetch(client: &zotapi::Client, _raw: bool, t: Type, id: &str) {
let res = match t {
- 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),
+ Type::Addr => zotapi::XChan::z().by_address(&id).fetch(&client),
+ Type::Hash => zotapi::XChan::z().by_hash(&id).fetch(&client),
+ Type::GUID => zotapi::XChan::z().by_guid(&id).fetch(&client),
};
match res {
Ok(payload) => {
- println!("{}", payload);
+ println!("{:?}", payload);
}
Err(e) => {
println!("{:?}", e);
diff --git a/src/lib.rs b/src/lib.rs
index c363c8e..357591d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -32,7 +32,7 @@ pub use error::Error;
pub use group::{group, group_members};
pub use item::item;
pub use network_stream::network_stream;
-pub use xchan::xchan;
+pub use xchan::XChan;
#[cfg(test)]
mod tests {
diff --git a/src/xchan.rs b/src/xchan.rs
index 1a5fadc..994f4b0 100644
--- a/src/xchan.rs
+++ b/src/xchan.rs
@@ -15,25 +15,55 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::{client::Client, error::Error};
-//use serde::{Serialize, Serializer};
-use serde::Serialize;
+use serde::Deserialize;
+
+#[derive(Debug, Default, Deserialize)]
+pub struct XChan {
+ hash: String,
+ guid: String,
+ guid_sig: String,
+ pubkey: String,
+ photo_mimetype: String,
+ photo_l: String,
+ photo_m: String,
+ photo_s: String,
+ addr: String,
+ url: String,
+ connurl: String,
+ follow: String,
+ connpage: String,
+ name: String,
+ network: String,
+ instance_url: String,
+ flags: u32,
+ photo_date: String,
+ name_date: String,
+ hidden: u32,
+ orphan: u32,
+ censored: u32,
+ selfcensored: u32,
+ system: u32,
+ pubforum: u32,
+ deleted: u32,
+}
+
+impl XChan {
+ pub fn z<'a>() -> XChanRequest<'a> {
+ XChanRequest::default()
+ }
+}
-#[derive(Debug, Serialize)]
-#[serde(rename_all = "lowercase")]
enum XChanRequestSelector<'a> {
Address(&'a str),
Hash(&'a str),
GUID(&'a str),
}
+#[derive(Default)]
pub struct XChanRequest<'a> {
data: Option<XChanRequestSelector<'a>>,
}
-pub fn xchan<'a>() -> XChanRequest<'a> {
- XChanRequest { data: None }
-}
-
impl<'a> XChanRequest<'a> {
pub fn by_address(&mut self, addr: &'a str) -> &mut XChanRequest<'a> {
self.data = Some(XChanRequestSelector::Address(addr));
@@ -50,7 +80,17 @@ impl<'a> XChanRequest<'a> {
self
}
- pub fn fetch(&self, client: &Client) -> Result<String, Error> {
- client.fetch_stream("xchan", &self.data.as_ref().unwrap())
+ pub fn fetch(&self, client: &Client) -> Result<XChan, Error> {
+ let mut req = client.get("xchan");
+
+ if let Some(sel) = &self.data {
+ req = match sel {
+ XChanRequestSelector::Address(s) => req.query(&[("address", s.to_string())]),
+ XChanRequestSelector::Hash(s) => req.query(&[("hash", s.to_string())]),
+ XChanRequestSelector::GUID(s) => req.query(&[("guid", s.to_string())]),
+ };
+ }
+
+ Ok(serde_json::from_str(&dbg!(req.send()?.text()?))?)
}
}
diff --git a/tests/zotapi.rs b/tests/zotapi.rs
index cd127ac..ba279a4 100644
--- a/tests/zotapi.rs
+++ b/tests/zotapi.rs
@@ -196,10 +196,44 @@ fn upload_item_with_two_files() {
m.assert();
}
+const EMPTY_XCHAN: &str = r#"{
+ "hash": "",
+ "guid": "",
+ "guid_sig": "",
+ "pubkey": "",
+ "photo_mimetype": "",
+ "photo_l": "",
+ "photo_m": "",
+ "photo_s": "",
+ "addr": "",
+ "url": "",
+ "connurl": "",
+ "follow": "",
+ "connpage": "",
+ "name": "",
+ "network": "",
+ "instance_url": "",
+ "flags": 0,
+ "photo_date": "",
+ "name_date": "",
+ "hidden": 0,
+ "orphan": 0,
+ "censored": 0,
+ "selfcensored": 0,
+ "system": 0,
+ "pubforum": 0,
+ "deleted": 0
+}"#;
+
#[test]
fn fetch_xchan_by_address() {
- let m = default_mock("GET", "/api/z/1.0/xchan?address=test%40test.com");
- let _res = zotapi::xchan()
+ let m = mock_with_authorization("GET", "/api/z/1.0/xchan?address=test%40test.com")
+ .with_status(200)
+ .with_header("content-type", "application/json")
+ .with_body(&EMPTY_XCHAN)
+ .create();
+
+ let _res = zotapi::XChan::z()
.by_address("test@test.com")
.fetch(&client())
.unwrap();
@@ -209,8 +243,13 @@ fn fetch_xchan_by_address() {
#[test]
fn fetch_xchan_by_hash() {
- let m = default_mock("GET", "/api/z/1.0/xchan?hash=baffebaff");
- let _res = zotapi::xchan()
+ let m = mock_with_authorization("GET", "/api/z/1.0/xchan?hash=baffebaff")
+ .with_status(200)
+ .with_header("content-type", "application/json")
+ .with_body(&EMPTY_XCHAN)
+ .create();
+
+ let _res = zotapi::XChan::z()
.by_hash("baffebaff")
.fetch(&client())
.unwrap();
@@ -220,8 +259,13 @@ fn fetch_xchan_by_hash() {
#[test]
fn fetch_xchan_by_guid() {
- let m = default_mock("GET", "/api/z/1.0/xchan?guid=baffebaff-baff-baff");
- let _res = zotapi::xchan()
+ let m = mock_with_authorization("GET", "/api/z/1.0/xchan?guid=baffebaff-baff-baff")
+ .with_status(200)
+ .with_header("content-type", "application/json")
+ .with_body(&EMPTY_XCHAN)
+ .create();
+
+ let _res = zotapi::XChan::z()
.by_guid("baffebaff-baff-baff")
.fetch(&client())
.unwrap();