From 7324a6c08681cd97e207d83835dfdbf3cf047e5a Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Mon, 27 Feb 2023 08:34:17 -0500 Subject: [PATCH] Fix stream urls --- src/macros.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++- src/mastodon.rs | 14 +++++-------- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 24373a1..570a3d2 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -578,7 +578,7 @@ tokio_test::block_on(async { ), pub async fn $fn_name(&self, $param: $param_type) -> Result + '_> { use $crate::event_stream::event_stream; - let mut url: Url = self.route(concat!("/api/v1/streaming/", stringify!($stream))).parse()?; + let mut url: Url = self.route(concat!("/api/v1/streaming/", $stream)).parse()?; url.query_pairs_mut().append_pair(stringify!($param), $param.as_ref()); let url = url.to_string(); let response = self.authenticated(self.client.get(url.as_str())).header("Accept", "application/json").send().await?; @@ -598,5 +598,56 @@ tokio_test::block_on(async { } streaming! { $($rest)* } }; + ($desc:tt $fn_name:ident(flag $param:ident)@$stream:literal, $($rest:tt)*) => { + doc_comment! { + concat!( + $desc, + "\n\nExample:\n\n", + " +use mastodon_async::prelude::*; +use mastodon_async::entities::event::Event; +use futures_util::{pin_mut, StreamExt, TryStreamExt}; + +tokio_test::block_on(async { + let data = Data::default(); + let client = Mastodon::from(data); + let stream = client.", + stringify!($fn_name), + "(false).await.unwrap(); + stream.try_for_each(|event| async move { + match event { + Event::Update(ref status) => { /* .. */ }, + Event::Notification(ref notification) => { /* .. */ }, + Event::Delete(ref id) => { /* .. */ }, + Event::FiltersChanged => { /* .. */ }, + } + Ok(()) + }).await.unwrap(); +});" + ), + pub async fn $fn_name(&self, $param: bool) -> Result + '_> { + use $crate::event_stream::event_stream; + let mut url: Url = self.route(concat!("/api/v1/streaming/", $stream)).parse()?; + if $param { + url.query_pairs_mut().append_key_only(stringify!($param)); + } + let url = url.to_string(); + let response = self.authenticated(self.client.get(url.as_str())).header("Accept", "application/json").send().await?; + debug!( + status = log_serde!(response Status), url = as_debug!(url), + headers = log_serde!(response Headers); + "received API response" + ); + let status = response.status(); + if status.is_success() { + Ok(event_stream(response, url, self)) + } else { + let response = response.json().await?; + Err(Error::Api{ status, response }) + } + } + } + streaming! { $($rest)* } + }; () => {} } diff --git a/src/mastodon.rs b/src/mastodon.rs index d337f56..46f4b41 100644 --- a/src/mastodon.rs +++ b/src/mastodon.rs @@ -130,21 +130,17 @@ impl Mastodon { "All public posts known to the server. Analogous to the federated timeline." stream_public@"public", "All public posts known to the server, filtered for media attachments. Analogous to the federated timeline with 'only media' enabled." - stream_public_media@"public:media", + stream_public_media@"public/media", "All public posts originating from this server." - stream_local@"public:local", - "All public posts originating from this server, filtered for media attachments. Analogous to the local timeline with 'only media' enabled." - stream_local_media@"public:local:media", + stream_local(flag only_media)@"public/local", "All public posts originating from other servers." - stream_remote@"public:remote", - "All public posts originating from other servers, filtered for media attachments." - stream_remote_media@"public:remote:media", + stream_remote(flag only_media)@"public/remote", "All public posts using a certain hashtag." stream_hashtag(tag: impl AsRef, like "#bots")@"hashtag", "All public posts using a certain hashtag, originating from this server." - stream_local_hashtag(tag: impl AsRef, like "#bots")@"hashtag:local", + stream_local_hashtag(tag: impl AsRef, like "#bots")@"hashtag/local", "Notifications for the current user." - stream_notifications@"user:notification", + stream_notifications@"user/notification", "Updates to a specific list." stream_list(list: impl AsRef, like "12345")@"list", "Updates to direct conversations."