Skip to content

Commit 5188764

Browse files
committed
Added better Pagination support via IAsyncEnumerable
1 parent 320aa5f commit 5188764

13 files changed

Lines changed: 247 additions & 49 deletions

File tree

‎.editorconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dotnet_diagnostic.CA1056.severity = none
1313
dotnet_diagnostic.CA1034.severity = none
1414
dotnet_diagnostic.CA1054.severity = none
1515
dotnet_diagnostic.CA2227.severity = none
16+
dotnet_diagnostic.CA1308.severity = none
1617
# Sort using and Import directives with System.* appearing first
1718
dotnet_sort_system_directives_first = true
1819
dotnet_style_require_accessibility_modifiers = always:warning

‎SpotifyAPI.Docs/docs/configuration.md‎

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
id: getting_started
3+
title: Getting Started
4+
---

‎SpotifyAPI.Docs/docusaurus.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = {
2626
items: [
2727
{
2828
label: 'Latest/Next',
29-
to: 'docs/next/home',
29+
to: 'docs/next/introduction',
3030
},
3131
{
3232
label: versions[0],

‎SpotifyAPI.Docs/sidebars.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ module.exports = {
33
'Spotify-API': [
44
'introduction',
55
'installation',
6-
'configuration',
6+
'getting_started',
77
{
88
type: 'category',
99
label: 'Guides',
1010
items: [
1111
'pagination',
1212
]
1313
},
14+
{
15+
type: 'category',
16+
label: 'Examples',
17+
items: []
18+
},
1419
]
1520
}
1621
};

‎SpotifyAPI.Docs/src/pages/index.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ const exampleCode =
1616
var me = await spotify.UserProfile.Current();
1717
Console.WriteLine($"Hello there {me.DisplayName}");
1818
19-
var playlists = await spotify.Paginate(() => spotify.Playlists.CurrentUsers());
20-
Console.WriteLine($"You got {playlists.Count} playlists 🚀");
21-
`;
19+
await foreach(var playlist in spotify.Paginate(
20+
() => spotify.Playlists.CurrentUsers()
21+
))
22+
{
23+
Console.WriteLine(playlist.Name);
24+
}`;
2225

2326
const installCodeNuget =
2427
`# Core Package
@@ -108,7 +111,7 @@ function Home() {
108111
'button button--outline button--secondary button--lg',
109112
styles.getStarted,
110113
)}
111-
to={useBaseUrl('docs/next/home')}>
114+
to={useBaseUrl('docs/next/introduction')}>
112115
Get Started
113116
</Link>
114117
</div>

‎SpotifyAPI.Web.Examples/Example.CLI.PersistentConfig/Program.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static async Task Start()
5353
var me = await spotify.UserProfile.Current();
5454
Console.WriteLine($"Welcome {me.DisplayName} ({me.Id}), your authenticated!");
5555

56-
var playlists = await spotify.Paginate(await spotify.Playlists.CurrentUsers());
56+
var playlists = await spotify.PaginateAll(() => spotify.Playlists.CurrentUsers());
5757
Console.WriteLine($"Total Playlists in your Account: {playlists.Count}");
5858

5959
Environment.Exit(0);

‎SpotifyAPI.Web.Examples/Example.UWP/ViewModels/PlaylistsListViewModel.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public class PlaylistsListViewModel : MvxViewModel<string>
1616
private SpotifyClient _spotify;
1717

1818
private List<SimplePlaylist> _playlists;
19-
public List<SimplePlaylist> Playlists {
19+
public List<SimplePlaylist> Playlists
20+
{
2021
get => _playlists ?? (_playlists = new List<SimplePlaylist>());
2122
set => SetProperty(ref _playlists, value);
2223
}
@@ -30,7 +31,7 @@ public override async Task Initialize()
3031
{
3132
await base.Initialize();
3233

33-
Playlists = await _spotify.Paginate(() => _spotify.Playlists.CurrentUsers());
34+
Playlists = await _spotify.PaginateAll(() => _spotify.Playlists.CurrentUsers());
3435
}
3536
}
3637
}
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Threading;
12
using System;
23
using System.Collections.Generic;
34
using System.Threading.Tasks;
@@ -7,10 +8,21 @@ namespace SpotifyAPI.Web
78
{
89
public interface IPaginator
910
{
10-
Task<List<T>> Paginate<T>(Paging<T> firstPage, IAPIConnector connector);
11-
Task<List<T>> Paginate<T>(Func<Task<Paging<T>>> getFirstPage, IAPIConnector connector);
11+
Task<List<T>> PaginateAll<T>(Paging<T> firstPage, IAPIConnector connector);
12+
Task<List<T>> PaginateAll<T, TNext>(
13+
Paging<T, TNext> firstPage,
14+
Func<TNext, Paging<T, TNext>> mapper,
15+
IAPIConnector connector
16+
);
1217

13-
Task<List<T>> Paginate<T, TNext>(Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper, IAPIConnector connector);
14-
Task<List<T>> Paginate<T, TNext>(Func<Task<Paging<T, TNext>>> getFirstPage, Func<TNext, Paging<T, TNext>> mapper, IAPIConnector connector);
18+
#if NETSTANDARD2_1
19+
IAsyncEnumerable<T> Paginate<T>(Paging<T> firstPage, IAPIConnector connector, CancellationToken cancel = default);
20+
IAsyncEnumerable<T> Paginate<T, TNext>(
21+
Paging<T, TNext> firstPage,
22+
Func<TNext, Paging<T, TNext>> mapper,
23+
IAPIConnector connector,
24+
CancellationToken cancel = default
25+
);
26+
#endif
1527
}
1628
}

‎SpotifyAPI.Web/Clients/Interfaces/ISpotifyClient.cs‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,26 @@ public interface ISpotifyClient
3434

3535
ILibraryClient Library { get; }
3636

37-
Task<List<T>> Paginate<T>(Paging<T> firstPage);
38-
Task<List<T>> Paginate<T, TNext>(Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper);
39-
Task<List<T>> Paginate<T>(Func<Task<Paging<T>>> getFirstPage);
40-
Task<List<T>> Paginate<T, TNext>(Func<Task<Paging<T, TNext>>> getFirstPage, Func<TNext, Paging<T, TNext>> mapper);
41-
Task<List<T>> Paginate<T>(Paging<T> firstPage, IPaginator paginator);
42-
Task<List<T>> Paginate<T>(Func<Task<Paging<T>>> getFirstPage, IPaginator paginator);
37+
Task<List<T>> PaginateAll<T>(Paging<T> firstPage);
38+
Task<List<T>> PaginateAll<T>(Paging<T> firstPage, IPaginator paginator);
39+
Task<List<T>> PaginateAll<T>(Func<Task<Paging<T>>> getFirstPage);
40+
Task<List<T>> PaginateAll<T>(Func<Task<Paging<T>>> getFirstPage, IPaginator paginator);
41+
42+
Task<List<T>> PaginateAll<T, TNext>(Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper);
43+
Task<List<T>> PaginateAll<T, TNext>(Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper, IPaginator paginator);
44+
Task<List<T>> PaginateAll<T, TNext>(Func<Task<Paging<T, TNext>>> getFirstPage, Func<TNext, Paging<T, TNext>> mapper);
45+
Task<List<T>> PaginateAll<T, TNext>(Func<Task<Paging<T, TNext>>> getFirstPage, Func<TNext, Paging<T, TNext>> mapper, IPaginator paginator);
46+
47+
#if NETSTANDARD2_1
48+
IAsyncEnumerable<T> Paginate<T>(Paging<T> firstPage);
49+
IAsyncEnumerable<T> Paginate<T>(Paging<T> firstPage, IPaginator paginator);
50+
IAsyncEnumerable<T> Paginate<T>(Func<Task<Paging<T>>> getFirstPage);
51+
IAsyncEnumerable<T> Paginate<T>(Func<Task<Paging<T>>> getFirstPage, IPaginator paginator);
52+
53+
IAsyncEnumerable<T> Paginate<T, TNext>(Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper);
54+
IAsyncEnumerable<T> Paginate<T, TNext>(Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper, IPaginator paginator);
55+
IAsyncEnumerable<T> Paginate<T, TNext>(Func<Task<Paging<T, TNext>>> getFirstPage, Func<TNext, Paging<T, TNext>> mapper);
56+
IAsyncEnumerable<T> Paginate<T, TNext>(Func<Task<Paging<T, TNext>>> getFirstPage, Func<TNext, Paging<T, TNext>> mapper, IPaginator paginator);
57+
#endif
4358
}
4459
}

0 commit comments

Comments
 (0)