From df6800f2ea274e69455e986c3af5b0d1fd09abed Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Fri, 8 May 2020 11:10:53 +0200 Subject: [PATCH] Added Artists Client #451 --- .vscode/csharp.code-snippets | 25 +++++++-- SpotifyAPI.Web/Clients/ArtistsClient.cs | 55 +++++++++++++++++++ .../Clients/Interfaces/IArtistsClient.cs | 18 ++++++ .../Clients/Interfaces/ISpotifyClient.cs | 2 + SpotifyAPI.Web/Clients/SpotifyClient.cs | 3 + .../Models/Request/ArtistsAlbumsRequest.cs | 35 ++++++++++++ .../Models/Request/ArtistsRequest.cs | 17 ++++++ .../Models/Request/ArtistsTopTracksRequest.cs | 15 +++++ .../Response/ArtistsRelatedArtistsResponse.cs | 9 +++ .../Models/Response/ArtistsResponse.cs | 9 +++ .../Response/ArtistsTopTracksResponse.cs | 9 +++ SpotifyAPI.Web/SpotifyUrls.cs | 10 ++++ 12 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 SpotifyAPI.Web/Clients/ArtistsClient.cs create mode 100644 SpotifyAPI.Web/Clients/Interfaces/IArtistsClient.cs create mode 100644 SpotifyAPI.Web/Models/Request/ArtistsAlbumsRequest.cs create mode 100644 SpotifyAPI.Web/Models/Request/ArtistsRequest.cs create mode 100644 SpotifyAPI.Web/Models/Request/ArtistsTopTracksRequest.cs create mode 100644 SpotifyAPI.Web/Models/Response/ArtistsRelatedArtistsResponse.cs create mode 100644 SpotifyAPI.Web/Models/Response/ArtistsResponse.cs create mode 100644 SpotifyAPI.Web/Models/Response/ArtistsTopTracksResponse.cs diff --git a/.vscode/csharp.code-snippets b/.vscode/csharp.code-snippets index 6d618ddd..58f4b322 100644 --- a/.vscode/csharp.code-snippets +++ b/.vscode/csharp.code-snippets @@ -1,18 +1,35 @@ { - "model": { + "model-class": { "scope": "csharp", - "prefix": "model", + "prefix": "model-class", "body": [ "namespace SpotifyAPI.Web", "{", - " public class $1", + " public class $TM_FILENAME_BASE", " {", - " public ${2:string} ${3:Name} { get; private set; }", + " public ${2:string} ${3:Name} { get; set; }", "", " $4", " }", "}" ], "description": "Creates a new model" + }, + "request-class": { + "scope": "csharp", + "prefix": "request-class", + "body": [ + "namespace SpotifyAPI.Web", + "{", + " public class $TM_FILENAME_BASE : RequestParams", + " {", + " [QueryParam(\"${3:Name}\")]", + " public ${2:string} ${3:Name} { get; set; }", + "", + " $4", + " }", + "}" + ], + "description": "Creates a new request" } } diff --git a/SpotifyAPI.Web/Clients/ArtistsClient.cs b/SpotifyAPI.Web/Clients/ArtistsClient.cs new file mode 100644 index 00000000..42bea3c6 --- /dev/null +++ b/SpotifyAPI.Web/Clients/ArtistsClient.cs @@ -0,0 +1,55 @@ +using System.Threading.Tasks; +using SpotifyAPI.Web.Http; +using URLs = SpotifyAPI.Web.SpotifyUrls; + +namespace SpotifyAPI.Web +{ + public class ArtistsClient : APIClient, IArtistsClient + { + public ArtistsClient(IAPIConnector apiConnector) : base(apiConnector) { } + + public Task Get(string artistId) + { + Ensure.ArgumentNotNullOrEmptyString(artistId, nameof(artistId)); + + return API.Get(URLs.Artist(artistId)); + } + + public Task> GetAlbums(string artistId) + { + Ensure.ArgumentNotNullOrEmptyString(artistId, nameof(artistId)); + + return API.Get>(URLs.ArtistAlbums(artistId)); + } + + public Task> GetAlbums(string artistId, ArtistsAlbumsRequest request) + { + Ensure.ArgumentNotNullOrEmptyString(artistId, nameof(artistId)); + Ensure.ArgumentNotNull(request, nameof(request)); + + return API.Get>(URLs.ArtistAlbums(artistId), request.BuildQueryParams()); + } + + public Task GetRelatedArtists(string artistId) + { + Ensure.ArgumentNotNullOrEmptyString(artistId, nameof(artistId)); + + return API.Get(URLs.ArtistRelatedArtists(artistId)); + } + + public Task GetSeveral(ArtistsRequest request) + { + Ensure.ArgumentNotNull(request, nameof(request)); + + return API.Get(URLs.Artists(), request.BuildQueryParams()); + } + + public Task GetTopTracks(string artistId, ArtistsTopTracksRequest request) + { + Ensure.ArgumentNotNullOrEmptyString(artistId, nameof(artistId)); + Ensure.ArgumentNotNull(request, nameof(request)); + + return API.Get(URLs.ArtistTopTracks(artistId), request.BuildQueryParams()); + } + } +} diff --git a/SpotifyAPI.Web/Clients/Interfaces/IArtistsClient.cs b/SpotifyAPI.Web/Clients/Interfaces/IArtistsClient.cs new file mode 100644 index 00000000..81a74453 --- /dev/null +++ b/SpotifyAPI.Web/Clients/Interfaces/IArtistsClient.cs @@ -0,0 +1,18 @@ +using System.Threading.Tasks; + +namespace SpotifyAPI.Web +{ + public interface IArtistsClient + { + Task GetSeveral(ArtistsRequest request); + + Task Get(string artistId); + + Task> GetAlbums(string artistId); + Task> GetAlbums(string artistId, ArtistsAlbumsRequest request); + + Task GetTopTracks(string artistId, ArtistsTopTracksRequest request); + + Task GetRelatedArtists(string artistId); + } +} diff --git a/SpotifyAPI.Web/Clients/Interfaces/ISpotifyClient.cs b/SpotifyAPI.Web/Clients/Interfaces/ISpotifyClient.cs index 511d0782..2f04367d 100644 --- a/SpotifyAPI.Web/Clients/Interfaces/ISpotifyClient.cs +++ b/SpotifyAPI.Web/Clients/Interfaces/ISpotifyClient.cs @@ -26,6 +26,8 @@ namespace SpotifyAPI.Web IAlbumsClient Albums { get; } + IArtistsClient Artists { get; } + Task> Paginate(Paging firstPage); Task> Paginate(Paging firstPage, Func> mapper); Task> Paginate(Func>> getFirstPage); diff --git a/SpotifyAPI.Web/Clients/SpotifyClient.cs b/SpotifyAPI.Web/Clients/SpotifyClient.cs index 404279f9..20e287e0 100644 --- a/SpotifyAPI.Web/Clients/SpotifyClient.cs +++ b/SpotifyAPI.Web/Clients/SpotifyClient.cs @@ -28,6 +28,7 @@ namespace SpotifyAPI.Web Tracks = new TracksClient(_apiConnector); Player = new PlayerClient(_apiConnector); Albums = new AlbumsClient(_apiConnector); + Artists = new ArtistsClient(_apiConnector); } public IPaginator DefaultPaginator { get; } @@ -50,6 +51,8 @@ namespace SpotifyAPI.Web public IAlbumsClient Albums { get; } + public IArtistsClient Artists { get; } + public Task> Paginate(Paging firstPage) { return DefaultPaginator.Paginate(firstPage, _apiConnector); diff --git a/SpotifyAPI.Web/Models/Request/ArtistsAlbumsRequest.cs b/SpotifyAPI.Web/Models/Request/ArtistsAlbumsRequest.cs new file mode 100644 index 00000000..bf9374d9 --- /dev/null +++ b/SpotifyAPI.Web/Models/Request/ArtistsAlbumsRequest.cs @@ -0,0 +1,35 @@ +using System; + +namespace SpotifyAPI.Web +{ + public class ArtistsAlbumsRequest : RequestParams + { + [QueryParam("include_groups")] + public IncludeGroups? IncludeGroupsParam { get; set; } + + [QueryParam("market")] + public string Market { get; set; } + + [QueryParam("limit")] + public int? Limit { get; set; } + + [QueryParam("offset")] + public int? Offset { get; set; } + + [Flags] + public enum IncludeGroups + { + [String("album")] + Album, + + [String("single")] + Single, + + [String("appears_on")] + AppearsOn, + + [String("compilation")] + Compilation + } + } +} diff --git a/SpotifyAPI.Web/Models/Request/ArtistsRequest.cs b/SpotifyAPI.Web/Models/Request/ArtistsRequest.cs new file mode 100644 index 00000000..81fdee4c --- /dev/null +++ b/SpotifyAPI.Web/Models/Request/ArtistsRequest.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; + +namespace SpotifyAPI.Web +{ + public class ArtistsRequest : RequestParams + { + public ArtistsRequest(IList ids) + { + Ensure.ArgumentNotNullOrEmptyList(ids, nameof(ids)); + + Ids = ids; + } + + [QueryParam("ids")] + public IList Ids { get; } + } +} diff --git a/SpotifyAPI.Web/Models/Request/ArtistsTopTracksRequest.cs b/SpotifyAPI.Web/Models/Request/ArtistsTopTracksRequest.cs new file mode 100644 index 00000000..d3b98cf5 --- /dev/null +++ b/SpotifyAPI.Web/Models/Request/ArtistsTopTracksRequest.cs @@ -0,0 +1,15 @@ +namespace SpotifyAPI.Web +{ + public class ArtistsTopTracksRequest : RequestParams + { + public ArtistsTopTracksRequest(string market) + { + Ensure.ArgumentNotNullOrEmptyString(market, nameof(market)); + + Market = market; + } + + [QueryParam("market")] + public string Market { get; } + } +} diff --git a/SpotifyAPI.Web/Models/Response/ArtistsRelatedArtistsResponse.cs b/SpotifyAPI.Web/Models/Response/ArtistsRelatedArtistsResponse.cs new file mode 100644 index 00000000..dcbf8a83 --- /dev/null +++ b/SpotifyAPI.Web/Models/Response/ArtistsRelatedArtistsResponse.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace SpotifyAPI.Web +{ + public class ArtistsRelatedArtistsResponse + { + public List Artists { get; set; } + } +} diff --git a/SpotifyAPI.Web/Models/Response/ArtistsResponse.cs b/SpotifyAPI.Web/Models/Response/ArtistsResponse.cs new file mode 100644 index 00000000..67d8b488 --- /dev/null +++ b/SpotifyAPI.Web/Models/Response/ArtistsResponse.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace SpotifyAPI.Web +{ + public class ArtistsResponse + { + public List Artists { get; set; } + } +} diff --git a/SpotifyAPI.Web/Models/Response/ArtistsTopTracksResponse.cs b/SpotifyAPI.Web/Models/Response/ArtistsTopTracksResponse.cs new file mode 100644 index 00000000..6f6d017e --- /dev/null +++ b/SpotifyAPI.Web/Models/Response/ArtistsTopTracksResponse.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace SpotifyAPI.Web +{ + public class ArtistsTopTracksResponse + { + public List Tracks { get; set; } + } +} diff --git a/SpotifyAPI.Web/SpotifyUrls.cs b/SpotifyAPI.Web/SpotifyUrls.cs index 6dbcd578..b79bad06 100644 --- a/SpotifyAPI.Web/SpotifyUrls.cs +++ b/SpotifyAPI.Web/SpotifyUrls.cs @@ -93,6 +93,16 @@ namespace SpotifyAPI.Web public static Uri AlbumTracks(string albumId) => EUri($"albums/{albumId}/tracks"); + public static Uri Artists() => EUri($"artists"); + + public static Uri Artist(string artistId) => EUri($"artists/{artistId}"); + + public static Uri ArtistAlbums(string artistId) => EUri($"artists/{artistId}/albums"); + + public static Uri ArtistTopTracks(string artistId) => EUri($"artists/{artistId}/top-tracks"); + + public static Uri ArtistRelatedArtists(string artistId) => EUri($"artists/{artistId}/related-artists"); + private static Uri EUri(FormattableString path) => new Uri(path.ToString(_provider), UriKind.Relative); } }