use std::marker::PhantomData;
use futures_util::stream;
pub use futures_util::stream::{BoxStream, StreamExt, TryStreamExt};
use serde::de::DeserializeOwned;
use crate::error::Error;
use crate::http::{HttpClient, RequestParts};
pub trait CursorPage: DeserializeOwned + Send + 'static {
type Item: Send + 'static;
fn next_cursor(&self) -> Option<&str>;
fn into_items(self) -> Vec<Self::Item>;
}
#[must_use = "a list does nothing until a page is requested or it is streamed"]
#[derive(Clone, Debug)]
pub struct Paginator<P> {
http: HttpClient,
parts: RequestParts,
cursor_param: &'static str,
cursor: Option<String>,
page: PhantomData<fn() -> P>,
}
impl<P: CursorPage> Paginator<P> {
pub fn new(
http: HttpClient,
parts: RequestParts,
cursor_param: &'static str,
cursor: Option<String>,
) -> Self {
Self {
http,
parts,
cursor_param,
cursor,
page: PhantomData,
}
}
async fn fetch(&self, cursor: Option<&str>) -> Result<P, Error> {
let parts = self.parts.clone().query_opt(self.cursor_param, cursor);
self.http.request::<P>(parts).send().await
}
pub async fn page(&self) -> Result<P, Error> {
self.fetch(self.cursor.as_deref()).await
}
pub async fn page_at(&self, cursor: &str) -> Result<P, Error> {
self.fetch(Some(cursor)).await
}
pub fn pages(self) -> BoxStream<'static, Result<P, Error>> {
enum State {
At(Option<String>),
Done,
}
let start = State::At(self.cursor.clone());
stream::try_unfold((self, start), |(list, state)| async move {
let State::At(cursor) = state else {
return Ok(None);
};
let page = list.fetch(cursor.as_deref()).await?;
let next = match page.next_cursor() {
Some(next) if !next.is_empty() => State::At(Some(next.to_owned())),
_ => State::Done,
};
Ok(Some((page, (list, next))))
})
.boxed()
}
pub fn stream(self) -> BoxStream<'static, Result<P::Item, Error>> {
self.pages()
.map_ok(|page| stream::iter(page.into_items().into_iter().map(Ok)))
.try_flatten()
.boxed()
}
pub async fn to_vec(self) -> Result<Vec<P::Item>, Error> {
self.stream().try_collect().await
}
pub async fn to_vec_max(self, max: usize) -> Result<Vec<P::Item>, Error> {
self.stream().take(max).try_collect().await
}
}