-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathSpotifyClient.cs
More file actions
316 lines (276 loc) · 13.3 KB
/
SpotifyClient.cs
File metadata and controls
316 lines (276 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Collections.Generic;
#if !NETSTANDARD2_0
using System.Runtime.CompilerServices;
#endif
using System.Threading;
using System.Threading.Tasks;
using SpotifyAPI.Web.Http;
namespace SpotifyAPI.Web
{
public class SpotifyClient : ISpotifyClient
{
private readonly IAPIConnector _apiConnector;
public SpotifyClient(IToken token) :
this(SpotifyClientConfig.CreateDefault(token?.AccessToken ?? throw new ArgumentNullException(nameof(token)), token.TokenType))
{ }
public SpotifyClient(string token, string tokenType = "Bearer") :
this(SpotifyClientConfig.CreateDefault(token, tokenType))
{ }
public SpotifyClient(SpotifyClientConfig config)
{
Ensure.ArgumentNotNull(config, nameof(config));
if (config.Authenticator == null)
{
#pragma warning disable CA2208
throw new ArgumentNullException("Authenticator in config is null. Please supply it via `WithAuthenticator` or `WithToken`");
#pragma warning restore CA2208
}
_apiConnector = config.BuildAPIConnector();
_apiConnector.ResponseReceived += (sender, response) =>
{
LastResponse = response;
};
DefaultPaginator = config.DefaultPaginator;
UserProfile = new UserProfileClient(_apiConnector);
Browse = new BrowseClient(_apiConnector);
Shows = new ShowsClient(_apiConnector);
Playlists = new PlaylistsClient(_apiConnector);
Search = new SearchClient(_apiConnector);
Follow = new FollowClient(_apiConnector);
Tracks = new TracksClient(_apiConnector);
Player = new PlayerClient(_apiConnector);
Albums = new AlbumsClient(_apiConnector);
Artists = new ArtistsClient(_apiConnector);
Personalization = new PersonalizationClient(_apiConnector);
Episodes = new EpisodesClient(_apiConnector);
Library = new LibraryClient(_apiConnector);
Markets = new MarketsClient(_apiConnector);
Audiobooks = new AudiobooksClient(_apiConnector);
Chapters = new ChaptersClient(_apiConnector);
}
public IPaginator DefaultPaginator { get; }
public IUserProfileClient UserProfile { get; }
public IBrowseClient Browse { get; }
public IShowsClient Shows { get; }
public IPlaylistsClient Playlists { get; }
public ISearchClient Search { get; }
public IFollowClient Follow { get; }
public ITracksClient Tracks { get; }
public IPlayerClient Player { get; }
public IAlbumsClient Albums { get; }
public IArtistsClient Artists { get; }
public IPersonalizationClient Personalization { get; }
public IEpisodesClient Episodes { get; }
public ILibraryClient Library { get; }
public IMarketsClient Markets { get; }
public IAudiobooksClient Audiobooks { get; }
public IChaptersClient Chapters { get; }
public IResponse? LastResponse { get; private set; }
/// <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>
/// <param name="cancellationToken">The cancellation-token to allow to cancel the request.</param>
/// <typeparam name="T">The Paging-Type</typeparam>
/// <returns>A list containing all fetched pages</returns>
public Task<IList<T>> PaginateAll<T>(IPaginatable<T> firstPage, IPaginator? paginator = null, CancellationToken cancellationToken = default)
{
return (paginator ?? DefaultPaginator).PaginateAll(firstPage, _apiConnector, cancellationToken);
}
/// <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>
/// <param name="cancellationToken">The cancellation-token to allow to cancel the request.</param>
/// <typeparam name="T">The Paging-Type</typeparam>
/// <typeparam name="TNext">The Response-Type</typeparam>
/// <returns>A list containing all fetched pages</returns>
public Task<IList<T>> PaginateAll<T, TNext>(
IPaginatable<T, TNext> firstPage,
Func<TNext, IPaginatable<T, TNext>> mapper,
IPaginator? paginator = null,
CancellationToken cancellationToken = default
)
{
return (paginator ?? DefaultPaginator).PaginateAll(firstPage, mapper, _apiConnector, cancellationToken);
}
private Task<T> FetchPage<T>(string? nextUrl)
{
if (nextUrl == null)
{
throw new APIPagingException("The paging object has no next page");
}
return _apiConnector.Get<T>(new Uri(nextUrl, UriKind.Absolute));
}
/// <summary>
/// Fetches the next page of the paging object
/// </summary>
/// <param name="paging">A paging object which has a next page</param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public Task<Paging<T>> NextPage<T>(Paging<T> paging)
{
Ensure.ArgumentNotNull(paging, nameof(paging));
return FetchPage<Paging<T>>(paging.Next);
}
/// <summary>
/// Fetches the next page of the cursor paging object
/// </summary>
/// <param name="cursorPaging">A cursor paging object which has a next page</param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public Task<CursorPaging<T>> NextPage<T>(CursorPaging<T> cursorPaging)
{
Ensure.ArgumentNotNull(cursorPaging, nameof(cursorPaging));
return FetchPage<CursorPaging<T>>(cursorPaging.Next);
}
/// <summary>
/// Fetches the next page of the complex IPaginatable object.
/// </summary>
/// <param name="paginatable">A complex IPaginatable object with a next page</param>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TNext">The type of the next page</typeparam>
/// <returns></returns>
public Task<TNext> NextPage<T, TNext>(IPaginatable<T, TNext> paginatable)
{
Ensure.ArgumentNotNull(paginatable, nameof(paginatable));
return FetchPage<TNext>(paginatable.Next);
}
/// <summary>
/// Fetches the previous page of the paging object.
/// </summary>
/// <param name="paging">A paging object with a previous page</param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public Task<Paging<T>> PreviousPage<T>(Paging<T> paging)
{
Ensure.ArgumentNotNull(paging, nameof(paging));
return FetchPage<Paging<T>>(paging.Previous);
}
/// <summary>
/// Fetches the previous page of the complex paging object.
/// </summary>
/// <param name="paging">A complex paging object with a previous page</param>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TNext">The type of the next page</typeparam>
/// <returns></returns>
public Task<TNext> PreviousPage<T, TNext>(Paging<T, TNext> paging)
{
Ensure.ArgumentNotNull(paging, nameof(paging));
return FetchPage<TNext>(paging.Previous);
}
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
/// <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="cancel">An optional Cancellation Token</param>
/// <typeparam name="T">The Paging-Type</typeparam>
/// <returns>An iterable IAsyncEnumerable</returns>
public IAsyncEnumerable<T> Paginate<T>(
IPaginatable<T> firstPage,
IPaginator? paginator = null,
CancellationToken cancel = default
)
{
return (paginator ?? DefaultPaginator).Paginate(firstPage, _apiConnector, cancel);
}
/// <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="cancel">An optional Cancellation Token</param>
/// <typeparam name="T">The Paging-Type</typeparam>
/// <typeparam name="TNext">The Response-Type</typeparam>
/// <returns></returns>
public IAsyncEnumerable<T> Paginate<T, TNext>(
IPaginatable<T, TNext> firstPage,
Func<TNext, IPaginatable<T, TNext>> mapper,
IPaginator? paginator = null,
CancellationToken cancel = default
)
{
return (paginator ?? DefaultPaginator).Paginate(firstPage, mapper, _apiConnector, cancel);
}
/// <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="cancel">An optional Cancellation Token</param>
/// <typeparam name="T">The Paging-Type</typeparam>
/// <typeparam name="TNext">The Response-Type</typeparam>
/// <returns></returns>
public async IAsyncEnumerable<T> Paginate<T, TNext>(
Func<Task<IPaginatable<T, TNext>>> getFirstPage,
Func<TNext, IPaginatable<T, TNext>> mapper,
IPaginator? paginator = null,
[EnumeratorCancellation] CancellationToken cancel = default)
{
Ensure.ArgumentNotNull(getFirstPage, nameof(getFirstPage));
var firstPage = await getFirstPage().ConfigureAwait(false);
await foreach (var item in (paginator ?? DefaultPaginator)
.Paginate(firstPage, mapper, _apiConnector, cancel)
.WithCancellation(cancel)
)
{
yield return item;
}
}
/// <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="cancel">An optional Cancellation Token</param>
/// <typeparam name="T">The Paging-Type</typeparam>
/// <typeparam name="TNext">The Response-Type</typeparam>
/// <returns></returns>
public async IAsyncEnumerable<T> Paginate<T, TNext>(
Task<IPaginatable<T, TNext>> firstPageTask,
Func<TNext, IPaginatable<T, TNext>> mapper,
IPaginator? paginator = null,
[EnumeratorCancellation] CancellationToken cancel = default)
{
Ensure.ArgumentNotNull(firstPageTask, nameof(firstPageTask));
var firstPage = await firstPageTask.ConfigureAwait(false);
await foreach (var item in (paginator ?? DefaultPaginator)
.Paginate(firstPage, mapper, _apiConnector, cancel)
.WithCancellation(cancel)
)
{
yield return item;
}
}
#endif
}
}