/*
Copyright 2016 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Google.Apis.Requests
{
// TODO(jskeet): Make sure one of our samples uses this.
///
/// A page streamer is a helper to provide both synchronous and asynchronous page streaming
/// of a listable or queryable resource.
///
///
///
/// The expected usage pattern is to create a single paginator for a resource collection,
/// and then use the instance methods to obtain paginated results.
///
///
///
/// 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.
/// (
/// (request, token) => request.PageToken = token,
/// response => response.NextPageToken,
/// response => response.Items);
/// ]]>
///
/// The type of resource being paginated
/// The type of request used to fetch pages
/// The type of response obtained when fetching pages
/// 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.
public sealed class PageStreamer
where TToken : class
where TRequest : IClientServiceRequest
{
// Simple way of avoiding NullReferenceException if the response extractor returns null.
private static readonly TResource[] emptyResources = new TResource[0];
private readonly Action requestModifier;
private readonly Func tokenExtractor;
private readonly Func> resourceExtractor;
///
/// Creates a paginator for later use.
///
/// Action to modify a request to include the specified page token.
/// Must not be null.
/// Function to extract the next page token from a response.
/// Must not be null.
/// 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.
public PageStreamer(
Action requestModifier,
Func tokenExtractor,
Func> resourceExtractor)
{
if (requestModifier == null)
{
throw new ArgumentNullException("requestProvider");
}
if (tokenExtractor == null)
{
throw new ArgumentNullException("tokenExtractor");
}
if (resourceExtractor == null)
{
throw new ArgumentNullException("resourceExtractor");
}
this.requestModifier = requestModifier;
this.tokenExtractor = tokenExtractor;
this.resourceExtractor = resourceExtractor;
}
///
/// Lazily fetches resources a page at a time.
///
/// 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.
/// A sequence of resources, which are fetched a page at a time. Must not be null.
public IEnumerable Fetch(TRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
TToken token;
do
{
TResponse response = request.Execute();
token = tokenExtractor(response);
requestModifier(request, token);
foreach (var item in resourceExtractor(response) ?? emptyResources)
{
yield return item;
}
} while (token != null);
}
///
/// Asynchronously (but eagerly) fetches a complete set of resources, potentially making multiple requests.
///
/// 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.
/// A sequence of resources, which are fetched asynchronously and a page at a time.
///
/// A task whose result (when complete) is the complete set of results fetched starting with the given
/// request, and continuing to make further requests until a response has no "next page" token.
public async Task> FetchAllAsync(
TRequest request,
CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
var results = new List();
TToken token;
do
{
cancellationToken.ThrowIfCancellationRequested();
TResponse response = await request.ExecuteAsync(cancellationToken).ConfigureAwait(false);
token = tokenExtractor(response);
requestModifier(request, token);
results.AddRange(resourceExtractor(response) ?? emptyResources);
} while (token != null);
return results;
}
}
}