public sealed class PageStreamer<TResource, TRequest, TResponse, TToken>
where TRequest : IClientServiceRequest<TResponse> where TToken : class
A page streamer is a helper to provide both synchronous and asynchronous page streaming of a listable or queryable resource.
Namespace
Google.Apis.RequestsAssembly
Google.Apis.dll
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.
Example
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>>)
public PageStreamer(Action<TRequest, TToken> requestModifier, Func<TResponse, TToken> tokenExtractor, Func<TResponse, IEnumerable<TResource>> resourceExtractor)
Creates a paginator for later use.
Parameters | |
---|---|
Name | Description |
requestModifier | Action<TRequest, TToken> Action to modify a request to include the specified page token. Must not be null. |
tokenExtractor | Func<TResponse, TToken> Function to extract the next page token from a response. Must not be null. |
resourceExtractor | Func<TResponse, IEnumerable<TResource>> 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)
public IEnumerable<TResource> Fetch(TRequest request)
Lazily fetches resources a page at a time.
Parameter | |
---|---|
Name | Description |
request | TRequest 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 |
IEnumerable<TResource> | A sequence of resources, which are fetched a page at a time. Must not be null. |
FetchAllAsync(TRequest, CancellationToken)
public Task<IList<TResource>> FetchAllAsync(TRequest request, CancellationToken cancellationToken)
Asynchronously (but eagerly) fetches a complete set of resources, potentially making multiple requests.
Parameters | |
---|---|
Name | Description |
request | TRequest 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. |
cancellationToken | CancellationToken |
Returns | |
---|---|
Type | Description |
Task<IList<TResource>> | A sequence of resources, which are fetched asynchronously and a page at a time. |