diff --git a/SpotifyAPI.Web.Tests/Clients/UserProfileClientTest.cs b/SpotifyAPI.Web.Tests/Clients/UserProfileClientTest.cs index 2818c2f1..0127d01a 100644 --- a/SpotifyAPI.Web.Tests/Clients/UserProfileClientTest.cs +++ b/SpotifyAPI.Web.Tests/Clients/UserProfileClientTest.cs @@ -31,5 +31,31 @@ namespace SpotifyAPI.Web api.Verify(a => a.Get(SpotifyUrls.User(userId), It.IsAny()), Times.Once); } + + [Test] + public async Task GetTopTracks() + { + + var request = new UsersTopItemsRequest(TimeRange.LongTerm); + var api = new Mock(); + var client = new UserProfileClient(api.Object); + + var res = await client.GetTopTracks(request); + + api.Verify(a => a.Get(SpotifyUrls.TopTracks(), request.BuildQueryParams(), It.IsAny()), Times.Once); + } + + [Test] + public async Task GetTopArtists() + { + + var request = new UsersTopItemsRequest(TimeRange.LongTerm); + var api = new Mock(); + var client = new UserProfileClient(api.Object); + + await client.GetTopArtists(request); + + api.Verify(a => a.Get(SpotifyUrls.TopArtists(), request.BuildQueryParams(), It.IsAny()), Times.Once); + } } } diff --git a/SpotifyAPI.Web/Clients/Interfaces/IUserProfileClient.cs b/SpotifyAPI.Web/Clients/Interfaces/IUserProfileClient.cs index 5a634f45..5256ec82 100644 --- a/SpotifyAPI.Web/Clients/Interfaces/IUserProfileClient.cs +++ b/SpotifyAPI.Web/Clients/Interfaces/IUserProfileClient.cs @@ -28,5 +28,23 @@ namespace SpotifyAPI.Web /// Thrown if the client is not authenticated. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716")] Task Get(string userId, CancellationToken cancel = default); + + /// + /// Get Top tracks for the current user + /// + /// The query params to send to get Top Artists + /// The cancellation-token to allow to cancel the request. + /// https://developer.spotify.com/documentation/web-api/reference/get-users-top-artists-and-tracks + /// Thrown if the client is not authenticated. + Task GetTopTracks(UsersTopItemsRequest request, CancellationToken cancel = default); + + /// + /// Get Top arsists for the current user + /// + /// The query params to send to get Top Artists + /// The cancellation-token to allow to cancel the request. + /// https://developer.spotify.com/documentation/web-api/reference/get-users-top-artists-and-tracks + /// Thrown if the client is not authenticated. + Task GetTopArtists(UsersTopItemsRequest request, CancellationToken cancel = default); } } diff --git a/SpotifyAPI.Web/Clients/UserProfileClient.cs b/SpotifyAPI.Web/Clients/UserProfileClient.cs index f51cda20..098a327e 100644 --- a/SpotifyAPI.Web/Clients/UserProfileClient.cs +++ b/SpotifyAPI.Web/Clients/UserProfileClient.cs @@ -1,4 +1,3 @@ -using System; using System.Threading; using System.Threading.Tasks; using SpotifyAPI.Web.Http; @@ -20,5 +19,20 @@ namespace SpotifyAPI.Web return API.Get(SpotifyUrls.User(userId), cancel); } + + public Task GetTopTracks(UsersTopItemsRequest request, CancellationToken cancel = default) + { + Ensure.ArgumentNotNull(request, nameof(request)); + + return API.Get(SpotifyUrls.TopTracks(), request.BuildQueryParams(), cancel); + + } + + public Task GetTopArtists(UsersTopItemsRequest request, CancellationToken cancel = default) + { + Ensure.ArgumentNotNull(request, nameof(request)); + + return API.Get(SpotifyUrls.TopArtists(), request.BuildQueryParams(), cancel); + } } } diff --git a/SpotifyAPI.Web/Models/Request/UsersTopItemsRequest.cs b/SpotifyAPI.Web/Models/Request/UsersTopItemsRequest.cs new file mode 100644 index 00000000..aa3c0bc4 --- /dev/null +++ b/SpotifyAPI.Web/Models/Request/UsersTopItemsRequest.cs @@ -0,0 +1,47 @@ +using System; + +namespace SpotifyAPI.Web +{ + public class UsersTopItemsRequest : RequestParams + { + public UsersTopItemsRequest(TimeRange timeRange) + { + Ensure.ArgumentNotNull(timeRange, nameof(TimeRange)); + + TimeRangeParam = timeRange; + } + + /// + /// The TimeRange Param : How far to look back for the top items. + /// + /// + [QueryParam("time_range")] + public TimeRange TimeRangeParam { get; } = TimeRange.MediumTerm; + + + /// + /// The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50. + /// + /// + [QueryParam("limit")] + public int? Limit { get; set; } + + /// + /// The index of the first object to return. Default: 0 (i.e., the first object). + /// Use with limit to get the next set of objects. + /// + /// + [QueryParam("offset")] + public int? Offset { get; set; } + + } + public enum TimeRange + { + [String("short_term")] + ShortTerm, + [String("medium_term")] + MediumTerm, + [String("long_term")] + LongTerm + } +} diff --git a/SpotifyAPI.Web/Models/Response/UsersTopArtistsResponse.cs b/SpotifyAPI.Web/Models/Response/UsersTopArtistsResponse.cs new file mode 100644 index 00000000..be05b17b --- /dev/null +++ b/SpotifyAPI.Web/Models/Response/UsersTopArtistsResponse.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; + +namespace SpotifyAPI.Web +{ + public class UsersTopArtistsResponse + { + public string Href { get; set; } = default!; + public int Limit { get; set; } + public string Next { get; set; } = default!; + public int Offset { get; set; } + public string Previous { get; set; } = default!; + public int Total { get; set; } = default!; + public List Items { get; set; } = default!; + } +} + diff --git a/SpotifyAPI.Web/Models/Response/UsersTopTracksResponse.cs b/SpotifyAPI.Web/Models/Response/UsersTopTracksResponse.cs new file mode 100644 index 00000000..0075231e --- /dev/null +++ b/SpotifyAPI.Web/Models/Response/UsersTopTracksResponse.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; + +namespace SpotifyAPI.Web +{ + public class UsersTopTracksResponse + { + public string Href { get; set; } = default!; + public int Limit { get; set; } + public string Next { get; set; } = default!; + public int Offset { get; set; } + public string Previous { get; set; } = default!; + public int Total { get; set; } = default!; + public List Items { get; set; } = default!; + } +} + diff --git a/SpotifyAPI.Web/SpotifyUrls.cs b/SpotifyAPI.Web/SpotifyUrls.cs index 570a0aa2..30aa943c 100644 --- a/SpotifyAPI.Web/SpotifyUrls.cs +++ b/SpotifyAPI.Web/SpotifyUrls.cs @@ -13,6 +13,10 @@ namespace SpotifyAPI.Web public static Uri Me() => EUri($"me"); + public static Uri TopTracks() => EUri($"me/top/tracks"); + + public static Uri TopArtists() => EUri($"me/top/artists"); + public static Uri User(string userId) => EUri($"users/{userId}"); public static Uri Categories() => EUri($"browse/categories");