diff options
author | 2024-09-18 15:02:20 -0700 | |
---|---|---|
committer | 2024-09-18 15:02:20 -0700 | |
commit | 832966882e212987cc691f6b56fcef58fc6e4e45 (patch) | |
tree | 6824d0dcbcea662f1bd48e895684a70efc4d3c6f /src/pagination.rs | |
parent | 2e44431c9e0a2efab1906390e0597748b8d0b7c7 (diff) | |
download | trustfall-pandascore-adapter-832966882e212987cc691f6b56fcef58fc6e4e45.tar.gz trustfall-pandascore-adapter-832966882e212987cc691f6b56fcef58fc6e4e45.tar.zst trustfall-pandascore-adapter-832966882e212987cc691f6b56fcef58fc6e4e45.zip |
feat: add more entrypoints
Diffstat (limited to '')
-rw-r--r-- | src/pagination.rs | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/src/pagination.rs b/src/pagination.rs index 3e43de7..b8482e9 100644 --- a/src/pagination.rs +++ b/src/pagination.rs @@ -1,50 +1,49 @@ use std::{collections::VecDeque, sync::Arc}; use pandascore::{ - endpoint::{CollectionOptions, ListResponse}, + endpoint::{ListResponse, PaginatedEndpoint}, ClientTransport, }; use crate::adapter::AdapterInner; #[derive(Debug)] -pub struct PaginationIterator<C, T, F> { +pub struct PaginationIterator<C, E: PaginatedEndpoint> { adapter: Arc<AdapterInner<C>>, - results: VecDeque<T>, - next: Option<CollectionOptions>, - f: F, + init: Option<E>, + results: VecDeque<E::Item>, } -impl<C, T, F> PaginationIterator<C, T, F> +impl<C, E> PaginationIterator<C, E> where C: ClientTransport, - F: Fn(Arc<AdapterInner<C>>, CollectionOptions) -> Option<ListResponse<T>>, + E: PaginatedEndpoint, { - pub fn new(adapter: Arc<AdapterInner<C>>, next: CollectionOptions, f: F) -> Self { + pub fn new(adapter: Arc<AdapterInner<C>>, init: E) -> Self { Self { adapter, + init: Some(init), results: VecDeque::new(), - next: Some(next), - f, } } } -impl<C, T, F> Iterator for PaginationIterator<C, T, F> +impl<C, E, T> Iterator for PaginationIterator<C, E> where C: ClientTransport, - F: Fn(Arc<AdapterInner<C>>, CollectionOptions) -> Option<ListResponse<T>>, + E: PaginatedEndpoint<Item = T, Response = ListResponse<T>> + Clone + std::fmt::Debug, { - type Item = T; + type Item = E::Item; fn next(&mut self) -> Option<Self::Item> { if let Some(next) = self.results.pop_front() { return Some(next); } - if let Some(next) = self.next.take() { - let mut response = (self.f)(Arc::clone(&self.adapter), next)?; - self.next = response.next.take(); + if let Some(req) = self.init.take() { + let response = self.adapter.execute(req.clone())?; + + self.init = response.next.map(|opts| req.with_options(opts)); self.results = response.results.into(); self.results.pop_front() } else { |