From 8017a02a10e7f949fe31222a621e479370bea107 Mon Sep 17 00:00:00 2001 From: "Johnny @PC" Date: Mon, 7 Dec 2015 14:08:23 +0100 Subject: [PATCH] Added support for /me/album endpoints --- SpotifyAPI/Web/SpotifyWebAPI.cs | 132 ++++++++++++++++++++++++++++ SpotifyAPI/Web/SpotifyWebBuilder.cs | 50 +++++++++++ 2 files changed, 182 insertions(+) diff --git a/SpotifyAPI/Web/SpotifyWebAPI.cs b/SpotifyAPI/Web/SpotifyWebAPI.cs index 77dc8fa4..25e93a71 100644 --- a/SpotifyAPI/Web/SpotifyWebAPI.cs +++ b/SpotifyAPI/Web/SpotifyWebAPI.cs @@ -935,6 +935,138 @@ namespace SpotifyAPI.Web return new ListResponse { List = null, Error = res["error"].ToObject() }; } + /// + /// Save one or more albums to the current user’s “Your Music” library. + /// + /// A list of the Spotify IDs + /// + /// AUTH NEEDED + public ErrorResponse SaveAlbums(List ids) + { + JArray array = new JArray(ids); + return UploadData(_builder.SaveAlbums(), array.ToString(Formatting.None), "PUT") ?? new ErrorResponse(); + } + + /// + /// Save one or more albums to the current user’s “Your Music” library asynchronously. + /// + /// A list of the Spotify IDs + /// + /// AUTH NEEDED + public async Task SaveAlbumsAsync(List ids) + { + JArray array = new JArray(ids); + return await UploadDataAsync(_builder.SaveAlbums(), array.ToString(Formatting.None), "PUT") ?? new ErrorResponse(); + } + + /// + /// Save one album to the current user’s “Your Music” library. + /// + /// A Spotify ID + /// + /// AUTH NEEDED + public ErrorResponse SaveAlbum(String id) + { + return SaveAlbums(new List { id }); + } + + /// + /// Save one album to the current user’s “Your Music” library asynchronously. + /// + /// A Spotify ID + /// + /// AUTH NEEDED + public async Task SaveAlbumAsync(String id) + { + return await SaveAlbumsAsync(new List { id }); + } + + /// + /// Get a list of the albums saved in the current Spotify user’s “Your Music” library. + /// + /// The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50. + /// The index of the first object to return. Default: 0 (i.e., the first object) + /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. + /// + /// AUTH NEEDED + public Paging GetSavedAlbums(int limit = 20, int offset = 0, String market = "") + { + if (!UseAuth) + throw new InvalidOperationException("Auth is required for GetSavedAlbums"); + return DownloadData>(_builder.GetSavedAlbums(limit, offset, market)); + } + + /// + /// Get a list of the albums saved in the current Spotify user’s “Your Music” library asynchronously. + /// + /// The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50. + /// The index of the first object to return. Default: 0 (i.e., the first object) + /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. + /// + /// AUTH NEEDED + public async Task> GetSavedAlbumsAsync(int limit = 20, int offset = 0, String market = "") + { + if (!UseAuth) + throw new InvalidOperationException("Auth is required for GetSavedAlbumsAsync"); + return await DownloadDataAsync>(_builder.GetSavedAlbums(limit, offset, market)); + } + + /// + /// Remove one or more albums from the current user’s “Your Music” library. + /// + /// A list of the Spotify IDs. + /// + /// AUTH NEEDED + public ErrorResponse RemoveSavedAlbums(List ids) + { + JArray array = new JArray(ids); + return UploadData(_builder.RemoveSavedAlbums(), array.ToString(Formatting.None), "DELETE") ?? new ErrorResponse(); + } + + /// + /// Remove one or more albums from the current user’s “Your Music” library asynchronously. + /// + /// A list of the Spotify IDs. + /// + /// AUTH NEEDED + public async Task RemoveSavedAlbumsAsync(List ids) + { + JArray array = new JArray(ids); + return await UploadDataAsync(_builder.RemoveSavedAlbums(), array.ToString(Formatting.None), "DELETE") ?? new ErrorResponse(); + } + + /// + /// Check if one or more albums is already saved in the current Spotify user’s “Your Music” library. + /// + /// A list of the Spotify IDs. + /// + /// AUTH NEEDED + public ListResponse CheckSavedAlbums(List ids) + { + if (!UseAuth) + throw new InvalidOperationException("Auth is required for CheckSavedTracks"); + JToken res = DownloadData(_builder.CheckSavedAlbums(ids)); + if (res is JArray) + return new ListResponse { List = res.ToObject>(), Error = null }; + return new ListResponse { List = null, Error = res["error"].ToObject() }; + } + + /// + /// Check if one or more albums is already saved in the current Spotify user’s “Your Music” library asynchronously. + /// + /// A list of the Spotify IDs. + /// + /// AUTH NEEDED + public async Task> CheckSavedAlbumsAsync(List ids) + { + if (!UseAuth) + throw new InvalidOperationException("Auth is required for CheckSavedAlbumsAsync"); + JToken res = await DownloadDataAsync(_builder.CheckSavedAlbums(ids)); + if (res is JArray) + return new ListResponse { List = res.ToObject>(), Error = null }; + return new ListResponse { List = null, Error = res["error"].ToObject() }; + } + #endregion Library #region Playlists diff --git a/SpotifyAPI/Web/SpotifyWebBuilder.cs b/SpotifyAPI/Web/SpotifyWebBuilder.cs index c03965b5..f978fe2f 100644 --- a/SpotifyAPI/Web/SpotifyWebBuilder.cs +++ b/SpotifyAPI/Web/SpotifyWebBuilder.cs @@ -438,6 +438,56 @@ namespace SpotifyAPI.Web return APIBase + "/me/tracks/contains?ids=" + string.Join(",", ids); } + /// + /// Save one or more albums to the current user’s "Your Music" library. + /// + /// + /// AUTH NEEDED + public string SaveAlbums() + { + return $"{APIBase}/me/albums"; + } + + /// + /// Get a list of the albums saved in the current Spotify user’s "Your Music" library. + /// + /// The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50. + /// The index of the first object to return. Default: 0 (i.e., the first object) + /// An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. + /// + /// AUTH NEEDED + public string GetSavedAlbums(int limit = 20, int offset = 0, string market = "") + { + limit = Math.Min(limit, 50); + StringBuilder builder = new StringBuilder(APIBase + "/me/albums"); + builder.Append("?limit=" + limit); + builder.Append("&offset=" + offset); + if (!string.IsNullOrEmpty(market)) + builder.Append("&market=" + market); + return builder.ToString(); + } + + /// + /// Remove one or more albums from the current user’s "Your Music" library. + /// + /// + /// AUTH NEEDED + public string RemoveSavedAlbums() + { + return APIBase + "/me/albums/"; + } + + /// + /// Check if one or more albums is already saved in the current Spotify user’s "Your Music" library. + /// + /// A list of the Spotify IDs. + /// + /// AUTH NEEDED + public string CheckSavedAlbums(List ids) + { + return APIBase + "/me/tracks/contains?ids=" + string.Join(",", ids); + } + #endregion Library #region Playlists