Add Users Top Tracks / Users Top Artists to UserClient (#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>
This commit is contained in:
Noel Griffin 2024-01-19 18:56:07 +00:00 committed by GitHub
parent 01fcd1d7d1
commit a27c3729c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 142 additions and 1 deletions

View File

@ -31,5 +31,31 @@ namespace SpotifyAPI.Web
api.Verify(a => a.Get<PublicUser>(SpotifyUrls.User(userId), It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task GetTopTracks()
{
var request = new UsersTopItemsRequest(TimeRange.LongTerm);
var api = new Mock<IAPIConnector>();
var client = new UserProfileClient(api.Object);
var res = await client.GetTopTracks(request);
api.Verify(a => a.Get<UsersTopTracksResponse>(SpotifyUrls.TopTracks(), request.BuildQueryParams(), It.IsAny<CancellationToken>()), Times.Once);
}
[Test]
public async Task GetTopArtists()
{
var request = new UsersTopItemsRequest(TimeRange.LongTerm);
var api = new Mock<IAPIConnector>();
var client = new UserProfileClient(api.Object);
await client.GetTopArtists(request);
api.Verify(a => a.Get<UsersTopArtistsResponse>(SpotifyUrls.TopArtists(), request.BuildQueryParams(), It.IsAny<CancellationToken>()), Times.Once);
}
}
}

View File

@ -28,5 +28,23 @@ namespace SpotifyAPI.Web
/// <exception cref="APIUnauthorizedException">Thrown if the client is not authenticated.</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716")]
Task<PublicUser> Get(string userId, CancellationToken cancel = default);
/// <summary>
/// Get Top tracks for the current user
/// </summary>
/// <param name="request">The query params to send to get Top Artists </param>
/// <param name="cancel">The cancellation-token to allow to cancel the request.</param>
/// <remarks>https://developer.spotify.com/documentation/web-api/reference/get-users-top-artists-and-tracks</remarks>
/// <exception cref="APIUnauthorizedException">Thrown if the client is not authenticated.</exception>
Task<UsersTopTracksResponse> GetTopTracks(UsersTopItemsRequest request, CancellationToken cancel = default);
/// <summary>
/// Get Top arsists for the current user
/// </summary>
/// <param name="request">The query params to send to get Top Artists</param>
/// <param name="cancel">The cancellation-token to allow to cancel the request.</param>
/// <remarks>https://developer.spotify.com/documentation/web-api/reference/get-users-top-artists-and-tracks</remarks>
/// <exception cref="APIUnauthorizedException">Thrown if the client is not authenticated.</exception>
Task<UsersTopArtistsResponse> GetTopArtists(UsersTopItemsRequest request, CancellationToken cancel = default);
}
}

View File

@ -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<PublicUser>(SpotifyUrls.User(userId), cancel);
}
public Task<UsersTopTracksResponse> GetTopTracks(UsersTopItemsRequest request, CancellationToken cancel = default)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<UsersTopTracksResponse>(SpotifyUrls.TopTracks(), request.BuildQueryParams(), cancel);
}
public Task<UsersTopArtistsResponse> GetTopArtists(UsersTopItemsRequest request, CancellationToken cancel = default)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<UsersTopArtistsResponse>(SpotifyUrls.TopArtists(), request.BuildQueryParams(), cancel);
}
}
}

View File

@ -0,0 +1,47 @@
using System;
namespace SpotifyAPI.Web
{
public class UsersTopItemsRequest : RequestParams
{
public UsersTopItemsRequest(TimeRange timeRange)
{
Ensure.ArgumentNotNull(timeRange, nameof(TimeRange));
TimeRangeParam = timeRange;
}
/// <summary>
/// The TimeRange Param : How far to look back for the top items.
/// </summary>
/// <value></value>
[QueryParam("time_range")]
public TimeRange TimeRangeParam { get; } = TimeRange.MediumTerm;
/// <summary>
/// The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50.
/// </summary>
/// <value></value>
[QueryParam("limit")]
public int? Limit { get; set; }
/// <summary>
/// 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.
/// </summary>
/// <value></value>
[QueryParam("offset")]
public int? Offset { get; set; }
}
public enum TimeRange
{
[String("short_term")]
ShortTerm,
[String("medium_term")]
MediumTerm,
[String("long_term")]
LongTerm
}
}

View File

@ -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<FullArtist> Items { get; set; } = default!;
}
}

View File

@ -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<FullTrack> Items { get; set; } = default!;
}
}

View File

@ -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");