Spotify.NET/SpotifyAPI.Web/Clients/PlaylistsClient.cs

138 lines
5.8 KiB
C#
Raw Normal View History

2020-05-03 07:10:41 +01:00
using System.Collections.Generic;
using System.Net;
using System.Threading;
2020-05-02 21:48:21 +01:00
using System.Threading.Tasks;
using SpotifyAPI.Web.Http;
using URLs = SpotifyAPI.Web.SpotifyUrls;
namespace SpotifyAPI.Web
{
public class PlaylistsClient : APIClient, IPlaylistsClient
{
public PlaylistsClient(IAPIConnector connector) : base(connector) { }
public Task<SnapshotResponse> RemoveItems(string playlistId, PlaylistRemoveItemsRequest request, CancellationToken cancel = default)
2020-05-02 21:48:21 +01:00
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Delete<SnapshotResponse>(URLs.PlaylistTracks(playlistId), null, request.BuildBodyParams(), cancel);
2020-05-02 21:48:21 +01:00
}
2020-05-03 00:00:35 +01:00
public Task<SnapshotResponse> AddItems(string playlistId, PlaylistAddItemsRequest request, CancellationToken cancel = default)
2020-05-03 00:00:35 +01:00
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Post<SnapshotResponse>(URLs.PlaylistTracks(playlistId), null, request.BuildBodyParams(), cancel);
2020-05-03 00:00:35 +01:00
}
public Task<Paging<PlaylistTrack<IPlayableItem>>> GetItems(string playlistId, CancellationToken cancel = default)
2020-05-03 00:00:35 +01:00
{
2020-05-03 08:06:28 +01:00
var request = new PlaylistGetItemsRequest();
2020-05-03 00:00:35 +01:00
return GetItems(playlistId, request, cancel);
2020-05-03 00:00:35 +01:00
}
public Task<Paging<PlaylistTrack<IPlayableItem>>> GetItems(string playlistId, PlaylistGetItemsRequest request, CancellationToken cancel = default)
2020-05-03 00:00:35 +01:00
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<Paging<PlaylistTrack<IPlayableItem>>>(URLs.PlaylistTracks(playlistId), request.BuildQueryParams(), cancel);
2020-05-03 00:00:35 +01:00
}
public Task<FullPlaylist> Create(string userId, PlaylistCreateRequest request, CancellationToken cancel = default)
2020-05-03 00:00:35 +01:00
{
Ensure.ArgumentNotNullOrEmptyString(userId, nameof(userId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Post<FullPlaylist>(URLs.UserPlaylists(userId), null, request.BuildBodyParams(), cancel);
2020-05-03 00:00:35 +01:00
}
2020-05-03 07:10:41 +01:00
public async Task<bool> UploadCover(string playlistId, string base64JpgData, CancellationToken cancel = default)
2020-05-03 07:10:41 +01:00
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNullOrEmptyString(base64JpgData, nameof(base64JpgData));
2020-05-03 07:10:41 +01:00
var statusCode = await API.PutRaw(URLs.PlaylistImages(playlistId), null, base64JpgData, cancel).ConfigureAwait(false);
2020-05-03 08:06:28 +01:00
return statusCode == HttpStatusCode.Accepted;
2020-05-03 07:10:41 +01:00
}
public Task<List<Image>> GetCovers(string playlistId, CancellationToken cancel = default)
2020-05-03 07:10:41 +01:00
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
return API.Get<List<Image>>(URLs.PlaylistImages(playlistId), cancel);
2020-05-03 07:10:41 +01:00
}
2020-05-03 08:06:28 +01:00
2023-05-27 21:20:33 +01:00
public Task<Paging<FullPlaylist>> GetUsers(string userId, CancellationToken cancel = default)
2020-05-03 08:06:28 +01:00
{
Ensure.ArgumentNotNullOrEmptyString(userId, nameof(userId));
2023-05-27 21:20:33 +01:00
return API.Get<Paging<FullPlaylist>>(URLs.UserPlaylists(userId), cancel);
2020-05-03 08:06:28 +01:00
}
2023-05-27 21:20:33 +01:00
public Task<Paging<FullPlaylist>> GetUsers(string userId, PlaylistGetUsersRequest request, CancellationToken cancel = default)
2020-05-03 08:06:28 +01:00
{
Ensure.ArgumentNotNullOrEmptyString(userId, nameof(userId));
Ensure.ArgumentNotNull(request, nameof(request));
2023-05-27 21:20:33 +01:00
return API.Get<Paging<FullPlaylist>>(URLs.UserPlaylists(userId), request.BuildQueryParams(), cancel);
2020-05-03 08:06:28 +01:00
}
public Task<FullPlaylist> Get(string playlistId, CancellationToken cancel = default)
2020-05-03 08:06:28 +01:00
{
var request = new PlaylistGetRequest(); // We need some defaults
return Get(playlistId, request, cancel);
2020-05-03 08:06:28 +01:00
}
public Task<FullPlaylist> Get(string playlistId, PlaylistGetRequest request, CancellationToken cancel = default)
2020-05-03 08:06:28 +01:00
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<FullPlaylist>(URLs.Playlist(playlistId), request.BuildQueryParams(), cancel);
2020-05-03 08:06:28 +01:00
}
public async Task<bool> ReplaceItems(string playlistId, PlaylistReplaceItemsRequest request, CancellationToken cancel = default)
2020-05-03 08:06:28 +01:00
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
var statusCode = await API.Put(URLs.PlaylistTracks(playlistId), null, request.BuildBodyParams(), cancel).ConfigureAwait(false);
2020-05-03 08:06:28 +01:00
return statusCode == HttpStatusCode.Created;
}
2023-05-27 21:20:33 +01:00
public Task<Paging<FullPlaylist>> CurrentUsers(CancellationToken cancel = default)
2020-05-03 08:06:28 +01:00
{
2023-05-27 21:20:33 +01:00
return API.Get<Paging<FullPlaylist>>(URLs.CurrentUserPlaylists(), cancel);
2020-05-03 08:06:28 +01:00
}
2023-05-27 21:20:33 +01:00
public Task<Paging<FullPlaylist>> CurrentUsers(PlaylistCurrentUsersRequest request, CancellationToken cancel = default)
2020-05-03 08:06:28 +01:00
{
Ensure.ArgumentNotNull(request, nameof(request));
2023-05-27 21:20:33 +01:00
return API.Get<Paging<FullPlaylist>>(URLs.CurrentUserPlaylists(), request.BuildQueryParams(), cancel);
2020-05-03 08:06:28 +01:00
}
public async Task<bool> ChangeDetails(string playlistId, PlaylistChangeDetailsRequest request, CancellationToken cancel = default)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
var statusCode = await API.Put(URLs.Playlist(playlistId), null, request.BuildBodyParams(), cancel).ConfigureAwait(false);
return statusCode == HttpStatusCode.OK;
}
public Task<SnapshotResponse> ReorderItems(string playlistId, PlaylistReorderItemsRequest request, CancellationToken cancel = default)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Put<SnapshotResponse>(URLs.PlaylistTracks(playlistId), null, request.BuildBodyParams(), cancel);
}
2020-05-02 21:48:21 +01:00
}
}