2022-12-05 13:52:48 +00:00
|
|
|
use super::{Mastodon, Result};
|
2022-12-23 15:09:33 +00:00
|
|
|
use crate::{
|
|
|
|
entities::itemsiter::ItemsIter,
|
|
|
|
format_err,
|
|
|
|
helpers::read_response::read_response,
|
|
|
|
Error,
|
|
|
|
};
|
2022-12-05 13:52:48 +00:00
|
|
|
use futures::Stream;
|
2022-11-27 14:44:43 +00:00
|
|
|
use hyper_old_types::header::{parsing, Link, RelationType};
|
2022-12-07 20:58:28 +00:00
|
|
|
use log::{as_debug, as_serde, debug, error, trace};
|
2022-12-05 13:52:48 +00:00
|
|
|
use reqwest::{header::LINK, Response, Url};
|
2022-12-07 20:58:28 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use uuid::Uuid;
|
2022-12-05 13:52:48 +00:00
|
|
|
// use url::Url;
|
2022-11-27 14:44:43 +00:00
|
|
|
|
|
|
|
macro_rules! pages {
|
|
|
|
($($direction:ident: $fun:ident),*) => {
|
|
|
|
|
|
|
|
$(
|
|
|
|
doc_comment!(concat!(
|
|
|
|
"Method to retrieve the ", stringify!($direction), " page of results"),
|
2022-12-05 13:52:48 +00:00
|
|
|
pub async fn $fun(&mut self) -> Result<Option<Vec<T>>> {
|
2022-11-27 14:44:43 +00:00
|
|
|
let url = match self.$direction.take() {
|
|
|
|
Some(s) => s,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
2022-12-07 20:58:28 +00:00
|
|
|
debug!(
|
2022-12-07 21:20:20 +00:00
|
|
|
url = url.as_str(), method = "get",
|
2022-12-07 20:58:28 +00:00
|
|
|
call_id = as_debug!(self.call_id),
|
|
|
|
direction = stringify!($direction);
|
|
|
|
"making API request"
|
|
|
|
);
|
|
|
|
let url: String = url.into(); // <- for logging
|
|
|
|
let response = self.mastodon.client.get(&url).send().await?;
|
|
|
|
match response.error_for_status() {
|
|
|
|
Ok(response) => {
|
|
|
|
let (prev, next) = get_links(&response, self.call_id)?;
|
2022-12-07 21:20:20 +00:00
|
|
|
let response = read_response(response).await?;
|
2022-12-07 20:58:28 +00:00
|
|
|
debug!(
|
|
|
|
url = url, method = "get", next = as_debug!(next),
|
|
|
|
prev = as_debug!(prev), call_id = as_debug!(self.call_id),
|
|
|
|
response = as_serde!(response);
|
|
|
|
"received next pages from API"
|
|
|
|
);
|
|
|
|
self.next = next;
|
|
|
|
self.prev = prev;
|
2022-11-27 14:44:43 +00:00
|
|
|
|
|
|
|
|
2022-12-07 20:58:28 +00:00
|
|
|
Ok(Some(response))
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
error!(
|
|
|
|
err = as_debug!(err), url = url,
|
|
|
|
method = stringify!($method),
|
|
|
|
call_id = as_debug!(self.call_id);
|
|
|
|
"error making API request"
|
|
|
|
);
|
|
|
|
Err(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 14:44:43 +00:00
|
|
|
});
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Owned version of the `Page` struct in this module. Allows this to be more
|
|
|
|
/// easily stored for later use
|
|
|
|
///
|
2022-11-29 23:50:29 +00:00
|
|
|
/// // Example
|
2022-11-27 14:44:43 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2022-12-22 18:19:49 +00:00
|
|
|
/// use mastodon_async::{
|
2022-11-29 23:50:29 +00:00
|
|
|
/// prelude::*,
|
2022-12-05 13:52:48 +00:00
|
|
|
/// page::Page,
|
2022-11-29 23:50:29 +00:00
|
|
|
/// entities::status::Status
|
|
|
|
/// };
|
|
|
|
/// use std::cell::RefCell;
|
|
|
|
///
|
2022-12-05 13:52:48 +00:00
|
|
|
/// tokio_test::block_on(async {
|
|
|
|
/// let data = Data::default();
|
|
|
|
/// struct HomeTimeline {
|
|
|
|
/// client: Mastodon,
|
|
|
|
/// page: RefCell<Option<Page<Status>>>,
|
|
|
|
/// }
|
|
|
|
/// let client = Mastodon::from(data);
|
|
|
|
/// let home = client.get_home_timeline().await.unwrap();
|
|
|
|
/// let tl = HomeTimeline {
|
|
|
|
/// client,
|
|
|
|
/// page: RefCell::new(Some(home)),
|
|
|
|
/// };
|
|
|
|
/// });
|
2022-11-27 14:44:43 +00:00
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// Represents a single page of API results
|
|
|
|
#[derive(Debug, Clone)]
|
2022-12-07 20:58:28 +00:00
|
|
|
pub struct Page<T: for<'de> Deserialize<'de> + Serialize> {
|
2022-12-05 13:52:48 +00:00
|
|
|
mastodon: Mastodon,
|
2022-11-27 14:44:43 +00:00
|
|
|
next: Option<Url>,
|
|
|
|
prev: Option<Url>,
|
|
|
|
/// Initial set of items
|
|
|
|
pub initial_items: Vec<T>,
|
2022-12-07 20:58:28 +00:00
|
|
|
pub(crate) call_id: Uuid,
|
2022-11-27 14:44:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 20:58:28 +00:00
|
|
|
impl<'a, T: for<'de> Deserialize<'de> + Serialize> Page<T> {
|
2022-11-27 14:44:43 +00:00
|
|
|
pages! {
|
|
|
|
next: next_page,
|
|
|
|
prev: prev_page
|
|
|
|
}
|
|
|
|
|
2022-12-05 13:52:48 +00:00
|
|
|
/// Create a new Page.
|
2022-12-07 20:58:28 +00:00
|
|
|
pub(crate) async fn new(mastodon: Mastodon, response: Response, call_id: Uuid) -> Result<Self> {
|
2022-12-23 15:09:33 +00:00
|
|
|
let status = response.status();
|
|
|
|
if status.is_success() {
|
|
|
|
let (prev, next) = get_links(&response, call_id)?;
|
|
|
|
let initial_items = read_response(response).await?;
|
|
|
|
debug!(
|
|
|
|
initial_items = as_serde!(initial_items), prev = as_debug!(prev),
|
|
|
|
next = as_debug!(next), call_id = as_debug!(call_id);
|
|
|
|
"received first page from API call"
|
|
|
|
);
|
|
|
|
Ok(Page {
|
|
|
|
initial_items,
|
|
|
|
next,
|
|
|
|
prev,
|
|
|
|
mastodon,
|
|
|
|
call_id,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let response = response.json().await?;
|
|
|
|
Err(Error::Api {
|
|
|
|
status,
|
|
|
|
response,
|
|
|
|
})
|
|
|
|
}
|
2022-11-27 14:44:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 20:58:28 +00:00
|
|
|
impl<T: Clone + for<'de> Deserialize<'de> + Serialize> Page<T> {
|
2022-11-27 14:44:43 +00:00
|
|
|
/// Returns an iterator that provides a stream of `T`s
|
|
|
|
///
|
|
|
|
/// This abstracts away the process of iterating over each item in a page,
|
|
|
|
/// then making an http call, then iterating over each item in the new
|
|
|
|
/// page, etc. The iterator provides a stream of `T`s, calling
|
|
|
|
/// `self.next_page()`
|
|
|
|
/// when necessary to get
|
|
|
|
/// more of them, until
|
|
|
|
/// there are no more items.
|
|
|
|
///
|
2022-11-29 23:50:29 +00:00
|
|
|
/// // Example
|
2022-11-27 14:44:43 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
2022-12-22 18:19:49 +00:00
|
|
|
/// use mastodon_async::prelude::*;
|
2022-12-05 13:52:48 +00:00
|
|
|
/// use futures_util::StreamExt;
|
|
|
|
///
|
2022-11-29 23:50:29 +00:00
|
|
|
/// let data = Data::default();
|
2022-11-27 14:44:43 +00:00
|
|
|
/// let mastodon = Mastodon::from(data);
|
|
|
|
/// let req = StatusesRequest::new();
|
2022-12-05 13:52:48 +00:00
|
|
|
///
|
|
|
|
/// tokio_test::block_on(async {
|
|
|
|
/// let resp = mastodon.statuses("some-id", req).await.unwrap();
|
|
|
|
/// resp.items_iter().for_each(|status| async move {
|
|
|
|
/// // do something with status
|
|
|
|
/// }).await;
|
|
|
|
/// });
|
2022-11-27 14:44:43 +00:00
|
|
|
/// ```
|
2022-12-05 13:52:48 +00:00
|
|
|
pub fn items_iter(self) -> impl Stream<Item = T> {
|
|
|
|
ItemsIter::new(self).stream()
|
2022-11-27 14:44:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 20:58:28 +00:00
|
|
|
fn get_links(response: &Response, call_id: Uuid) -> Result<(Option<Url>, Option<Url>)> {
|
2022-11-27 14:44:43 +00:00
|
|
|
let mut prev = None;
|
|
|
|
let mut next = None;
|
|
|
|
|
|
|
|
if let Some(link_header) = response.headers().get(LINK) {
|
|
|
|
let link_header = link_header.to_str()?;
|
2022-12-07 20:58:28 +00:00
|
|
|
trace!(link_header = link_header, call_id = as_debug!(call_id); "parsing link header");
|
2022-11-27 14:44:43 +00:00
|
|
|
let link_header = link_header.as_bytes();
|
2022-12-27 14:50:28 +00:00
|
|
|
let link_header: Link = parsing::from_raw_str(link_header)?;
|
2022-11-27 14:44:43 +00:00
|
|
|
for value in link_header.values() {
|
|
|
|
if let Some(relations) = value.rel() {
|
|
|
|
if relations.contains(&RelationType::Next) {
|
2022-12-05 13:52:48 +00:00
|
|
|
// next = Some(Url::parse(value.link())?);
|
|
|
|
next = if let Ok(url) = Url::parse(value.link()) {
|
2022-12-07 21:20:20 +00:00
|
|
|
trace!(next = url.as_str(), call_id = as_debug!(call_id); "parsed link header");
|
2022-12-05 13:52:48 +00:00
|
|
|
Some(url)
|
|
|
|
} else {
|
|
|
|
// HACK: url::ParseError::into isn't working for some reason.
|
|
|
|
return Err(format_err!("error parsing url {:?}", value.link()));
|
|
|
|
};
|
2022-11-27 14:44:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if relations.contains(&RelationType::Prev) {
|
2022-12-05 13:52:48 +00:00
|
|
|
prev = if let Ok(url) = Url::parse(value.link()) {
|
2022-12-07 21:20:20 +00:00
|
|
|
trace!(prev = url.as_str(), call_id = as_debug!(call_id); "parsed link header");
|
2022-12-05 13:52:48 +00:00
|
|
|
Some(url)
|
|
|
|
} else {
|
|
|
|
// HACK: url::ParseError::into isn't working for some reason.
|
|
|
|
return Err(format_err!("error parsing url {:?}", value.link()));
|
|
|
|
};
|
2022-11-27 14:44:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((prev, next))
|
|
|
|
}
|