Fix routes which take an Id type; fix doctests

This commit is contained in:
D. Scott Boggs 2023-01-21 14:36:45 -05:00 committed by Scott Boggs
parent 53fc05d36f
commit e662eac46a
9 changed files with 60 additions and 43 deletions

View File

@ -106,8 +106,7 @@ features = ["async_tokio"]
version = "0.13"
[features]
all = ["toml", "json", "env"]
# default = ["reqwest/default-tls"]
all = ["toml", "json", "env", "mt"]
default = ["reqwest/default-tls"]
env = ["envy"]
mt = ["tokio/rt-multi-thread"]

View File

@ -30,7 +30,7 @@ This library offers structured logging. To get better information about bugs or
how something is working, I recommend adding the femme crate as a dependency,
then adding this line to the beginning of your main() function:
```rust
```rust,ignore
femme::with_level(log::LevelFilter::Trace);
```
@ -49,9 +49,13 @@ In your `Cargo.toml`, make sure you enable the `toml` feature:
```toml
[dependencies.mastodon-async]
version = "1.0"
features = ["toml"]
features = ["toml", "mt"]
```
The `"mt"` feature is for tokio multi-threaded. For single threaded, drop the
`"mt"` feature and replace `#[tokio::main]` with
`#[tokio::main(flavor = "current_thread")]`.
```rust,no_run
// src/main.rs
@ -59,7 +63,7 @@ use mastodon_async::prelude::*;
use mastodon_async::helpers::toml; // requires `features = ["toml"]`
use mastodon_async::{helpers::cli, Result};
#[tokio::main]
#[tokio::main] // requires `features = ["mt"]
async fn main() -> Result<()> {
let mastodon = if let Ok(data) = toml::from_file("mastodon-data.toml") {
Mastodon::from(data)

View File

@ -10,8 +10,20 @@ edition.workspace = true
[dependencies]
futures = "0.3.25"
log = { version = "0.4", features = ["kv_unstable", "serde", "std", "kv_unstable_serde", "kv_unstable_std"] }
serde = { version = "1", features = ["derive"] }
thiserror = "1"
time = { version = "0.3", features = ["parsing", "serde", "formatting"] }
static_assertions = "1"
static_assertions = "1"
[dependencies.log]
version = "0.4"
features = ["kv_unstable", "serde", "std", "kv_unstable_serde", "kv_unstable_std"]
[dependencies.serde]
version = "1"
features = ["derive"]
[dependencies.time]
version = "0.3"
features = ["parsing", "serde", "formatting"]
[dev-dependencies]
serde_json = "1.0.91"

View File

@ -8,7 +8,7 @@ use time::{serde::iso8601, OffsetDateTime};
///
/// ## Example
/// ```rust
/// use mastodon_async::entities::filter::Filter;
/// use mastodon_async_entities::prelude::*;
/// let subject = r#"{
/// "id": "19972",
/// "title": "Test filter",
@ -32,7 +32,7 @@ use time::{serde::iso8601, OffsetDateTime};
/// ]
/// }"#;
/// let subject: Filter = serde_json::from_str(subject).expect("deserialize");
/// assert_eq!(subject.id, "19972");
/// assert_eq!(subject.id, FilterId::new("19972"));
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Filter {

View File

@ -5,9 +5,11 @@ use mastodon_async::Result;
#[cfg(feature = "toml")]
async fn run() -> Result<()> {
use mastodon_async::entities::account::AccountId;
let mastodon = register::get_mastodon_data().await?;
let input = register::read_line("Enter the account id you'd like to follow: ")?;
let new_follow = mastodon.follow(input.trim()).await?;
let account = AccountId::new(input.trim());
let new_follow = mastodon.follow(&account).await?;
println!("{:#?}", new_follow);
Ok(())

View File

@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize};
/// tokio_test::block_on(async {
/// let data = Data::default();
/// let client = Mastodon::from(data);
/// let statuses = client.statuses("user-id", Default::default()).await.unwrap().items_iter();
/// let statuses = client.statuses(&AccountId::new("user-id"), Default::default()).await.unwrap().items_iter();
/// statuses.for_each(|status| async move {
/// // Do something with the status
/// }).await;

View File

@ -439,7 +439,7 @@ macro_rules! route {
macro_rules! route_id {
($(($method:ident) $name:ident: $url:expr => $ret:ty,)*) => {
($(($method:ident) $name:ident[$id_type:ty]: $url:expr => $ret:ty,)*) => {
$(
doc_comment! {
concat!(
@ -456,7 +456,7 @@ macro_rules! route_id {
"# }\n",
"```"
),
pub async fn $name(&self, id: &str) -> Result<$ret> {
pub async fn $name(&self, id: &$id_type) -> Result<$ret> {
self.$method(self.route(&format!(concat!("/api/v1/", $url), id))).await
}
}

View File

@ -98,27 +98,27 @@ impl Mastodon {
}
route_id! {
(get) get_account: "accounts/{}" => Account,
(post) follow: "accounts/{}/follow" => Relationship,
(post) unfollow: "accounts/{}/unfollow" => Relationship,
(post) block: "accounts/{}/block" => Relationship,
(post) unblock: "accounts/{}/unblock" => Relationship,
(get) mute: "accounts/{}/mute" => Relationship,
(get) unmute: "accounts/{}/unmute" => Relationship,
(get) get_notification: "notifications/{}" => Notification,
(get) get_status: "statuses/{}" => Status,
(get) get_context: "statuses/{}/context" => Context,
(get) get_card: "statuses/{}/card" => Card,
(post) reblog: "statuses/{}/reblog" => Status,
(post) unreblog: "statuses/{}/unreblog" => Status,
(post) favourite: "statuses/{}/favourite" => Status,
(post) unfavourite: "statuses/{}/unfavourite" => Status,
(delete) delete_status: "statuses/{}" => Empty,
(get) get_filter: "filters/{}" => Filter,
(delete) delete_filter: "filters/{}" => Empty,
(delete) delete_from_suggestions: "suggestions/{}" => Empty,
(post) endorse_user: "accounts/{}/pin" => Relationship,
(post) unendorse_user: "accounts/{}/unpin" => Relationship,
(get) get_account[AccountId]: "accounts/{}" => Account,
(post) follow[AccountId]: "accounts/{}/follow" => Relationship,
(post) unfollow[AccountId]: "accounts/{}/unfollow" => Relationship,
(post) block[AccountId]: "accounts/{}/block" => Relationship,
(post) unblock[AccountId]: "accounts/{}/unblock" => Relationship,
(get) mute[AccountId]: "accounts/{}/mute" => Relationship,
(get) unmute[AccountId]: "accounts/{}/unmute" => Relationship,
(get) get_notification[NotificationId]: "notifications/{}" => Notification,
(get) get_status[StatusId]: "statuses/{}" => Status,
(get) get_context[StatusId]: "statuses/{}/context" => Context,
(get) get_card[StatusId]: "statuses/{}/card" => Card,
(post) reblog[StatusId]: "statuses/{}/reblog" => Status,
(post) unreblog[StatusId]: "statuses/{}/unreblog" => Status,
(post) favourite[StatusId]: "statuses/{}/favourite" => Status,
(post) unfavourite[StatusId]: "statuses/{}/unfavourite" => Status,
(delete) delete_status[StatusId]: "statuses/{}" => Empty,
(get) get_filter[FilterId]: "filters/{}" => Filter,
(delete) delete_filter[FilterId]: "filters/{}" => Empty,
(delete) delete_from_suggestions[AccountId]: "suggestions/{}" => Empty,
(post) endorse_user[AccountId]: "accounts/{}/pin" => Relationship,
(post) unendorse_user[AccountId]: "accounts/{}/unpin" => Relationship,
}
streaming! {
@ -225,7 +225,7 @@ impl Mastodon {
/// tokio_test::block_on(async {
/// let data = Data::default();
/// let client = Mastodon::from(data);
/// let statuses = client.statuses("user-id", Default::default()).await.unwrap();
/// let statuses = client.statuses(&AccountId::new("user-id"), Default::default()).await.unwrap();
/// });
/// ```
///
@ -236,12 +236,12 @@ impl Mastodon {
/// let client = Mastodon::from(data);
/// let mut request = StatusesRequest::new();
/// request.only_media();
/// let statuses = client.statuses("user-id", request).await.unwrap();
/// let statuses = client.statuses(&AccountId::new("user-id"), request).await.unwrap();
/// });
/// ```
pub async fn statuses<'a, 'b: 'a>(
&'b self,
id: &'b str,
id: &'b AccountId,
request: StatusesRequest<'a>,
) -> Result<Page<Status>> {
let call_id = Uuid::new_v4();
@ -257,17 +257,17 @@ impl Mastodon {
/// Returns the client account's relationship to a list of other accounts.
/// Such as whether they follow them or vice versa.
pub async fn relationships(&self, ids: &[&str]) -> Result<Page<Relationship>> {
pub async fn relationships(&self, ids: &[&AccountId]) -> Result<Page<Relationship>> {
let call_id = Uuid::new_v4();
let mut url = self.route("/api/v1/accounts/relationships?");
if ids.len() == 1 {
url += "id=";
url += ids[0];
url += ids[0].as_ref();
} else {
for id in ids {
url += "id[]=";
url += id;
url += id.as_ref();
url += "&";
}
url.pop();

View File

@ -151,7 +151,7 @@ impl<T: Clone + for<'de> Deserialize<'de> + Serialize> Page<T> {
/// let req = StatusesRequest::new();
///
/// tokio_test::block_on(async {
/// let resp = mastodon.statuses("some-id", req).await.unwrap();
/// let resp = mastodon.statuses(&AccountId::new("some-id"), req).await.unwrap();
/// resp.items_iter().for_each(|status| async move {
/// // do something with status
/// }).await;