HttpPaginator

Struct HttpPaginator 

Source
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>
where F: Fn(Option<HttpPaginationOptions>) -> Fut, Fut: Future<Output = HttpResult<Vec<T>>>,

Source

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.
);
Source

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);
    }
}
Source

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);
    }
}
Source

pub async fn collect_all(self) -> HttpResult<Vec<T>>

Collect all remaining items into a Vec. This continues to request batches until empty.

Source

pub async fn take(self, n: usize) -> HttpResult<Vec<T>>

Process items in chunks, calling the provided closure for each chunk.

Source

pub async fn for_each_chuck<C>( self, chunk_size: usize, chunk_fn: C, ) -> HttpResult<()>
where C: FnMut(&[T]) -> 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?;
}
Source

pub async fn skip(self, n: usize) -> Self

Skip n items and return the paginator.

Source

pub fn current_pagination(&self) -> &HttpPaginationOptions

Get the current pagination options state.

Source

pub fn has_more(&self) -> bool

Check if there are potentially more items to fetch.

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>
where F: Send, Fut: Send, T: Send,

§

impl<T, F, Fut> Sync for HttpPaginator<T, F, Fut>
where F: Sync, Fut: Sync, T: Sync,

§

impl<T, F, Fut> Unpin for HttpPaginator<T, F, Fut>
where F: Unpin, Fut: Unpin, T: Unpin,

§

impl<T, F, Fut> UnwindSafe for HttpPaginator<T, F, Fut>
where F: UnwindSafe, Fut: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,