pub struct HttpPaginator<T, F, Fut> { /* private fields */ }Expand description
Call a function with an update HttpPaginationOptions for each batch request,
simplifying lazy access to large response sets such as messages etc.
Implementations§
Source§impl<T, F, Fut> HttpPaginator<T, F, Fut>
impl<T, F, Fut> HttpPaginator<T, F, Fut>
Sourcepub fn new(http_fn: F, pagination: HttpPaginationOptions) -> Self
pub fn new(http_fn: F, pagination: HttpPaginationOptions) -> Self
Create the paginator with the http batch generator.
§Example
use sms_client::Client;
use sms_client::config::ClientConfig;
use sms_client::http::paginator::HttpPaginator;
use sms_client::types::http::HttpPaginationOptions;
let http = Client::new(ClientConfig::http_only("http://localhost:3000").with_auth("token!"))?.http_arc();
let mut paginator = HttpPaginator::new(
move |pagination| {
let http = http.expect("Missing HTTP client configuration!").clone();
async move {
http.get_latest_numbers(pagination).await
}
},
HttpPaginationOptions::default()
.with_limit(10) // Do it in batches of 10.
.with_offset(10) // Skip the first 10 results.
.with_reverse(true) // Reverse the results set.
);Sourcepub fn with_defaults(http_fn: F) -> Self
pub fn with_defaults(http_fn: F) -> Self
Create a paginator with default pagination settings. This starts at offset 0 with a limit of 50 per page.
§Example
use sms_client::Client;
use sms_client::config::ClientConfig;
use sms_client::http::HttpClient;
use sms_client::http::paginator::HttpPaginator;
/// View all latest numbers, in a default paginator with a limit of 50 per chunk.
async fn view_all_latest_numbers(http: HttpClient) {
let mut paginator = HttpPaginator::with_defaults(|pagination| {
http.get_latest_numbers(pagination)
});
while let Some(message) = paginator.next().await {
log::info!("{:?}", message);
}
}Sourcepub async fn next(&mut self) -> Option<T>
pub async fn next(&mut self) -> Option<T>
Get the next item, automatically fetching next pages as needed.
§Example
use sms_client::http::HttpClient;
use sms_client::http::paginator::HttpPaginator;
async fn get_delivery_reports(message_id: i64, http: HttpClient) {
let mut paginator = HttpPaginator::with_defaults(|pagination| {
http.get_delivery_reports(message_id, pagination)
}).await;
/// Iterate through ALL messages, with a page size of 50 (default).
while let Some(message) = paginator.next().await {
log::info!("{:?}", message);
}
}Sourcepub async fn collect_all(self) -> HttpResult<Vec<T>>
pub async fn collect_all(self) -> HttpResult<Vec<T>>
Collect all remaining items into a Vec. This continues to request batches until empty.
Sourcepub async fn take(self, n: usize) -> HttpResult<Vec<T>>
pub async fn take(self, n: usize) -> HttpResult<Vec<T>>
Process items in chunks, calling the provided closure for each chunk.
Sourcepub async fn for_each_chuck<C>(
self,
chunk_size: usize,
chunk_fn: C,
) -> HttpResult<()>
pub async fn for_each_chuck<C>( self, chunk_size: usize, chunk_fn: C, ) -> HttpResult<()>
Process items in chunks, calling the provided closure for each chunk.
§Example
use std::sync::Arc;
use sms_client::http::HttpClient;
use sms_client::http::paginator::HttpPaginator;
use sms_client::types::http::HttpPaginationOptions;
/// Read all messages from a phone number, in chunks of 10.
async fn read_all_messages(phone_number: &str, http: Arc<HttpClient>) {
let paginator = HttpPaginator::with_defaults(|pagination| {
http.get_messages(phone_number, pagination)
}).await;
paginator.for_each_chuck(10, |batch| {
for message in batch {
log::info!("{:?}", message);
}
}).await?;
}Sourcepub fn current_pagination(&self) -> &HttpPaginationOptions
pub fn current_pagination(&self) -> &HttpPaginationOptions
Get the current pagination options state.
Auto Trait Implementations§
impl<T, F, Fut> Freeze for HttpPaginator<T, F, Fut>where
F: Freeze,
impl<T, F, Fut> RefUnwindSafe for HttpPaginator<T, F, Fut>
impl<T, F, Fut> Send for HttpPaginator<T, F, Fut>
impl<T, F, Fut> Sync for HttpPaginator<T, F, Fut>
impl<T, F, Fut> Unpin for HttpPaginator<T, F, Fut>
impl<T, F, Fut> UnwindSafe for HttpPaginator<T, F, Fut>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more