Show / Hide Table of Contents

Class PageStreamer<TResource, TRequest, TResponse, TToken>

A page streamer is a helper to provide both synchronous and asynchronous page streaming of a listable or queryable resource.

Inheritance
System.Object
PageStreamer<TResource, TRequest, TResponse, TToken>
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Google.Apis.Requests
Assembly: Google.Apis.dll
Syntax
public sealed class PageStreamer<TResource, TRequest, TResponse, TToken>
    where TRequest : IClientServiceRequest<TResponse> where TToken : class
Type Parameters
Name Description
TResource

The type of resource being paginated

TRequest

The type of request used to fetch pages

TResponse

The type of response obtained when fetching pages

TToken

The type of the "next page token", which must be a reference type; a null reference for a token indicates the end of a stream of pages.

Remarks

The expected usage pattern is to create a single paginator for a resource collection, and then use the instance methods to obtain paginated results.

Examples

To construct a page streamer to return snippets from the YouTube v3 Data API, you might use code such as the following. The pattern for other APIs would be very similar, with the request.PageToken, response.NextPageToken and response.Items properties potentially having different names. Constructing the page streamer doesn't require any service references or authentication, so it's completely safe to perform this in a type initializer.

using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
...
private static readonly snippetPageStreamer = new PageStreamer<SearchResult, SearchResource.ListRequest, SearchListResponse, string>(
    (request, token) => request.PageToken = token,
    response => response.NextPageToken,
    response => response.Items);

Constructors

PageStreamer(Action<TRequest, TToken>, Func<TResponse, TToken>, Func<TResponse, IEnumerable<TResource>>)

Creates a paginator for later use.

Declaration
public PageStreamer(Action<TRequest, TToken> requestModifier, Func<TResponse, TToken> tokenExtractor, Func<TResponse, IEnumerable<TResource>> resourceExtractor)
Parameters
Type Name Description
System.Action<TRequest, TToken> requestModifier

Action to modify a request to include the specified page token. Must not be null.

System.Func<TResponse, TToken> tokenExtractor

Function to extract the next page token from a response. Must not be null.

System.Func<TResponse, System.Collections.Generic.IEnumerable<TResource>> resourceExtractor

Function to extract a sequence of resources from a response. Must not be null, although it can return null if it is passed a response which contains no resources.

Methods

Fetch(TRequest)

Lazily fetches resources a page at a time.

Declaration
public IEnumerable<TResource> Fetch(TRequest request)
Parameters
Type Name Description
TRequest request

The initial request to send. If this contains a page token, that token is maintained. This will be modified with new page tokens over time, and should not be changed by the caller. (The caller should clone the request if they want an independent object to use in other calls or to modify.) Must not be null.

Returns
Type Description
System.Collections.Generic.IEnumerable<TResource>

A sequence of resources, which are fetched a page at a time. Must not be null.

FetchAllAsync(TRequest, CancellationToken)

Asynchronously (but eagerly) fetches a complete set of resources, potentially making multiple requests.

Declaration
public async Task<IList<TResource>> FetchAllAsync(TRequest request, CancellationToken cancellationToken)
Parameters
Type Name Description
TRequest request

The initial request to send. If this contains a page token, that token is maintained. This will be modified with new page tokens over time, and should not be changed by the caller. (The caller should clone the request if they want an independent object to use in other calls or to modify.) Must not be null.

System.Threading.CancellationToken cancellationToken
Returns
Type Description
System.Threading.Tasks.Task<System.Collections.Generic.IList<TResource>>

A sequence of resources, which are fetched asynchronously and a page at a time.

In This Article
Back to top