Fix stream urls

This commit is contained in:
D. Scott Boggs 2023-02-27 08:34:17 -05:00 committed by Scott Boggs
parent ae3c9ae6ce
commit 7324a6c086
2 changed files with 57 additions and 10 deletions

View File

@ -578,7 +578,7 @@ tokio_test::block_on(async {
), ),
pub async fn $fn_name(&self, $param: $param_type) -> Result<impl TryStream<Ok=(Event, Mastodon), Error=Error> + '_> { pub async fn $fn_name(&self, $param: $param_type) -> Result<impl TryStream<Ok=(Event, Mastodon), Error=Error> + '_> {
use $crate::event_stream::event_stream; 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()); url.query_pairs_mut().append_pair(stringify!($param), $param.as_ref());
let url = url.to_string(); let url = url.to_string();
let response = self.authenticated(self.client.get(url.as_str())).header("Accept", "application/json").send().await?; 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)* } 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<impl TryStream<Ok=(Event, Mastodon), Error=Error> + '_> {
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)* }
};
() => {} () => {}
} }

View File

@ -130,21 +130,17 @@ impl Mastodon {
"All public posts known to the server. Analogous to the federated timeline." "All public posts known to the server. Analogous to the federated timeline."
stream_public@"public", stream_public@"public",
"All public posts known to the server, filtered for media attachments. Analogous to the federated timeline with 'only media' enabled." "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." "All public posts originating from this server."
stream_local@"public:local", stream_local(flag only_media)@"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",
"All public posts originating from other servers." "All public posts originating from other servers."
stream_remote@"public:remote", stream_remote(flag only_media)@"public/remote",
"All public posts originating from other servers, filtered for media attachments."
stream_remote_media@"public:remote:media",
"All public posts using a certain hashtag." "All public posts using a certain hashtag."
stream_hashtag(tag: impl AsRef<str>, like "#bots")@"hashtag", stream_hashtag(tag: impl AsRef<str>, like "#bots")@"hashtag",
"All public posts using a certain hashtag, originating from this server." "All public posts using a certain hashtag, originating from this server."
stream_local_hashtag(tag: impl AsRef<str>, like "#bots")@"hashtag:local", stream_local_hashtag(tag: impl AsRef<str>, like "#bots")@"hashtag/local",
"Notifications for the current user." "Notifications for the current user."
stream_notifications@"user:notification", stream_notifications@"user/notification",
"Updates to a specific list." "Updates to a specific list."
stream_list(list: impl AsRef<str>, like "12345")@"list", stream_list(list: impl AsRef<str>, like "12345")@"list",
"Updates to direct conversations." "Updates to direct conversations."