2020-05-05 04:26:37 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-05-31 14:56:49 +01:00
|
|
|
using System.Threading;
|
2020-05-05 04:26:37 +01:00
|
|
|
using System.Threading.Tasks;
|
2020-05-01 19:05:28 +01:00
|
|
|
using SpotifyAPI.Web.Http;
|
2020-05-31 14:56:49 +01:00
|
|
|
using System.Runtime.CompilerServices;
|
2020-05-01 19:05:28 +01:00
|
|
|
|
|
|
|
namespace SpotifyAPI.Web
|
|
|
|
{
|
|
|
|
public class SpotifyClient : ISpotifyClient
|
|
|
|
{
|
2020-05-02 12:04:26 +01:00
|
|
|
private readonly IAPIConnector _apiConnector;
|
2020-05-01 19:05:28 +01:00
|
|
|
|
|
|
|
public SpotifyClient(string token, string tokenType = "Bearer") :
|
2020-05-02 12:04:26 +01:00
|
|
|
this(SpotifyClientConfig.CreateDefault(token, tokenType))
|
2020-05-01 19:05:28 +01:00
|
|
|
{ }
|
|
|
|
|
2020-05-02 12:04:26 +01:00
|
|
|
public SpotifyClient(SpotifyClientConfig config)
|
2020-05-01 19:05:28 +01:00
|
|
|
{
|
2020-05-02 12:04:26 +01:00
|
|
|
Ensure.ArgumentNotNull(config, nameof(config));
|
2020-05-12 15:35:59 +01:00
|
|
|
if (config.Authenticator == null)
|
|
|
|
{
|
|
|
|
throw new NullReferenceException("Authenticator in config is null. Please supply it via `WithAuthenticator` or `WithToken`");
|
|
|
|
}
|
2020-05-01 19:05:28 +01:00
|
|
|
|
2020-05-12 15:35:59 +01:00
|
|
|
_apiConnector = new APIConnector(
|
|
|
|
config.BaseAddress,
|
|
|
|
config.Authenticator,
|
|
|
|
config.JSONSerializer,
|
|
|
|
config.HTTPClient,
|
|
|
|
config.RetryHandler,
|
|
|
|
config.HTTPLogger
|
|
|
|
);
|
2020-05-30 22:20:42 +01:00
|
|
|
_apiConnector.ResponseReceived += (sender, response) =>
|
|
|
|
{
|
|
|
|
LastResponse = response;
|
|
|
|
};
|
|
|
|
|
2020-05-11 17:43:52 +01:00
|
|
|
DefaultPaginator = config.DefaultPaginator;
|
2020-05-01 19:05:28 +01:00
|
|
|
UserProfile = new UserProfileClient(_apiConnector);
|
|
|
|
Browse = new BrowseClient(_apiConnector);
|
2020-05-02 21:48:21 +01:00
|
|
|
Shows = new ShowsClient(_apiConnector);
|
|
|
|
Playlists = new PlaylistsClient(_apiConnector);
|
2020-05-03 21:34:03 +01:00
|
|
|
Search = new SearchClient(_apiConnector);
|
2020-05-04 22:02:53 +01:00
|
|
|
Follow = new FollowClient(_apiConnector);
|
2020-05-04 22:04:59 +01:00
|
|
|
Tracks = new TracksClient(_apiConnector);
|
2020-05-07 17:03:20 +01:00
|
|
|
Player = new PlayerClient(_apiConnector);
|
2020-05-07 21:33:29 +01:00
|
|
|
Albums = new AlbumsClient(_apiConnector);
|
2020-05-08 10:10:53 +01:00
|
|
|
Artists = new ArtistsClient(_apiConnector);
|
2020-05-08 11:09:59 +01:00
|
|
|
Personalization = new PersonalizationClient(_apiConnector);
|
2020-05-11 17:43:52 +01:00
|
|
|
Episodes = new EpisodesClient(_apiConnector);
|
2020-05-20 07:48:08 +01:00
|
|
|
Library = new LibraryClient(_apiConnector);
|
2020-05-01 19:05:28 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 04:26:37 +01:00
|
|
|
public IPaginator DefaultPaginator { get; }
|
|
|
|
|
2020-05-01 19:05:28 +01:00
|
|
|
public IUserProfileClient UserProfile { get; }
|
|
|
|
|
|
|
|
public IBrowseClient Browse { get; }
|
2020-05-02 21:48:21 +01:00
|
|
|
|
|
|
|
public IShowsClient Shows { get; }
|
|
|
|
|
|
|
|
public IPlaylistsClient Playlists { get; }
|
2020-05-03 21:34:03 +01:00
|
|
|
|
|
|
|
public ISearchClient Search { get; }
|
2020-05-04 22:02:53 +01:00
|
|
|
|
|
|
|
public IFollowClient Follow { get; }
|
2020-05-04 22:04:59 +01:00
|
|
|
|
|
|
|
public ITracksClient Tracks { get; }
|
2020-05-05 04:26:37 +01:00
|
|
|
|
2020-05-07 17:03:20 +01:00
|
|
|
public IPlayerClient Player { get; }
|
|
|
|
|
2020-05-07 21:33:29 +01:00
|
|
|
public IAlbumsClient Albums { get; }
|
|
|
|
|
2020-05-08 10:10:53 +01:00
|
|
|
public IArtistsClient Artists { get; }
|
|
|
|
|
2020-05-08 11:09:59 +01:00
|
|
|
public IPersonalizationClient Personalization { get; }
|
|
|
|
|
2020-05-11 17:43:52 +01:00
|
|
|
public IEpisodesClient Episodes { get; }
|
|
|
|
|
2020-05-20 07:48:08 +01:00
|
|
|
public ILibraryClient Library { get; }
|
|
|
|
|
2020-05-30 22:20:42 +01:00
|
|
|
public IResponse? LastResponse { get; private set; }
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Fetches all pages and returns them grouped in a list.
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="firstPage">The first page, will be included in the output list!</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <returns>A list containing all fetched pages</returns>
|
|
|
|
public Task<IList<T>> PaginateAll<T>(Paging<T> firstPage, IPaginator? paginator = null)
|
2020-05-05 04:26:37 +01:00
|
|
|
{
|
2020-05-31 14:56:49 +01:00
|
|
|
return (paginator ?? DefaultPaginator).PaginateAll(firstPage, _apiConnector);
|
2020-05-05 04:26:37 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Fetches all pages and returns them grouped in a list.
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="getFirstPage">A function to retrive the first page, will be included in the output list!</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <returns>A list containing all fetched pages</returns>
|
|
|
|
public async Task<IList<T>> PaginateAll<T>(Func<Task<Paging<T>>> getFirstPage, IPaginator? paginator = null)
|
2020-05-20 19:59:11 +01:00
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(getFirstPage, nameof(getFirstPage));
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
var firstPage = await getFirstPage().ConfigureAwait(false);
|
|
|
|
return await (paginator ?? DefaultPaginator).PaginateAll(firstPage, _apiConnector).ConfigureAwait(false);
|
2020-05-20 19:59:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Fetches all pages and returns them grouped in a list.
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="firstPageTask">A task to retrive the first page, will be included in the output list!</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <returns>A list containing all fetched pages</returns>
|
|
|
|
public async Task<IList<T>> PaginateAll<T>(Task<Paging<T>> firstPageTask, IPaginator? paginator = null)
|
2020-05-20 19:59:11 +01:00
|
|
|
{
|
2020-05-31 14:56:49 +01:00
|
|
|
Ensure.ArgumentNotNull(firstPageTask, nameof(firstPageTask));
|
2020-05-20 19:59:11 +01:00
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
var firstPage = await firstPageTask.ConfigureAwait(false);
|
|
|
|
return await (paginator ?? DefaultPaginator).PaginateAll(firstPage, _apiConnector).ConfigureAwait(false);
|
2020-05-20 19:59:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Fetches all pages and returns them grouped in a list.
|
|
|
|
/// Some responses (e.g search response) have the pagination nested in a JSON Property.
|
|
|
|
/// To workaround this limitation, the mapper is required and needs to point to the correct next pagination.
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="firstPage">A first page, will be included in the output list!</param>
|
|
|
|
/// <param name="mapper">A function which maps response objects to the next paging object</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <returns>A list containing all fetched pages</returns>
|
2020-05-30 22:20:42 +01:00
|
|
|
public Task<IList<T>> PaginateAll<T, TNext>(
|
2020-05-20 19:59:11 +01:00
|
|
|
Paging<T, TNext> firstPage,
|
2020-05-31 14:56:49 +01:00
|
|
|
Func<TNext, Paging<T, TNext>> mapper,
|
|
|
|
IPaginator? paginator = null
|
2020-05-20 19:59:11 +01:00
|
|
|
)
|
|
|
|
{
|
2020-05-31 14:56:49 +01:00
|
|
|
return (paginator ?? DefaultPaginator).PaginateAll(firstPage, mapper, _apiConnector);
|
2020-05-20 19:59:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Fetches all pages and returns them grouped in a list.
|
|
|
|
/// Some responses (e.g search response) have the pagination nested in a JSON Property.
|
|
|
|
/// To workaround this limitation, the mapper is required and needs to point to the correct next pagination.
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="getFirstPage">A function to retrive the first page, will be included in the output list!</param>
|
|
|
|
/// <param name="mapper">A function which maps response objects to the next paging object</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <typeparam name="TNext">The Response-Type</typeparam>
|
|
|
|
/// <returns></returns>
|
2020-05-30 22:20:42 +01:00
|
|
|
public async Task<IList<T>> PaginateAll<T, TNext>(
|
2020-05-20 19:59:11 +01:00
|
|
|
Func<Task<Paging<T, TNext>>> getFirstPage,
|
2020-05-31 14:56:49 +01:00
|
|
|
Func<TNext, Paging<T, TNext>> mapper,
|
|
|
|
IPaginator? paginator = null
|
2020-05-20 19:59:11 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(getFirstPage, nameof(getFirstPage));
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
var firstPage = await getFirstPage().ConfigureAwait(false);
|
|
|
|
return await (paginator ?? DefaultPaginator).PaginateAll(firstPage, mapper, _apiConnector).ConfigureAwait(false);
|
2020-05-20 19:59:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Fetches all pages and returns them grouped in a list.
|
|
|
|
/// Some responses (e.g search response) have the pagination nested in a JSON Property.
|
|
|
|
/// To workaround this limitation, the mapper is required and needs to point to the correct next pagination.
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="firstPageTask">A Task to retrive the first page, will be included in the output list!</param>
|
|
|
|
/// <param name="mapper">A function which maps response objects to the next paging object</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <typeparam name="TNext">The Response-Type</typeparam>
|
|
|
|
/// <returns></returns>
|
2020-05-30 22:20:42 +01:00
|
|
|
public async Task<IList<T>> PaginateAll<T, TNext>(
|
2020-05-31 14:56:49 +01:00
|
|
|
Task<Paging<T, TNext>> firstPageTask,
|
2020-05-20 19:59:11 +01:00
|
|
|
Func<TNext, Paging<T, TNext>> mapper,
|
2020-05-31 14:56:49 +01:00
|
|
|
IPaginator? paginator = null
|
2020-05-20 19:59:11 +01:00
|
|
|
)
|
|
|
|
{
|
2020-05-31 14:56:49 +01:00
|
|
|
Ensure.ArgumentNotNull(firstPageTask, nameof(firstPageTask));
|
2020-05-20 19:59:11 +01:00
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
var firstPage = await firstPageTask.ConfigureAwait(false);
|
|
|
|
return await (paginator ?? DefaultPaginator).PaginateAll(firstPage, mapper, _apiConnector).ConfigureAwait(false);
|
2020-05-20 19:59:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if NETSTANDARD2_1
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Paginate through pages by using IAsyncEnumerable, introduced in C# 8
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="firstPage">A first page, will be included in the output list!</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <param name="cancellationToken">An optional Cancellation Token</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <returns>An iterable IAsyncEnumerable</returns>
|
|
|
|
public IAsyncEnumerable<T> Paginate<T>(
|
|
|
|
Paging<T> firstPage,
|
|
|
|
IPaginator? paginator = null,
|
|
|
|
CancellationToken cancellationToken = default
|
|
|
|
)
|
2020-05-05 04:26:37 +01:00
|
|
|
{
|
2020-05-31 14:56:49 +01:00
|
|
|
return (paginator ?? DefaultPaginator).Paginate(firstPage, _apiConnector, cancellationToken);
|
2020-05-05 04:26:37 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Paginate through pages by using IAsyncEnumerable, introduced in C# 8
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="getFirstPage">A Function to retrive the first page, will be included in the output list!</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <param name="cancellationToken">An optional Cancellation Token</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <returns>An iterable IAsyncEnumerable</returns>
|
|
|
|
public async IAsyncEnumerable<T> Paginate<T>(
|
|
|
|
Func<Task<Paging<T>>> getFirstPage,
|
|
|
|
IPaginator? paginator = null,
|
|
|
|
[EnumeratorCancellation] CancellationToken cancellationToken = default
|
|
|
|
)
|
2020-05-05 04:26:37 +01:00
|
|
|
{
|
2020-05-31 14:56:49 +01:00
|
|
|
|
2020-05-20 19:59:11 +01:00
|
|
|
Ensure.ArgumentNotNull(getFirstPage, nameof(getFirstPage));
|
|
|
|
|
|
|
|
var firstPage = await getFirstPage().ConfigureAwait(false);
|
2020-05-31 14:56:49 +01:00
|
|
|
await foreach (var item in (paginator ?? DefaultPaginator)
|
|
|
|
.Paginate(firstPage, _apiConnector)
|
|
|
|
.WithCancellation(cancellationToken)
|
|
|
|
)
|
2020-05-20 19:59:11 +01:00
|
|
|
{
|
|
|
|
yield return item;
|
|
|
|
}
|
2020-05-05 04:26:37 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Paginate through pages by using IAsyncEnumerable, introduced in C# 8
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="firstPageTask">A Task to retrive the first page, will be included in the output list!</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <param name="cancellationToken">An optional Cancellation Token</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <returns>An iterable IAsyncEnumerable</returns>
|
|
|
|
public async IAsyncEnumerable<T> Paginate<T>(
|
|
|
|
Task<Paging<T>> firstPageTask,
|
|
|
|
IPaginator? paginator = null,
|
|
|
|
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
2020-05-05 16:52:23 +01:00
|
|
|
{
|
2020-05-31 14:56:49 +01:00
|
|
|
Ensure.ArgumentNotNull(firstPageTask, nameof(firstPageTask));
|
2020-05-20 19:59:11 +01:00
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
var firstPage = await firstPageTask.ConfigureAwait(false);
|
|
|
|
await foreach (var item in (paginator ?? DefaultPaginator)
|
|
|
|
.Paginate(firstPage, _apiConnector)
|
|
|
|
.WithCancellation(cancellationToken)
|
|
|
|
)
|
2020-05-20 19:59:11 +01:00
|
|
|
{
|
|
|
|
yield return item;
|
|
|
|
}
|
2020-05-05 16:52:23 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Paginate through pages by using IAsyncEnumerable, introduced in C# 8
|
|
|
|
/// Some responses (e.g search response) have the pagination nested in a JSON Property.
|
|
|
|
/// To workaround this limitation, the mapper is required and needs to point to the correct next pagination.
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="firstPage">A first page, will be included in the output list!</param>
|
|
|
|
/// <param name="mapper">A function which maps response objects to the next paging object</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <param name="cancellationToken">An optional Cancellation Token</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <typeparam name="TNext">The Response-Type</typeparam>
|
|
|
|
/// <returns></returns>
|
2020-05-20 19:59:11 +01:00
|
|
|
public IAsyncEnumerable<T> Paginate<T, TNext>(
|
|
|
|
Paging<T, TNext> firstPage,
|
|
|
|
Func<TNext, Paging<T, TNext>> mapper,
|
2020-05-31 14:56:49 +01:00
|
|
|
IPaginator? paginator = null,
|
|
|
|
CancellationToken cancellationToken = default
|
2020-05-20 19:59:11 +01:00
|
|
|
)
|
|
|
|
{
|
2020-05-31 14:56:49 +01:00
|
|
|
return (paginator ?? DefaultPaginator).Paginate(firstPage, mapper, _apiConnector, cancellationToken);
|
2020-05-20 19:59:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Paginate through pages by using IAsyncEnumerable, introduced in C# 8
|
|
|
|
/// Some responses (e.g search response) have the pagination nested in a JSON Property.
|
|
|
|
/// To workaround this limitation, the mapper is required and needs to point to the correct next pagination.
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="getFirstPage">A Function to retrive the first page, will be included in the output list!</param>
|
|
|
|
/// <param name="mapper">A function which maps response objects to the next paging object</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <param name="cancellationToken">An optional Cancellation Token</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <typeparam name="TNext">The Response-Type</typeparam>
|
|
|
|
/// <returns></returns>
|
2020-05-20 19:59:11 +01:00
|
|
|
public async IAsyncEnumerable<T> Paginate<T, TNext>(
|
|
|
|
Func<Task<Paging<T, TNext>>> getFirstPage,
|
2020-05-31 14:56:49 +01:00
|
|
|
Func<TNext, Paging<T, TNext>> mapper,
|
|
|
|
IPaginator? paginator = null,
|
|
|
|
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
2020-05-20 19:59:11 +01:00
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(getFirstPage, nameof(getFirstPage));
|
|
|
|
|
|
|
|
var firstPage = await getFirstPage().ConfigureAwait(false);
|
2020-05-31 14:56:49 +01:00
|
|
|
await foreach (var item in (paginator ?? DefaultPaginator)
|
|
|
|
.Paginate(firstPage, mapper, _apiConnector)
|
|
|
|
.WithCancellation(cancellationToken)
|
|
|
|
)
|
2020-05-20 19:59:11 +01:00
|
|
|
{
|
|
|
|
yield return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Paginate through pages by using IAsyncEnumerable, introduced in C# 8
|
|
|
|
/// Some responses (e.g search response) have the pagination nested in a JSON Property.
|
|
|
|
/// To workaround this limitation, the mapper is required and needs to point to the correct next pagination.
|
|
|
|
/// The default paginator will fetch all available resources without a delay between requests.
|
|
|
|
/// This can drain your request limit quite fast, so consider using a custom paginator with delays.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="firstPageTask">A Task to retrive the first page, will be included in the output list!</param>
|
|
|
|
/// <param name="mapper">A function which maps response objects to the next paging object</param>
|
|
|
|
/// <param name="paginator">Optional. If not supplied, DefaultPaginator will be used</param>
|
|
|
|
/// <param name="cancellationToken">An optional Cancellation Token</param>
|
|
|
|
/// <typeparam name="T">The Paging-Type</typeparam>
|
|
|
|
/// <typeparam name="TNext">The Response-Type</typeparam>
|
|
|
|
/// <returns></returns>
|
2020-05-20 19:59:11 +01:00
|
|
|
public async IAsyncEnumerable<T> Paginate<T, TNext>(
|
2020-05-31 14:56:49 +01:00
|
|
|
Task<Paging<T, TNext>> firstPageTask,
|
2020-05-20 19:59:11 +01:00
|
|
|
Func<TNext, Paging<T, TNext>> mapper,
|
2020-05-31 14:56:49 +01:00
|
|
|
IPaginator? paginator = null,
|
|
|
|
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
2020-05-20 19:59:11 +01:00
|
|
|
{
|
2020-05-31 14:56:49 +01:00
|
|
|
Ensure.ArgumentNotNull(firstPageTask, nameof(firstPageTask));
|
2020-05-05 04:26:37 +01:00
|
|
|
|
2020-05-31 14:56:49 +01:00
|
|
|
var firstPage = await firstPageTask.ConfigureAwait(false);
|
|
|
|
await foreach (var item in (paginator ?? DefaultPaginator)
|
|
|
|
.Paginate(firstPage, mapper, _apiConnector)
|
|
|
|
.WithCancellation(cancellationToken)
|
|
|
|
)
|
2020-05-20 19:59:11 +01:00
|
|
|
{
|
|
|
|
yield return item;
|
|
|
|
}
|
2020-05-05 04:26:37 +01:00
|
|
|
}
|
2020-05-20 19:59:11 +01:00
|
|
|
#endif
|
2020-05-01 19:05:28 +01:00
|
|
|
}
|
|
|
|
}
|