Skip to content

Commit a27c372

Browse files
Noelg14JohnnyCrazy
andauthored
Add Users Top Tracks / Users Top Artists to UserClient (JohnnyCrazy#938)
* add top tracks & artists endpoints * fix file extenstion * added TODO * Add Methods to interface * Add Top items query class * implement query into UserProfileClient.cs - Top Requests * Tidy Up Naming * Removed random json file change * Added Tests. * run formatter * remove additional namespace --------- Co-authored-by: Jonas Dellinger <jonas@dellinger.dev>
1 parent 01fcd1d commit a27c372

7 files changed

Lines changed: 142 additions & 1 deletion

File tree

‎SpotifyAPI.Web.Tests/Clients/UserProfileClientTest.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,31 @@ public async Task Get()
3131

3232
api.Verify(a => a.Get<PublicUser>(SpotifyUrls.User(userId), It.IsAny<CancellationToken>()), Times.Once);
3333
}
34+
35+
[Test]
36+
public async Task GetTopTracks()
37+
{
38+
39+
var request = new UsersTopItemsRequest(TimeRange.LongTerm);
40+
var api = new Mock<IAPIConnector>();
41+
var client = new UserProfileClient(api.Object);
42+
43+
var res = await client.GetTopTracks(request);
44+
45+
api.Verify(a => a.Get<UsersTopTracksResponse>(SpotifyUrls.TopTracks(), request.BuildQueryParams(), It.IsAny<CancellationToken>()), Times.Once);
46+
}
47+
48+
[Test]
49+
public async Task GetTopArtists()
50+
{
51+
52+
var request = new UsersTopItemsRequest(TimeRange.LongTerm);
53+
var api = new Mock<IAPIConnector>();
54+
var client = new UserProfileClient(api.Object);
55+
56+
await client.GetTopArtists(request);
57+
58+
api.Verify(a => a.Get<UsersTopArtistsResponse>(SpotifyUrls.TopArtists(), request.BuildQueryParams(), It.IsAny<CancellationToken>()), Times.Once);
59+
}
3460
}
3561
}

‎SpotifyAPI.Web/Clients/Interfaces/IUserProfileClient.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,23 @@ public interface IUserProfileClient
2828
/// <exception cref="APIUnauthorizedException">Thrown if the client is not authenticated.</exception>
2929
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716")]
3030
Task<PublicUser> Get(string userId, CancellationToken cancel = default);
31+
32+
/// <summary>
33+
/// Get Top tracks for the current user
34+
/// </summary>
35+
/// <param name="request">The query params to send to get Top Artists </param>
36+
/// <param name="cancel">The cancellation-token to allow to cancel the request.</param>
37+
/// <remarks>https://developer.spotify.com/documentation/web-api/reference/get-users-top-artists-and-tracks</remarks>
38+
/// <exception cref="APIUnauthorizedException">Thrown if the client is not authenticated.</exception>
39+
Task<UsersTopTracksResponse> GetTopTracks(UsersTopItemsRequest request, CancellationToken cancel = default);
40+
41+
/// <summary>
42+
/// Get Top arsists for the current user
43+
/// </summary>
44+
/// <param name="request">The query params to send to get Top Artists</param>
45+
/// <param name="cancel">The cancellation-token to allow to cancel the request.</param>
46+
/// <remarks>https://developer.spotify.com/documentation/web-api/reference/get-users-top-artists-and-tracks</remarks>
47+
/// <exception cref="APIUnauthorizedException">Thrown if the client is not authenticated.</exception>
48+
Task<UsersTopArtistsResponse> GetTopArtists(UsersTopItemsRequest request, CancellationToken cancel = default);
3149
}
3250
}

‎SpotifyAPI.Web/Clients/UserProfileClient.cs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Threading;
32
using System.Threading.Tasks;
43
using SpotifyAPI.Web.Http;
@@ -20,5 +19,20 @@ public Task<PublicUser> Get(string userId, CancellationToken cancel = default)
2019

