Spotify.NET/SpotifyAPI.Web/Clients/SpotifyClient.cs

108 lines
3.2 KiB
C#
Raw Normal View History

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
{
private readonly IAPIConnector _apiConnector;
2020-05-01 19:05:28 +01:00
public SpotifyClient(string token, string tokenType = "Bearer") :
this(SpotifyClientConfig.CreateDefault(token, tokenType))
2020-05-01 19:05:28 +01:00
{ }
public SpotifyClient(SpotifyClientConfig config)
2020-05-01 19:05:28 +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
);
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);
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);
Episodes = new EpisodesClient(_apiConnector);
2020-05-01 19:05:28 +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; }
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-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; }
public IEpisodesClient Episodes { get; }
public Task<List<T>> Paginate<T>(Paging<T> firstPage)
{
return DefaultPaginator.Paginate(firstPage, _apiConnector);
}
public Task<List<T>> Paginate<T, TNext>(Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper)
{
return DefaultPaginator.Paginate(firstPage, mapper, _apiConnector);
}
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);
}
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);
}
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
}
}