Added Personalization Client #451

This commit is contained in:
Jonas Dellinger 2020-05-08 12:09:59 +02:00
parent df6800f2ea
commit 8fb0ba7974
6 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,13 @@
using System.Threading.Tasks;
namespace SpotifyAPI.Web
{
public interface IPersonalizationClient
{
Task<Paging<FullTrack>> GetTopTracks();
Task<Paging<FullTrack>> GetTopTracks(PersonalizationTopRequest request);
Task<Paging<FullArtist>> GetTopArtists();
Task<Paging<FullArtist>> GetTopArtists(PersonalizationTopRequest request);
}
}

View File

@ -28,6 +28,8 @@ namespace SpotifyAPI.Web
IArtistsClient Artists { get; } IArtistsClient Artists { get; }
IPersonalizationClient Personalization { get; }
Task<List<T>> Paginate<T>(Paging<T> firstPage); Task<List<T>> Paginate<T>(Paging<T> firstPage);
Task<List<T>> Paginate<T, TNext>(Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper); Task<List<T>> Paginate<T, TNext>(Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper);
Task<List<T>> Paginate<T>(Func<Task<Paging<T>>> getFirstPage); Task<List<T>> Paginate<T>(Func<Task<Paging<T>>> getFirstPage);

View File

@ -0,0 +1,35 @@
using System.Threading.Tasks;
using SpotifyAPI.Web.Http;
using URLs = SpotifyAPI.Web.SpotifyUrls;
namespace SpotifyAPI.Web
{
public class PersonalizationClient : APIClient, IPersonalizationClient
{
public PersonalizationClient(IAPIConnector apiConnector) : base(apiConnector) { }
public Task<Paging<FullArtist>> GetTopArtists()
{
return API.Get<Paging<FullArtist>>(URLs.PersonalizationTop("artists"));
}
public Task<Paging<FullArtist>> GetTopArtists(PersonalizationTopRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<Paging<FullArtist>>(URLs.PersonalizationTop("artists"), request.BuildQueryParams());
}
public Task<Paging<FullTrack>> GetTopTracks()
{
return API.Get<Paging<FullTrack>>(URLs.PersonalizationTop("tracks"));
}
public Task<Paging<FullTrack>> GetTopTracks(PersonalizationTopRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<Paging<FullTrack>>(URLs.PersonalizationTop("tracks"), request.BuildQueryParams());
}
}
}

View File

@ -29,6 +29,7 @@ namespace SpotifyAPI.Web
Player = new PlayerClient(_apiConnector); Player = new PlayerClient(_apiConnector);
Albums = new AlbumsClient(_apiConnector); Albums = new AlbumsClient(_apiConnector);
Artists = new ArtistsClient(_apiConnector); Artists = new ArtistsClient(_apiConnector);
Personalization = new PersonalizationClient(_apiConnector);
} }
public IPaginator DefaultPaginator { get; } public IPaginator DefaultPaginator { get; }
@ -53,6 +54,8 @@ namespace SpotifyAPI.Web
public IArtistsClient Artists { get; } public IArtistsClient Artists { get; }
public IPersonalizationClient Personalization { get; }
public Task<List<T>> Paginate<T>(Paging<T> firstPage) public Task<List<T>> Paginate<T>(Paging<T> firstPage)
{ {
return DefaultPaginator.Paginate(firstPage, _apiConnector); return DefaultPaginator.Paginate(firstPage, _apiConnector);

View File

@ -0,0 +1,26 @@
namespace SpotifyAPI.Web
{
public class PersonalizationTopRequest : RequestParams
{
[QueryParam("limit")]
public int? Limit { get; set; }
[QueryParam("offset")]
public int? Offset { get; set; }
[QueryParam("time_range")]
public TimeRange? TimeRangeParam { get; set; }
public enum TimeRange
{
[String("long_term")]
LongTerm,
[String("medium_term")]
MediumTerm,
[String("short_term")]
ShortTerm
}
}
}

View File

@ -103,6 +103,8 @@ namespace SpotifyAPI.Web
public static Uri ArtistRelatedArtists(string artistId) => EUri($"artists/{artistId}/related-artists"); public static Uri ArtistRelatedArtists(string artistId) => EUri($"artists/{artistId}/related-artists");
public static Uri PersonalizationTop(string type) => EUri($"me/top/{type}");
private static Uri EUri(FormattableString path) => new Uri(path.ToString(_provider), UriKind.Relative); private static Uri EUri(FormattableString path) => new Uri(path.ToString(_provider), UriKind.Relative);
} }
} }