From 45a95e5048f645caae8d175f5de31a937a5907aa Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Sat, 16 Mar 2019 11:03:33 -0400 Subject: [PATCH] add the beginning of an unauthenticated client --- src/lib.rs | 64 +++++++++++++++++++++++++++++++++++++++++- src/mastodon_client.rs | 26 +++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 098add8..6c1b9e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,7 +118,7 @@ use page::Page; pub use data::Data; pub use errors::{ApiError, Error, Result}; pub use isolang::Language; -pub use mastodon_client::MastodonClient; +pub use mastodon_client::{MastodonClient, MastodonUnauthenticated}; pub use registration::Registration; pub use requests::{ AddFilterRequest, @@ -660,6 +660,68 @@ impl MastodonBuilder { } } +/// Client that can make unauthenticated calls to a mastodon instance +#[derive(Clone, Debug)] +pub struct MastodonUnauth { + client: Client, + http_sender: H, + base: url::Url, +} + +impl MastodonUnauth { + /// Create a new unauthenticated client + pub fn new(base: &str) -> Result> { + let base = if base.starts_with("https://") { + base.to_string() + } else { + format!("https://{}", base) + }; + Ok(MastodonUnauth { + client: Client::new(), + http_sender: HttpSender, + base: url::Url::parse(&base)?, + }) + } +} + +impl MastodonUnauth { + fn route(&self, url: &str) -> Result { + Ok(self.base.join(url)?) + } + + fn send(&self, req: RequestBuilder) -> Result { + Ok(self.http_sender.send(&self.client, req)?) + } +} + +impl MastodonUnauthenticated for MastodonUnauth { + /// GET /api/v1/statuses/:id + fn get_status(&self, id: &str) -> Result { + let route = self.route("/api/v1/statuses")?; + let route = route.join(id)?; + let response = self.send(self.client.get(route))?; + deserialise(response) + } + + /// GET /api/v1/statuses/:id/context + fn get_context(&self, id: &str) -> Result { + let route = self.route("/api/v1/statuses")?; + let route = route.join(id)?; + let route = route.join("context")?; + let response = self.send(self.client.get(route))?; + deserialise(response) + } + + /// GET /api/v1/statuses/:id/card + fn get_card(&self, id: &str) -> Result { + let route = self.route("/api/v1/statuses")?; + let route = route.join(id)?; + let route = route.join("card")?; + let response = self.send(self.client.get(route))?; + deserialise(response) + } +} + // Convert the HTTP response body from JSON. Pass up deserialization errors // transparently. fn deserialise serde::Deserialize<'de>>(response: Response) -> Result { diff --git a/src/mastodon_client.rs b/src/mastodon_client.rs index c5fee0e..23c6f8b 100644 --- a/src/mastodon_client.rs +++ b/src/mastodon_client.rs @@ -356,3 +356,29 @@ pub trait MastodonClient { unimplemented!("This method was not implemented"); } } + +/// Trait that represents clients that can make unauthenticated calls to a +/// mastodon instance +#[allow(unused)] +pub trait MastodonUnauthenticated { + /// GET /api/v1/statuses/:id + fn get_status(&self, id: &str) -> Result { + unimplemented!("This method was not implemented"); + } + /// GET /api/v1/statuses/:id/context + fn get_context(&self, id: &str) -> Result { + unimplemented!("This method was not implemented"); + } + /// GET /api/v1/statuses/:id/card + fn get_card(&self, id: &str) -> Result { + unimplemented!("This method was not implemented"); + } + /// GET /api/v1/statuses/:id/reblogged_by + fn reblogged_by(&self, id: &str) -> Result> { + unimplemented!("This method was not implemented"); + } + /// GET /api/v1/statuses/:id/favourited_by + fn favourited_by(&self, id: &str) -> Result> { + unimplemented!("This method was not implemented"); + } +}