Skip to content

Commit 8017a02

Browse files
committed
Added support for /me/album endpoints
1 parent 59bdd4b commit 8017a02

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

SpotifyAPI/Web/SpotifyWebAPI.cs

+132
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,138 @@ public async Task<ListResponse<Boolean>> CheckSavedTracksAsync(List<String> ids)
935935
return new ListResponse<Boolean> { List = null, Error = res["error"].ToObject<Error>() };
936936
}
937937

938+
/// <summary>
939+
/// Save one or more albums to the current user’s “Your Music” library.
940+
/// </summary>
941+
/// <param name="ids">A list of the Spotify IDs</param>
942+
/// <returns></returns>
943+
/// <remarks>AUTH NEEDED</remarks>
944+
public ErrorResponse SaveAlbums(List<String> ids)
945+
{
946+
JArray array = new JArray(ids);
947+
return UploadData<ErrorResponse>(_builder.SaveAlbums(), array.ToString(Formatting.None), "PUT") ?? new ErrorResponse();
948+
}
949+
950+
/// <summary>
951+
/// Save one or more albums to the current user’s “Your Music” library asynchronously.
952+
/// </summary>
953+
/// <param name="ids">A list of the Spotify IDs</param>
954+
/// <returns></returns>
955+
/// <remarks>AUTH NEEDED</remarks>
956+
public async Task<ErrorResponse> SaveAlbumsAsync(List<String> ids)
957+
{
958+
JArray array = new JArray(ids);
959+
return await UploadDataAsync<ErrorResponse>(_builder.SaveAlbums(), array.ToString(Formatting.None), "PUT") ?? new ErrorResponse();
960+
}
961+
962+
/// <summary>
963+
/// Save one album to the current user’s “Your Music” library.
964+
/// </summary>
965+
/// <param name="id">A Spotify ID</param>
966+
/// <returns></returns>
967+
/// <remarks>AUTH NEEDED</remarks>
968+
public ErrorResponse SaveAlbum(String id)
969+
{
970+
return SaveAlbums(new List<string> { id });
971+
}
972+
973+
/// <summary>
974+
/// Save one album to the current user’s “Your Music” library asynchronously.
975+
/// </summary>
976+
/// <param name="id">A Spotify ID</param>
977+
/// <returns></returns>
978+
/// <remarks>AUTH NEEDED</remarks>
979+
public async Task<ErrorResponse> SaveAlbumAsync(String id)
980+
{
981+
return await SaveAlbumsAsync(new List<string> { id });
982+
}
983+
984+
/// <summary>
985+
/// Get a list of the albums saved in the current Spotify user’s “Your Music” library.
986+
/// </summary>
987+
/// <param name="limit">The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50.</param>
988+
/// <param name="offset">The index of the first object to return. Default: 0 (i.e., the first object)</param>
989+
/// <param name="market">An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking.</param>
990+
/// <returns></returns>
991+
/// <remarks>AUTH NEEDED</remarks>
992+
public Paging<SavedTrack> GetSavedAlbums(int limit = 20, int offset = 0, String market = "")
993+
{
994+
if (!UseAuth)
995+
throw new InvalidOperationException("Auth is required for GetSavedAlbums");
996+
return DownloadData<Paging<SavedTrack>>(_builder.GetSavedAlbums(limit, offset, market));
997+
}
998+
999+
/// <summary>
1000+
/// Get a list of the albums saved in the current Spotify user’s “Your Music” library asynchronously.
1001+
/// </summary>
1002+
/// <param name="limit">The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50.</param>
1003+
/// <param name="offset">The index of the first object to return. Default: 0 (i.e., the first object)</param>
1004+
/// <param name="market">An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking.</param>
1005+
/// <returns></returns>
1006+
/// <remarks>AUTH NEEDED</remarks>
1007+
public async Task<Paging<SavedTrack>> GetSavedAlbumsAsync(int limit = 20, int offset = 0, String market = "")
1008+
{
1009+
if (!UseAuth)
1010+
throw new InvalidOperationException("Auth is required for GetSavedAlbumsAsync");
1011+
return await DownloadDataAsync<Paging<SavedTrack>>(_builder.GetSavedAlbums(limit, offset, market));
1012+
}
1013+
1014+
/// <summary>
1015+
/// Remove one or more albums from the current user’s “Your Music” library.
1016+
/// </summary>
1017+
/// <param name="ids">A list of the Spotify IDs.</param>
1018+
/// <returns></returns>
1019+
/// <remarks>AUTH NEEDED</remarks>
1020+
public ErrorResponse RemoveSavedAlbums(List<String> ids)
1021+
{
1022+
JArray array = new JArray(ids);
1023+
return UploadData<ErrorResponse>(_builder.RemoveSavedAlbums(), array.ToString(Formatting.None), "DELETE") ?? new ErrorResponse();
1024+
}
1025+
1026+
/// <summary>
1027+
/// Remove one or more albums from the current user’s “Your Music” library asynchronously.
1028+
/// </summary>
1029+
/// <param name="ids">A list of the Spotify IDs.</param>
1030+
/// <returns></returns>
1031+
/// <remarks>AUTH NEEDED</remarks>
1032+
public async Task<ErrorResponse> RemoveSavedAlbumsAsync(List<String> ids)
1033+
{
1034+
JArray array = new JArray(ids);
1035+
return await UploadDataAsync<ErrorResponse>(_builder.RemoveSavedAlbums(), array.ToString(Formatting.None), "DELETE") ?? new ErrorResponse();
1036+
}
1037+
1038+
/// <summary>
1039+
/// Check if one or more albums is already saved in the current Spotify user’s “Your Music” library.
1040+
/// </summary>
1041+
/// <param name="ids">A list of the Spotify IDs.</param>
1042+
/// <returns></returns>
1043+
/// <remarks>AUTH NEEDED</remarks>
1044+
public ListResponse<Boolean> CheckSavedAlbums(List<String> ids)
1045+
{
1046+
if (!UseAuth)
1047+
throw new InvalidOperationException("Auth is required for CheckSavedTracks");
1048+
JToken res = DownloadData<JToken>(_builder.CheckSavedAlbums(ids));
1049+
if (res is JArray)
1050+
return new ListResponse<Boolean> { List = res.ToObject<List<Boolean>>(), Error = null };
1051+
return new ListResponse<Boolean> { List = null, Error = res["error"].ToObject<Error>() };
1052+
}
1053+
1054+
/// <summary>
1055+
/// Check if one or more albums is already saved in the current Spotify user’s “Your Music” library asynchronously.
1056+
/// </summary>
1057+
/// <param name="ids">A list of the Spotify IDs.</param>
1058+
/// <returns></returns>
1059+
/// <remarks>AUTH NEEDED</remarks>
1060+
public async Task<ListResponse<Boolean>> CheckSavedAlbumsAsync(List<String> ids)
1061+
{
1062+
if (!UseAuth)
1063+
throw new InvalidOperationException("Auth is required for CheckSavedAlbumsAsync");
1064+
JToken res = await DownloadDataAsync<JToken>(_builder.CheckSavedAlbums(ids));
1065+
if (res is JArray)
1066+
return new ListResponse<Boolean> { List = res.ToObject<List<Boolean>>(), Error = null };
1067+
return new ListResponse<Boolean> { List = null, Error = res["error"].ToObject<Error>() };
1068+
}
1069+
9381070
#endregion Library
9391071

