2020-05-05 04:26:37 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading.Tasks;
|
2020-05-01 19:05:28 +01:00
|
|
|
using SpotifyAPI.Web.Http;
|
|
|
|
|
|
|
|
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-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-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-05 04:26:37 +01:00
|
|
|
public Task<List<T>> Paginate<T>(Paging<T> firstPage)
|
|
|
|
{
|
|
|
|
return DefaultPaginator.Paginate(firstPage, _apiConnector);
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:52:23 +01:00
|
|
|
public Task<List<T>> Paginate<T, TNext>(Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper)
|
|
|
|
{
|
|
|
|
return DefaultPaginator.Paginate(firstPage, mapper, _apiConnector);
|
|
|
|
}
|
|
|
|
|
2020-05-05 04:26:37 +01:00
|
|
|
public Task<List<T>> Paginate<T>(Paging<T> firstPage, IPaginator paginator)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(paginator, nameof(paginator));
|
|
|
|
|
|
|
|
return paginator.Paginate(firstPage, _apiConnector);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<List<T>> Paginate<T>(Func<Task<Paging<T>>> getFirstPage)
|
|
|
|
{
|
|
|
|
return DefaultPaginator.Paginate(getFirstPage, _apiConnector);
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:52:23 +01:00
|
|
|
public Task<List<T>> Paginate<T, TNext>(Func<Task<Paging<T, TNext>>> getFirstPage, Func<TNext, Paging<T, TNext>> mapper)
|
|
|
|
{
|
|
|
|
return DefaultPaginator.Paginate(getFirstPage, mapper, _apiConnector);
|
|
|
|
}
|
|
|
|
|
2020-05-05 04:26:37 +01:00
|
|
|
public Task<List<T>> Paginate<T>(Func<Task<Paging<T>>> getFirstPage, IPaginator paginator)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(paginator, nameof(paginator));
|
|
|
|
|
|
|
|
return paginator.Paginate(getFirstPage, _apiConnector);
|
|
|
|
}
|
2020-05-01 19:05:28 +01:00
|
|
|
}
|
|
|
|
}
|