summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Woolcock <paul@woolcock.us>2019-03-16 11:03:33 -0400
committerPaul Woolcock <paul@woolcock.us>2019-03-16 11:04:47 -0400
commit45a95e5048f645caae8d175f5de31a937a5907aa (patch)
treea7d64bd7cb71814d57b6b7e50d47a543f6a2cf2a
parent9e3b7af44afc9d6ffa3f8589e581a21b801b8507 (diff)
add the beginning of an unauthenticated client
-rw-r--r--src/lib.rs64
-rw-r--r--src/mastodon_client.rs26
2 files changed, 89 insertions, 1 deletions
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<H: HttpSend> MastodonBuilder<H> {
}
}
+/// Client that can make unauthenticated calls to a mastodon instance
+#[derive(Clone, Debug)]
+pub struct MastodonUnauth<H: HttpSend = HttpSender> {
+ client: Client,
+ http_sender: H,
+ base: url::Url,
+}
+
+impl MastodonUnauth<HttpSender> {
+ /// Create a new unauthenticated client
+ pub fn new(base: &str) -> Result<MastodonUnauth<HttpSender>> {
+ 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<H: HttpSend> MastodonUnauth<H> {
+ fn route(&self, url: &str) -> Result<url::Url> {
+ Ok(self.base.join(url)?)
+ }
+
+ fn send(&self, req: RequestBuilder) -> Result<Response> {
+ Ok(self.http_sender.send(&self.client, req)?)
+ }
+}
+
+impl<H: HttpSend> MastodonUnauthenticated<H> for MastodonUnauth<H> {
+ /// GET /api/v1/statuses/:id
+ fn get_status(&self, id: &str) -> Result<Status> {
+ 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<Context> {
+ 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<Card> {
+ 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<T: for<'de> serde::Deserialize<'de>>(response: Response) -> Result<T> {
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<H: HttpSend = HttpSender> {
unimplemented!("This method was not implemented");
}
}
+
+/// Trait that represents clients that can make unauthenticated calls to a
+/// mastodon instance
+#[allow(unused)]
+pub trait MastodonUnauthenticated<H: HttpSend> {
+ /// GET /api/v1/statuses/:id
+ fn get_status(&self, id: &str) -> Result<Status> {
+ unimplemented!("This method was not implemented");
+ }
+ /// GET /api/v1/statuses/:id/context
+ fn get_context(&self, id: &str) -> Result<Context> {
+ unimplemented!("This method was not implemented");
+ }
+ /// GET /api/v1/statuses/:id/card
+ fn get_card(&self, id: &str) -> Result<Card> {
+ unimplemented!("This method was not implemented");
+ }
+ /// GET /api/v1/statuses/:id/reblogged_by
+ fn reblogged_by(&self, id: &str) -> Result<Page<Account, H>> {
+ unimplemented!("This method was not implemented");
+ }
+ /// GET /api/v1/statuses/:id/favourited_by
+ fn favourited_by(&self, id: &str) -> Result<Page<Account, H>> {
+ unimplemented!("This method was not implemented");
+ }
+}