add the beginning of an unauthenticated client

master
Paul Woolcock 5 years ago
parent 9e3b7af44a
commit 45a95e5048
  1. 64
      src/lib.rs
  2. 26
      src/mastodon_client.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> {

@ -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");
}
}

Loading…
Cancel
Save