2120
return API.Get<PublicUser>(SpotifyUrls.User(userId), cancel);
2221
}
22+
23+
public Task<UsersTopTracksResponse> GetTopTracks(UsersTopItemsRequest request, CancellationToken cancel = default)
24+
{
25+
Ensure.ArgumentNotNull(request, nameof(request));
26+
27+
return API.Get<UsersTopTracksResponse>(SpotifyUrls.TopTracks(), request.BuildQueryParams(), cancel);
28+
29+
}
30+
31+
public Task<UsersTopArtistsResponse> GetTopArtists(UsersTopItemsRequest request, CancellationToken cancel = default)
32+
{
33+
Ensure.ArgumentNotNull(request, nameof(request));
34+
35+
return API.Get<UsersTopArtistsResponse>(SpotifyUrls.TopArtists(), request.BuildQueryParams(), cancel);
36+
}
2337
}
2438
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
3+
namespace SpotifyAPI.Web
4+
{
5+
public class UsersTopItemsRequest : RequestParams
6+
{
7+
public UsersTopItemsRequest(TimeRange timeRange)
8+
{
9+
Ensure.ArgumentNotNull(timeRange, nameof(TimeRange));
10+
11+
TimeRangeParam = timeRange;
12+
}
13+
14+
/// <summary>
15+
/// The TimeRange Param : How far to look back for the top items.
16+
/// </summary>
17+
/// <value></value>
18+
[QueryParam("time_range")]
19+
public TimeRange TimeRangeParam { get; } = TimeRange.MediumTerm;
20+
21+
22+
/// <summary>
23+
/// The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50.
24+
/// </summary>
25+
/// <value></value>
26+
[QueryParam("limit")]
27+
public int? Limit { get; set; }
28+
29+
/// <summary>
30+
/// The index of the first object to return. Default: 0 (i.e., the first object).
31+
/// Use with limit to get the next set of objects.
32+
/// </summary>
33+
/// <value></value>
34+
[QueryParam("offset")]
35+
public int? Offset { get; set; }
36+
37+
}
38+
public enum TimeRange
39+
{
40+
[String("short_term")]
41+
ShortTerm,
42+
[String("medium_term")]
43+
MediumTerm,
44+
[String("long_term")]
45+
LongTerm
46+
}
47+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Collections.Generic;
2+
3+
namespace SpotifyAPI.Web
4+
{
5+
public class UsersTopArtistsResponse
6+
{
7+
public string Href { get; set; } = default!;
8+
public int Limit { get; set; }
9+
public string Next { get; set; } = default!;
10+
public int Offset { get; set; }
11+
public string Previous { get; set; } = default!;
12+
public int Total { get; set; } = default!;
13+
public List<FullArtist> Items { get; set; } = default!;
14+
}
15+
}
16+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Collections.Generic;
2+
3+
namespace SpotifyAPI.Web
4+
{
5+
public class UsersTopTracksResponse
6+
{
7+
public string Href { get; set; } = default!;
8+
public int Limit { get; set; }
9+
public string Next { get; set; } = default!;
10+
public int Offset { get; set; }
11+
public string Previous { get; set; } = default!;
12+
public int Total { get; set; } = default!;
13+
public List<FullTrack> Items { get; set; } = default!;
14+
}
15+
}
16+

‎SpotifyAPI.Web/SpotifyUrls.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public static class SpotifyUrls
1313

1414
public static Uri Me() => EUri($"me");
1515

16+
public static Uri TopTracks() => EUri($"me/top/tracks");
17+
18+
public static Uri TopArtists() => EUri($"me/top/artists");
19+
1620
public static Uri User(string userId) => EUri($"users/{userId}");
1721

1822
public static Uri Categories() => EUri($"browse/categories");

0 commit comments

Comments
 (0)