From cd650a7e6db97b163954e9b22a8c64cee50664bd Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Tue, 5 May 2020 17:52:23 +0200 Subject: [PATCH] Further refined IPaginator, making it possible to do search pagination! --- .../Clients/Interfaces/IPaginator.cs | 3 ++ .../Clients/Interfaces/ISpotifyClient.cs | 2 + SpotifyAPI.Web/Clients/SimplePaginator.cs | 39 +++++++++++++++++-- SpotifyAPI.Web/Clients/SpotifyClient.cs | 10 +++++ SpotifyAPI.Web/Http/APIConnector.cs | 2 +- SpotifyAPI.Web/Models/Response/Paging.cs | 11 ++++++ .../Models/Response/SearchResponse.cs | 10 ++--- 7 files changed, 68 insertions(+), 9 deletions(-) diff --git a/SpotifyAPI.Web/Clients/Interfaces/IPaginator.cs b/SpotifyAPI.Web/Clients/Interfaces/IPaginator.cs index 0a92405f..dd806689 100644 --- a/SpotifyAPI.Web/Clients/Interfaces/IPaginator.cs +++ b/SpotifyAPI.Web/Clients/Interfaces/IPaginator.cs @@ -9,5 +9,8 @@ namespace SpotifyAPI.Web { Task> Paginate(Paging firstPage, IAPIConnector connector); Task> Paginate(Func>> getFirstPage, IAPIConnector connector); + + Task> Paginate(Paging firstPage, Func> mapper, IAPIConnector connector); + Task> Paginate(Func>> getFirstPage, Func> mapper, IAPIConnector connector); } } diff --git a/SpotifyAPI.Web/Clients/Interfaces/ISpotifyClient.cs b/SpotifyAPI.Web/Clients/Interfaces/ISpotifyClient.cs index e0c36d3a..3cb40f35 100644 --- a/SpotifyAPI.Web/Clients/Interfaces/ISpotifyClient.cs +++ b/SpotifyAPI.Web/Clients/Interfaces/ISpotifyClient.cs @@ -23,7 +23,9 @@ namespace SpotifyAPI.Web ITracksClient Tracks { get; } Task> Paginate(Paging firstPage); + Task> Paginate(Paging firstPage, Func> mapper); Task> Paginate(Func>> getFirstPage); + Task> Paginate(Func>> getFirstPage, Func> mapper); Task> Paginate(Paging firstPage, IPaginator paginator); Task> Paginate(Func>> getFirstPage, IPaginator paginator); } diff --git a/SpotifyAPI.Web/Clients/SimplePaginator.cs b/SpotifyAPI.Web/Clients/SimplePaginator.cs index 5c2a6ee6..9ed6bbc9 100644 --- a/SpotifyAPI.Web/Clients/SimplePaginator.cs +++ b/SpotifyAPI.Web/Clients/SimplePaginator.cs @@ -7,9 +7,13 @@ namespace SpotifyAPI.Web { public class SimplePaginator : IPaginator { - protected virtual bool ShouldContinue(List results, Paging page) + protected virtual Task ShouldContinue(List results, Paging page) { - return true; + return Task.FromResult(true); + } + protected virtual Task ShouldContinue(List results, Paging page) + { + return Task.FromResult(true); } public async Task> Paginate(Paging firstPage, IAPIConnector connector) @@ -20,7 +24,7 @@ namespace SpotifyAPI.Web var page = firstPage; var results = new List(); results.AddRange(firstPage.Items); - while (page.Next != null && ShouldContinue(results, page)) + while (page.Next != null && await ShouldContinue(results, page).ConfigureAwait(false)) { page = await connector.Get>(new Uri(page.Next, UriKind.Absolute)).ConfigureAwait(false); results.AddRange(page.Items); @@ -36,5 +40,34 @@ namespace SpotifyAPI.Web var firstPage = await getFirstPage().ConfigureAwait(false); return await Paginate(firstPage, connector).ConfigureAwait(false); } + + public async Task> Paginate( + Paging firstPage, Func> 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(); + results.AddRange(firstPage.Items); + while (page.Next != null && await ShouldContinue(results, page).ConfigureAwait(false)) + { + var next = await connector.Get(new Uri(page.Next, UriKind.Absolute)).ConfigureAwait(false); + page = mapper(next); + results.AddRange(page.Items); + } + + return results; + } + + public async Task> Paginate(Func>> getFirstPage, Func> mapper, IAPIConnector connector) + { + Ensure.ArgumentNotNull(getFirstPage, nameof(getFirstPage)); + + var firstPage = await getFirstPage().ConfigureAwait(false); + return await Paginate(firstPage, mapper, connector).ConfigureAwait(false); + } } } diff --git a/SpotifyAPI.Web/Clients/SpotifyClient.cs b/SpotifyAPI.Web/Clients/SpotifyClient.cs index fd10aea3..1d8d039e 100644 --- a/SpotifyAPI.Web/Clients/SpotifyClient.cs +++ b/SpotifyAPI.Web/Clients/SpotifyClient.cs @@ -49,6 +49,11 @@ namespace SpotifyAPI.Web return DefaultPaginator.Paginate(firstPage, _apiConnector); } + public Task> Paginate(Paging firstPage, Func> mapper) + { + return DefaultPaginator.Paginate(firstPage, mapper, _apiConnector); + } + public Task> Paginate(Paging firstPage, IPaginator paginator) { Ensure.ArgumentNotNull(paginator, nameof(paginator)); @@ -61,6 +66,11 @@ namespace SpotifyAPI.Web return DefaultPaginator.Paginate(getFirstPage, _apiConnector); } + public Task> Paginate(Func>> getFirstPage, Func> mapper) + { + return DefaultPaginator.Paginate(getFirstPage, mapper, _apiConnector); + } + public Task> Paginate(Func>> getFirstPage, IPaginator paginator) { Ensure.ArgumentNotNull(paginator, nameof(paginator)); diff --git a/SpotifyAPI.Web/Http/APIConnector.cs b/SpotifyAPI.Web/Http/APIConnector.cs index 03840acc..b563a247 100644 --- a/SpotifyAPI.Web/Http/APIConnector.cs +++ b/SpotifyAPI.Web/Http/APIConnector.cs @@ -158,7 +158,7 @@ namespace SpotifyAPI.Web.Http Ensure.ArgumentNotNull(uri, nameof(uri)); Ensure.ArgumentNotNull(method, nameof(method)); - return new Request(parameters) + return new Request(new Dictionary(), parameters) { BaseAddress = _baseAddress, Endpoint = uri, diff --git a/SpotifyAPI.Web/Models/Response/Paging.cs b/SpotifyAPI.Web/Models/Response/Paging.cs index 4c99ce76..1701abb2 100644 --- a/SpotifyAPI.Web/Models/Response/Paging.cs +++ b/SpotifyAPI.Web/Models/Response/Paging.cs @@ -12,4 +12,15 @@ namespace SpotifyAPI.Web public string Previous { get; private set; } public int Total { get; private set; } } + + public class Paging + { + public string Href { get; private set; } + public List Items { get; private set; } + public int Limit { get; private set; } + public string Next { get; private set; } + public int Offset { get; private set; } + public string Previous { get; private set; } + public int Total { get; private set; } + } } diff --git a/SpotifyAPI.Web/Models/Response/SearchResponse.cs b/SpotifyAPI.Web/Models/Response/SearchResponse.cs index 5488fa0c..7273ff9e 100644 --- a/SpotifyAPI.Web/Models/Response/SearchResponse.cs +++ b/SpotifyAPI.Web/Models/Response/SearchResponse.cs @@ -2,10 +2,10 @@ namespace SpotifyAPI.Web { public class SearchResponse { - public Paging Artists { get; private set; } - public Paging Albums { get; private set; } - public Paging Tracks { get; private set; } - public Paging Shows { get; private set; } - public Paging Episodes { get; private set; } + public Paging Artists { get; private set; } + public Paging Albums { get; private set; } + public Paging Tracks { get; private set; } + public Paging Shows { get; private set; } + public Paging Episodes { get; private set; } } }