Version 0.9, Changed API to use Cow<'static, str>
Added documentation to Error enum
This commit is contained in:
+5
-2
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "mammut"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
description = "A wrapper around the Mastodon API."
|
||||
authors = ["Aaron Power <theaaronepower@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
@@ -10,7 +10,7 @@ keywords = ["api", "web", "social", "mastodon", "wrapper"]
|
||||
categories = ["web-programming", "http-client"]
|
||||
|
||||
[dependencies]
|
||||
reqwest = "0.6"
|
||||
reqwest = "0.8"
|
||||
serde = "1"
|
||||
serde_json = "1"
|
||||
serde_derive = "1"
|
||||
@@ -18,3 +18,6 @@ serde_derive = "1"
|
||||
[dependencies.chrono]
|
||||
version = "0.4"
|
||||
features = ["serde"]
|
||||
|
||||
[dev-dependencies]
|
||||
dotenv = "0.10"
|
||||
|
||||
+37
-23
@@ -49,10 +49,11 @@ pub mod entities;
|
||||
/// Registering your app.
|
||||
pub mod registration;
|
||||
|
||||
use std::ops;
|
||||
use std::fmt;
|
||||
use std::borrow::Cow;
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
use std::io::Error as IoError;
|
||||
use std::ops;
|
||||
|
||||
use json::Error as SerdeError;
|
||||
use reqwest::Error as HttpError;
|
||||
@@ -105,7 +106,7 @@ macro_rules! route {
|
||||
|
||||
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)))
|
||||
@@ -224,31 +225,43 @@ pub struct Mastodon {
|
||||
/// to authenticate on every run.
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct Data {
|
||||
pub base: String,
|
||||
pub client_id: String,
|
||||
pub client_secret: String,
|
||||
pub redirect: String,
|
||||
pub token: String,
|
||||
pub base: Cow<'static, str>,
|
||||
pub client_id: Cow<'static, str>,
|
||||
pub client_secret: Cow<'static, str>,
|
||||
pub redirect: Cow<'static, str>,
|
||||
pub token: Cow<'static, str>,
|
||||
}
|
||||
|
||||
/// enum of possible errors encountered using the mastodon API.
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum Error {
|
||||
/// Error from the Mastodon API. This typically means something went
|
||||
/// wrong with your authentication or data.
|
||||
Api(ApiError),
|
||||
/// Error deserialising to json. Typically represents a breaking change in
|
||||
/// the Mastodon API
|
||||
#[serde(skip_deserializing)]
|
||||
Serde(SerdeError),
|
||||
/// Error encountered in the HTTP backend while requesting a route.
|
||||
#[serde(skip_deserializing)]
|
||||
Http(HttpError),
|
||||
/// Wrapper around the `std::io::Error` struct.
|
||||
#[serde(skip_deserializing)]
|
||||
Io(IoError),
|
||||
/// Missing Client Id.
|
||||
#[serde(skip_deserializing)]
|
||||
ClientIdRequired,
|
||||
/// Missing Client Secret.
|
||||
#[serde(skip_deserializing)]
|
||||
ClientSecretRequired,
|
||||
/// Missing Access Token.
|
||||
#[serde(skip_deserializing)]
|
||||
AccessTokenRequired,
|
||||
/// Generic client error.
|
||||
#[serde(skip_deserializing)]
|
||||
Client(StatusCode),
|
||||
/// Generic server error.
|
||||
#[serde(skip_deserializing)]
|
||||
Server(StatusCode),
|
||||
}
|
||||
@@ -286,25 +299,26 @@ pub struct ApiError {
|
||||
}
|
||||
|
||||
impl Mastodon {
|
||||
fn from_registration(base: String,
|
||||
client_id: String,
|
||||
client_secret: String,
|
||||
redirect: String,
|
||||
token: String,
|
||||
fn from_registration<I>(base: I,
|
||||
client_id: I,
|
||||
client_secret: I,
|
||||
redirect: I,
|
||||
token: I,
|
||||
client: Client)
|
||||
-> Self
|
||||
where I: Into<Cow<'static, str>>
|
||||
{
|
||||
let data = Data {
|
||||
base: base,
|
||||
client_id: client_id,
|
||||
client_secret: client_secret,
|
||||
redirect: redirect,
|
||||
token: token,
|
||||
base: base.into(),
|
||||
client_id: client_id.into(),
|
||||
client_secret: client_secret.into(),
|
||||
redirect: redirect.into(),
|
||||
token: token.into(),
|
||||
|
||||
};
|
||||
|
||||
let mut headers = Headers::new();
|
||||
headers.set(Authorization(Bearer { token: data.token.clone() }));
|
||||
headers.set(Authorization(Bearer { token: (*data.token).to_owned() }));
|
||||
|
||||
Mastodon {
|
||||
client: client,
|
||||
@@ -316,7 +330,7 @@ impl Mastodon {
|
||||
/// Creates a mastodon instance from the data struct.
|
||||
pub fn from_data(data: Data) -> Self {
|
||||
let mut headers = Headers::new();
|
||||
headers.set(Authorization(Bearer { token: data.token.clone() }));
|
||||
headers.set(Authorization(Bearer { token: (*data.token).to_owned() }));
|
||||
|
||||
Mastodon {
|
||||
client: Client::new(),
|
||||
@@ -335,9 +349,9 @@ impl Mastodon {
|
||||
(get) get_home_timeline: "timelines/home" => Vec<Status>,
|
||||
(post (id: u64,)) allow_follow_request: "accounts/follow_requests/authorize" => 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 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:
|
||||
"reports" => Report,
|
||||
(post (q: String, resolve: bool,)) search: "search" => SearchResult,
|
||||
@@ -458,7 +472,7 @@ impl Mastodon {
|
||||
methods![get, post, delete,];
|
||||
|
||||
fn route(&self, url: &str) -> String {
|
||||
let mut s = self.base.clone();
|
||||
let mut s = (*self.base).to_owned();
|
||||
s += url;
|
||||
s
|
||||
}
|
||||
|
||||
@@ -15,11 +15,11 @@ fn upload_photo() {
|
||||
fn run() -> mammut::Result<()> {
|
||||
|
||||
let data = Data {
|
||||
base: String::from(env::var("BASE").unwrap()),
|
||||
client_id: String::from(env::var("CLIENT_ID").unwrap()),
|
||||
client_secret: String::from(env::var("CLIENT_SECRET").unwrap()),
|
||||
redirect: String::from(env::var("REDIRECT").unwrap()),
|
||||
token: String::from(env::var("TOKEN").unwrap()),
|
||||
base: env::var("BASE").unwrap().into(),
|
||||
client_id: env::var("CLIENT_ID").unwrap().into(),
|
||||
client_secret: env::var("CLIENT_SECRET").unwrap().into(),
|
||||
redirect: env::var("REDIRECT").unwrap().into(),
|
||||
token: env::var("TOKEN").unwrap().into(),
|
||||
};
|
||||
|
||||
let mastodon = Mastodon::from_data(data);
|
||||
|
||||
Reference in New Issue
Block a user