2020-05-05 04:26:37 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using SpotifyAPI.Web.Http;
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web
|
|
|
|
{
|
|
|
|
public class SimplePaginator : IPaginator
|
|
|
|
{
|
2020-05-05 16:52:23 +01:00
|
|
|
protected virtual Task<bool> ShouldContinue<T>(List<T> results, Paging<T> page)
|
2020-05-05 04:26:37 +01:00
|
|
|
{
|
2020-05-05 16:52:23 +01:00
|
|
|
return Task.FromResult(true);
|
|
|
|
}
|
|
|
|
protected virtual Task<bool> ShouldContinue<T, TNext>(List<T> results, Paging<T, TNext> page)
|
|
|
|
{
|
|
|
|
return Task.FromResult(true);
|
2020-05-05 04:26:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<List<T>> Paginate<T>(Paging<T> firstPage, IAPIConnector connector)
|
|
|
|
{
|
2020-05-05 04:45:33 +01:00
|
|
|
Ensure.ArgumentNotNull(firstPage, nameof(firstPage));
|
|
|
|
Ensure.ArgumentNotNull(connector, nameof(connector));
|
|
|
|
|
2020-05-05 04:26:37 +01:00
|
|
|
var page = firstPage;
|
|
|
|
var results = new List<T>();
|
|
|
|
results.AddRange(firstPage.Items);
|
2020-05-05 16:52:23 +01:00
|
|
|
while (page.Next != null && await ShouldContinue(results, page).ConfigureAwait(false))
|
2020-05-05 04:26:37 +01:00
|
|
|
{
|
|
|
|
page = await connector.Get<Paging<T>>(new Uri(page.Next, UriKind.Absolute)).ConfigureAwait(false);
|
|
|
|
results.AddRange(page.Items);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<List<T>> Paginate<T>(Func<Task<Paging<T>>> getFirstPage, IAPIConnector connector)
|
|
|
|
{
|
2020-05-05 04:45:33 +01:00
|
|
|
Ensure.ArgumentNotNull(getFirstPage, nameof(getFirstPage));
|
|
|
|
|
2020-05-05 04:26:37 +01:00
|
|
|
var firstPage = await getFirstPage().ConfigureAwait(false);
|
|
|
|
return await Paginate(firstPage, connector).ConfigureAwait(false);
|
|
|
|
}
|
2020-05-05 16:52:23 +01:00
|
|
|
|
|
|
|
public async Task<List<T>> Paginate<T, TNext>(
|
|
|
|
Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper, IAPIConnector connector
|
|
|
|
)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(firstPage, nameof(firstPage));
|
|
|
|
Ensure.ArgumentNotNull(mapper, nameof(mapper));
|
|
|
|
Ensure.ArgumentNotNull(connector, nameof(connector));
|
|
|
|
|
|
|
|
var page = firstPage;
|
|
|
|
var results = new List<T>();
|
|
|
|
results.AddRange(firstPage.Items);
|
|
|
|
while (page.Next != null && await ShouldContinue(results, page).ConfigureAwait(false))
|
|
|
|
{
|
|
|
|
var next = await connector.Get<TNext>(new Uri(page.Next, UriKind.Absolute)).ConfigureAwait(false);
|
|
|
|
page = mapper(next);
|
|
|
|
results.AddRange(page.Items);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<List<T>> Paginate<T, TNext>(Func<Task<Paging<T, TNext>>> getFirstPage, Func<TNext, Paging<T, TNext>> mapper, IAPIConnector connector)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(getFirstPage, nameof(getFirstPage));
|
|
|
|
|
|
|
|
var firstPage = await getFirstPage().ConfigureAwait(false);
|
|
|
|
return await Paginate(firstPage, mapper, connector).ConfigureAwait(false);
|
|
|
|
}
|
2020-05-05 04:26:37 +01:00
|
|
|
}
|
|
|
|
}
|