Skip to content
26 changes: 26 additions & 0 deletions SpotifyAPI.Web.Tests/Clients/UserProfileClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,31 @@ public async Task Get()

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);
}
}
}
18 changes: 18 additions & 0 deletions SpotifyAPI.Web/Clients/Interfaces/IUserProfileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,23 @@ public interface IUserProfileClient
/// <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);
}
}
16 changes: 15 additions & 1 deletion SpotifyAPI.Web/Clients/UserProfileClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using SpotifyAPI.Web.Http;
Expand All @@ -20,5 +19,20 @@ public Task<PublicUser> Get(string userId, CancellationToken cancel = default)

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);
}
}
}
47 changes: 47 additions & 0 deletions SpotifyAPI.Web/Models/Request/UsersTopItemsRequest.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
16 changes: 16 additions & 0 deletions SpotifyAPI.Web/Models/Response/UsersTopArtistsResponse.cs
Original file line number Diff line number Diff line change
@@ -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!;
}
}

16 changes: 16 additions & 0 deletions SpotifyAPI.Web/Models/Response/UsersTopTracksResponse.cs
Original file line number Diff line number Diff line change
@@ -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!;
}
}

4 changes: 4 additions & 0 deletions SpotifyAPI.Web/SpotifyUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public static class SpotifyUrls

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");
Expand Down