Skip to main content

PageStream

Struct PageStream 

Source
pub struct PageStream<'a, T> { /* private fields */ }
Available on crate feature client only.
Expand description

An asynchronous crawl over every page of a list endpoint.

Following the Link: <…>; rel="next" header is only most of the job. The specification also describes what to do when the result set changes underneath the crawl:

While a client crawls over the pages … a new object might be created on the server. The client detects this: the X-Total-Count will be higher on the next call. Even so, the client does not have to retry any requests when this happens because only the last page will be different.

When there are for example 1000 objects matching a query … while crawling over the pages one of these objects is updated. The client detects this: X-Total-Count will be lower in the next request. It is advised to redo the previous GET with the offset lowered by 1 (if the offset was not 0) and after that continue crawling the ‘next’ page links.

PageStream does both, and reports the correction it made through PageStream::corrections so a pull that keeps shifting is visible rather than silent.

while let Some(location) = stream.next().await? {
    println!("{}", location.id);
}
println!("{} objects over {} pages", stream.seen(), stream.pages_fetched());

Spec: 2.3.0 §transport_and_format_paginated_response

Implementations§

Source§

impl<'a, T: DeserializeOwned> PageStream<'a, T>

Source

pub fn new( transport: &'a Transport, peer: &'a Peer, module: ModuleId, routing: RoutingHeaders, first: Url, ) -> Self

Starts a crawl at first.

Source

pub const fn bridging(self, kind: ObjectKind) -> Self

Translates every page out of the peer’s OCPI version into the canonical model.

kind says which object the endpoint carries. A peer that already speaks the canonical version costs nothing: the translation is skipped, not applied as an identity.

Source

pub const fn with_max_pages(self, max_pages: usize) -> Self

Caps how many pages this crawl will fetch.

Source

pub async fn next(&mut self) -> Result<Option<T>, OcpiError>

The next object, fetching another page when the buffer runs dry.

§Errors

Propagates transport, decoding and OCPI-level errors from the page fetch.

Source

pub async fn collect_all(self) -> Result<Vec<T>, OcpiError>

Collects the whole list.

§Errors

Propagates any error from PageStream::next.

Source

pub const fn pages_fetched(&self) -> usize

How many pages have been fetched.

Source

pub const fn seen(&self) -> usize

How many objects have been yielded.

Source

pub const fn corrections(&self) -> usize

How many times the crawl was rewound because X-Total-Count shrank.

A non-zero count means objects were changing while the crawl ran.

Source

pub const fn total_count(&self) -> Option<u64>

The total the peer reported for the query, from the most recent page.

Auto Trait Implementations§

§

impl<'a, T> !RefUnwindSafe for PageStream<'a, T>

§

impl<'a, T> !UnwindSafe for PageStream<'a, T>

§

impl<'a, T> Freeze for PageStream<'a, T>
where VecDeque<T>: Freeze,

§

impl<'a, T> Send for PageStream<'a, T>
where VecDeque<T>: Send,

§

impl<'a, T> Sync for PageStream<'a, T>
where VecDeque<T>: Sync,

§

impl<'a, T> Unpin for PageStream<'a, T>
where VecDeque<T>: Unpin,

§

impl<'a, T> UnsafeUnpin for PageStream<'a, T>

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: Sized + 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: Sized + 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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<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