Version 0.9, Changed API to use Cow<'static, str>

Added documentation to Error enum
master
Aaron Power 7 years ago
parent b6d350f29e
commit f8aa32f003
  1. 7
      Cargo.toml
  2. 60
      src/lib.rs
  3. 10
      tests/upload_photo.rs

@ -1,6 +1,6 @@
[package] [package]
name = "mammut" name = "mammut"
version = "0.8.0" version = "0.9.0"
description = "A wrapper around the Mastodon API." description = "A wrapper around the Mastodon API."
authors = ["Aaron Power <theaaronepower@gmail.com>"] authors = ["Aaron Power <theaaronepower@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
@ -10,7 +10,7 @@ keywords = ["api", "web", "social", "mastodon", "wrapper"]
categories = ["web-programming", "http-client"] categories = ["web-programming", "http-client"]
[dependencies] [dependencies]
reqwest = "0.6" reqwest = "0.8"
serde = "1" serde = "1"
serde_json = "1" serde_json = "1"
serde_derive = "1" serde_derive = "1"
@ -18,3 +18,6 @@ serde_derive = "1"
[dependencies.chrono] [dependencies.chrono]
version = "0.4" version = "0.4"
features = ["serde"] features = ["serde"]
[dev-dependencies]
dotenv = "0.10"

@ -49,10 +49,11 @@ pub mod entities;
/// Registering your app. /// Registering your app.
pub mod registration; pub mod registration;
use std::ops; use std::borrow::Cow;
use std::fmt;
use std::error::Error as StdError; use std::error::Error as StdError;
use std::fmt;
use std::io::Error as IoError; use std::io::Error as IoError;
use std::ops;
use json::Error as SerdeError; use json::Error as SerdeError;
use reqwest::Error as HttpError; use reqwest::Error as HttpError;
@ -105,7 +106,7 @@ macro_rules! route {
let form_data = Form::new() let form_data = Form::new()
$( $(
.file(stringify!($param), $param)? .file(stringify!($param), $param.as_ref())?
)*; )*;
let mut response = self.client.post(&self.route(concat!("/api/v1/", $url))) let mut response = self.client.post(&self.route(concat!("/api/v1/", $url)))
@ -224,31 +225,43 @@ pub struct Mastodon {
/// to authenticate on every run. /// to authenticate on every run.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Data { pub struct Data {
pub base: String, pub base: Cow<'static, str>,
pub client_id: String, pub client_id: Cow<'static, str>,
pub client_secret: String, pub client_secret: Cow<'static, str>,
pub redirect: String, pub redirect: Cow<'static, str>,
pub token: String, pub token: Cow<'static, str>,
} }
/// enum of possible errors encountered using the mastodon API.
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum Error { pub enum Error {
/// Error from the Mastodon API. This typically means something went
/// wrong with your authentication or data.
Api(ApiError), Api(ApiError),
/// Error deserialising to json. Typically represents a breaking change in
/// the Mastodon API
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
Serde(SerdeError), Serde(SerdeError),
/// Error encountered in the HTTP backend while requesting a route.
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
Http(HttpError), Http(HttpError),
/// Wrapper around the `std::io::Error` struct.
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
Io(IoError), Io(IoError),
/// Missing Client Id.
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
ClientIdRequired, ClientIdRequired,
/// Missing Client Secret.
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
ClientSecretRequired, ClientSecretRequired,
/// Missing Access Token.
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
AccessTokenRequired, AccessTokenRequired,
/// Generic client error.
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
Client(StatusCode), Client(StatusCode),
/// Generic server error.
#[serde(skip_deserializing)] #[serde(skip_deserializing)]
Server(StatusCode), Server(StatusCode),
} }
@ -286,25 +299,26 @@ pub struct ApiError {
} }
impl Mastodon { impl Mastodon {
fn from_registration(base: String, fn from_registration<I>(base: I,
client_id: String, client_id: I,
client_secret: String, client_secret: I,
redirect: String, redirect: I,
token: String, token: I,
client: Client) client: Client)
-> Self -> Self
where I: Into<Cow<'static, str>>
{ {
let data = Data { let data = Data {
base: base, base: base.into(),
client_id: client_id, client_id: client_id.into(),
client_secret: client_secret, client_secret: client_secret.into(),
redirect: redirect, redirect: redirect.into(),
token: token, token: token.into(),
}; };
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.set(Authorization(Bearer { token: data.token.clone() })); headers.set(Authorization(Bearer { token: (*data.token).to_owned() }));
Mastodon { Mastodon {
client: client, client: client,
@ -316,7 +330,7 @@ impl Mastodon {
/// Creates a mastodon instance from the data struct. /// Creates a mastodon instance from the data struct.
pub fn from_data(data: Data) -> Self { pub fn from_data(data: Data) -> Self {
let mut headers = Headers::new(); let mut headers = Headers::new();
headers.set(Authorization(Bearer { token: data.token.clone() })); headers.set(Authorization(Bearer { token: (*data.token).to_owned() }));
Mastodon { Mastodon {
client: Client::new(), client: Client::new(),
@ -335,9 +349,9 @@ impl Mastodon {
(get) get_home_timeline: "timelines/home" => Vec<Status>, (get) get_home_timeline: "timelines/home" => Vec<Status>,
(post (id: u64,)) allow_follow_request: "accounts/follow_requests/authorize" => Empty, (post (id: u64,)) allow_follow_request: "accounts/follow_requests/authorize" => Empty,
(post (id: u64,)) reject_follow_request: "accounts/follow_requests/reject" => Empty, (post (id: u64,)) reject_follow_request: "accounts/follow_requests/reject" => Empty,
(post (uri: String,)) follows: "follows" => Account, (post (uri: Cow<'static, str>,)) follows: "follows" => Account,
(post) clear_notifications: "notifications/clear" => Empty, (post) clear_notifications: "notifications/clear" => Empty,
(post multipart (file: String,)) media: "media" => Attachment, (post multipart (file: Cow<'static, str>,)) media: "media" => Attachment,
(post (account_id: u64, status_ids: Vec<u64>, comment: String,)) report: (post (account_id: u64, status_ids: Vec<u64>, comment: String,)) report:
"reports" => Report, "reports" => Report,
(post (q: String, resolve: bool,)) search: "search" => SearchResult, (post (q: String, resolve: bool,)) search: "search" => SearchResult,
@ -458,7 +472,7 @@ impl Mastodon {
methods![get, post, delete,]; methods![get, post, delete,];
fn route(&self, url: &str) -> String { fn route(&self, url: &str) -> String {
let mut s = self.base.clone(); let mut s = (*self.base).to_owned();
s += url; s += url;
s s
} }

@ -15,11 +15,11 @@ fn upload_photo() {
fn run() -> mammut::Result<()> { fn run() -> mammut::Result<()> {
let data = Data { let data = Data {
base: String::from(env::var("BASE").unwrap()), base: env::var("BASE").unwrap().into(),
client_id: String::from(env::var("CLIENT_ID").unwrap()), client_id: env::var("CLIENT_ID").unwrap().into(),
client_secret: String::from(env::var("CLIENT_SECRET").unwrap()), client_secret: env::var("CLIENT_SECRET").unwrap().into(),
redirect: String::from(env::var("REDIRECT").unwrap()), redirect: env::var("REDIRECT").unwrap().into(),
token: String::from(env::var("TOKEN").unwrap()), token: env::var("TOKEN").unwrap().into(),
}; };
let mastodon = Mastodon::from_data(data); let mastodon = Mastodon::from_data(data);

Loading…
Cancel
Save