9401072
#region Playlists

SpotifyAPI/Web/SpotifyWebBuilder.cs

+50
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,56 @@ public string CheckSavedTracks(List<String> ids)
438438
return APIBase + "/me/tracks/contains?ids=" + string.Join(",", ids);
439439
}
440440

441+
/// <summary>
442+
/// Save one or more albums to the current user’s "Your Music" library.
443+
/// </summary>
444+
/// <returns></returns>
445+
/// <remarks>AUTH NEEDED</remarks>
446+
public string SaveAlbums()
447+
{
448+
return $"{APIBase}/me/albums";
449+
}
450+
451+
/// <summary>
452+
/// Get a list of the albums saved in the current Spotify user’s "Your Music" library.
453+
/// </summary>
454+
/// <param name="limit">The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50.</param>
455+
/// <param name="offset">The index of the first object to return. Default: 0 (i.e., the first object)</param>
456+
/// <param name="market">An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking.</param>
457+
/// <returns></returns>
458+
/// <remarks>AUTH NEEDED</remarks>
459+
public string GetSavedAlbums(int limit = 20, int offset = 0, string market = "")
460+
{
461+
limit = Math.Min(limit, 50);
462+
StringBuilder builder = new StringBuilder(APIBase + "/me/albums");
463+
builder.Append("?limit=" + limit);
464+
builder.Append("&offset=" + offset);
465+
if (!string.IsNullOrEmpty(market))
466+
builder.Append("&market=" + market);
467+
return builder.ToString();
468+
}
469+
470+
/// <summary>
471+
/// Remove one or more albums from the current user’s "Your Music" library.
472+
/// </summary>
473+
/// <returns></returns>
474+
/// <remarks>AUTH NEEDED</remarks>
475+
public string RemoveSavedAlbums()
476+
{
477+
return APIBase + "/me/albums/";
478+
}
479+
480+
/// <summary>
481+
/// Check if one or more albums is already saved in the current Spotify user’s "Your Music" library.
482+
/// </summary>
483+
/// <param name="ids">A list of the Spotify IDs.</param>
484+
/// <returns></returns>
485+
/// <remarks>AUTH NEEDED</remarks>
486+
public string CheckSavedAlbums(List<String> ids)
487+
{
488+
return APIBase + "/me/tracks/contains?ids=" + string.Join(",", ids);
489+
}
490+
441491
#endregion Library
442492

443493
#region Playlists

0 commit comments

Comments
 (0)