/// <summary> /// Applies the action on each value in the paginated response. /// </summary> /// <typeparam name="TType">The type of the response.</typeparam> /// <param name="getFirstPage">The function that returns the first page.</param> /// <param name="getNextPage">The function that returns subsequent pages.</param> /// <param name="cancellationToken">The cancellation token.</param> public static TType[] Enumerate <TType>( Func <Task <ResponseWithContinuation <TType[]> > > getFirstPage, Func <string, Task <ResponseWithContinuation <TType[]> > > getNextPage, CancellationToken?cancellationToken) { var result = new List <TType>(); ResponseWithContinuation <TType[]> batch = null; while ((cancellationToken.HasValue && !cancellationToken.Value.IsCancellationRequested) && (batch == null || !string.IsNullOrWhiteSpace(batch.NextLink))) { cancellationToken.Value.ThrowIfCancellationRequested(); batch = batch == null ? getFirstPage().Result : getNextPage(batch.NextLink).Result; if (batch == null) { return(result.ToArray()); } result.AddRange(batch.Value.Coalesce()); } return(result.ToArray()); }
/// <summary> /// Applies the action on each value in the paginated response. /// </summary> /// <typeparam name="TType">The type of the response.</typeparam> /// <param name="getFirstPage">The function that returns the first page.</param> /// <param name="getNextPage">The function that returns subsequent pages.</param> /// <param name="action">The action to apply.</param> /// <param name="cancellationToken">The cancellation token.</param> public static void ForEach <TType>( Func <Task <ResponseWithContinuation <TType[]> > > getFirstPage, Func <string, Task <ResponseWithContinuation <TType[]> > > getNextPage, CancellationToken?cancellationToken, Action <TType[]> action) { ResponseWithContinuation <TType[]> batch = null; while ((cancellationToken.HasValue && !cancellationToken.Value.IsCancellationRequested) && (batch == null || !string.IsNullOrWhiteSpace(batch.NextLink))) { cancellationToken.Value.ThrowIfCancellationRequested(); batch = batch == null ? getFirstPage().Result : getNextPage(batch.NextLink).Result; if (batch == null) { return; } action(batch.Value); } }