aboutsummaryrefslogtreecommitdiffstats
path: root/src/xchan.rs
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2023-03-29 13:08:09 +0200
committerHarald Eilertsen <haraldei@anduin.net>2023-03-29 13:08:09 +0200
commit8100cff32bf0c87dc19fba78f3faac8090f863da (patch)
tree7c6315dbb83162b8d91aae53b920c2e7f20a4268 /src/xchan.rs
parenta4b9520f430ce188ba9165dcabd3320763ff2e16 (diff)
downloadrust-zotapi-8100cff32bf0c87dc19fba78f3faac8090f863da.tar.gz
rust-zotapi-8100cff32bf0c87dc19fba78f3faac8090f863da.tar.bz2
rust-zotapi-8100cff32bf0c87dc19fba78f3faac8090f863da.zip
Another reqrite...
Third time must be where it sits I hope. I felt the API was getting a bit too distracted by unnecessary constructs and abstractions, so I'm trying to simplify it by making it more straight forward. The idea now is to have one main API class (ZotApi), and all the various remote API's as public methods on this basic class. Iow, the ZotApi class is mainly based on the existing `Client` class, which is then being phased out. And instead of having each API tied to the data type they return, I'm just adding methods that will return the respective data types. This should reduce coupling between the returned data, and the API calls themselves.
Diffstat (limited to 'src/xchan.rs')
-rw-r--r--src/xchan.rs54
1 files changed, 1 insertions, 53 deletions
diff --git a/src/xchan.rs b/src/xchan.rs
index 4544f08..e4f6893 100644
--- a/src/xchan.rs
+++ b/src/xchan.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/>.
-use crate::{client::Client, error::Error};
+use crate::{error::Error};
use serde::Deserialize;
use std::convert::TryFrom;
@@ -103,12 +103,6 @@ pub struct XChan {
pub deleted: u32,
}
-impl XChan {
- pub fn z<'a>() -> XChanRequest<'a> {
- XChanRequest::default()
- }
-}
-
impl<'a> TryFrom<&'a str> for XChan {
type Error = Error;
@@ -116,49 +110,3 @@ impl<'a> TryFrom<&'a str> for XChan {
Ok(serde_json::from_str(s)?)
}
}
-
-enum XChanRequestSelector<'a> {
- Address(&'a str),
- Hash(&'a str),
- GUID(&'a str),
-}
-
-#[derive(Default)]
-pub struct XChanRequest<'a> {
- data: Option<XChanRequestSelector<'a>>,
-}
-
-impl<'a> XChanRequest<'a> {
- pub fn by_address(&mut self, addr: &'a str) -> &mut XChanRequest<'a> {
- self.data = Some(XChanRequestSelector::Address(addr));
- self
- }
-
- pub fn by_hash(&mut self, hash: &'a str) -> &mut XChanRequest<'a> {
- self.data = Some(XChanRequestSelector::Hash(hash));
- self
- }
-
- pub fn by_guid(&mut self, guid: &'a str) -> &mut XChanRequest<'a> {
- self.data = Some(XChanRequestSelector::GUID(guid));
- self
- }
-
- pub async fn fetch_raw(&self, client: &Client) -> Result<String, 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(req.send().await?.text().await?)
- }
-
- pub async fn fetch(&self, client: &Client) -> Result<XChan, Error> {
- Ok(XChan::try_from(self.fetch_raw(&client).await?.as_str())?)
- }
-}