Added some more playlists endpoints

This commit is contained in:
Jonas Dellinger 2020-05-03 01:00:35 +02:00
parent 870b13eb2d
commit 93bcff5c1d
13 changed files with 133 additions and 5 deletions

View File

@ -5,5 +5,12 @@ namespace SpotifyAPI.Web
public interface IPlaylistsClient public interface IPlaylistsClient
{ {
Task<SnapshotResponse> RemoveItems(string playlistId, PlaylistRemoveItemsRequest request); Task<SnapshotResponse> RemoveItems(string playlistId, PlaylistRemoveItemsRequest request);
Task<SnapshotResponse> AddItems(string playlistId, PlaylistAddItemsRequest request);
Task<Paging<PlaylistTrack<IPlaylistItem>>> GetItems(string playlistId);
Task<Paging<PlaylistTrack<IPlaylistItem>>> GetItems(string playlistId, PlaylistGetItemsRequest request);
Task<FullPlaylist> Create(string userId, PlaylistCreateRequest request);
} }
} }

View File

@ -15,5 +15,36 @@ namespace SpotifyAPI.Web
return API.Delete<SnapshotResponse>(URLs.PlaylistTracks(playlistId), null, request.BuildBodyParams()); return API.Delete<SnapshotResponse>(URLs.PlaylistTracks(playlistId), null, request.BuildBodyParams());
} }
public Task<SnapshotResponse> AddItems(string playlistId, PlaylistAddItemsRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Post<SnapshotResponse>(URLs.PlaylistTracks(playlistId), null, request.BuildBodyParams());
}
public Task<Paging<PlaylistTrack<IPlaylistItem>>> GetItems(string playlistId)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
return API.Get<Paging<PlaylistTrack<IPlaylistItem>>>(URLs.PlaylistTracks(playlistId));
}
public Task<Paging<PlaylistTrack<IPlaylistItem>>> GetItems(string playlistId, PlaylistGetItemsRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<Paging<PlaylistTrack<IPlaylistItem>>>(URLs.PlaylistTracks(playlistId), request.BuildQueryParams());
}
public Task<FullPlaylist> Create(string userId, PlaylistCreateRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(userId, nameof(userId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Post<FullPlaylist>(URLs.UserPlaylists(userId), null, request.BuildBodyParams());
}
} }
} }

View File

@ -84,7 +84,7 @@ namespace SpotifyAPI.Web.Http
{ {
Ensure.ArgumentNotNull(uri, nameof(uri)); Ensure.ArgumentNotNull(uri, nameof(uri));
return SendAPIRequest<T>(uri, HttpMethod.Get, parameters, body); return SendAPIRequest<T>(uri, HttpMethod.Post, parameters, body);
} }
public Task<T> Put<T>(Uri uri) public Task<T> Put<T>(Uri uri)

View File

@ -1,3 +1,4 @@
using System.Text;
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -62,7 +63,7 @@ namespace SpotifyAPI.Web.Http
requestMsg.Content = body; requestMsg.Content = body;
break; break;
case string body: case string body:
requestMsg.Content = new StringContent(body); requestMsg.Content = new StringContent(body, Encoding.UTF8, "application/json");
break; break;
case Stream body: case Stream body:
requestMsg.Content = new StreamContent(body); requestMsg.Content = new StreamContent(body);

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace SpotifyAPI.Web
{
public class PlaylistAddItemsRequest : RequestParams
{
[BodyParam("uris")]
public List<string> Uris { get; set; }
[BodyParam("position")]
public int? Position { get; set; }
}
}

View File

@ -0,0 +1,22 @@
namespace SpotifyAPI.Web
{
public class PlaylistCreateRequest : RequestParams
{
public PlaylistCreateRequest(string name)
{
Name = name;
}
[BodyParam("name")]
public string Name { get; set; }
[BodyParam("public")]
public bool? Public { get; set; }
[BodyParam("collaborative")]
public bool? Collaborative { get; set; }
[BodyParam("description")]
public string Description { get; set; }
}
}

View File

@ -0,0 +1,22 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class PlaylistGetItemsRequest : RequestParams
{
[QueryParam("fields")]
public List<string> Fields { get; set; }
[QueryParam("limit")]
public int? Limit { get; set; }
[QueryParam("offset")]
public int? Offset { get; set; }
[QueryParam("market")]
public string Market { get; set; }
[QueryParam("additional_types")]
public List<string> AdditionalTypes { get; set; }
}
}

View File

@ -5,6 +5,11 @@ namespace SpotifyAPI.Web
{ {
public class PlaylistRemoveItemsRequest : RequestParams public class PlaylistRemoveItemsRequest : RequestParams
{ {
public PlaylistRemoveItemsRequest(List<Item> tracks)
{
Tracks = tracks;
}
[BodyParam("tracks")] [BodyParam("tracks")]
public List<Item> Tracks { get; set; } public List<Item> Tracks { get; set; }
[BodyParam("snapshot_id")] [BodyParam("snapshot_id")]

View File

@ -4,6 +4,10 @@ namespace SpotifyAPI.Web
{ {
public class ShowsRequest : RequestParams public class ShowsRequest : RequestParams
{ {
public ShowsRequest(List<string> ids)
{
Ids = ids;
}
[QueryParam("ids")] [QueryParam("ids")]
public List<string> Ids { get; set; } public List<string> Ids { get; set; }

View File

@ -0,0 +1,19 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class FullPlaylist
{
public bool Collaborative { get; set; }
public Dictionary<string, string> ExternalUrls { get; set; }
public string Href { get; set; }
public string Id { get; set; }
public List<Image> Images { get; set; }
public string Name { get; set; }
public PublicUser Owner { get; set; }
public bool Public { get; set; }
public string SnapshotId { get; set; }
public Paging<PlaylistTrack<IPlaylistItem>> Tracks { get; set; }
public string Type { get; set; }
public string Uri { get; set; }
}
}

View File

@ -3,12 +3,13 @@ using Newtonsoft.Json;
namespace SpotifyAPI.Web namespace SpotifyAPI.Web
{ {
public class PlaylistTrack public class PlaylistTrack<T>
{ {
public DateTime? AddedAt { get; set; } public DateTime? AddedAt { get; set; }
public PublicUser AddedBy { get; set; } public PublicUser AddedBy { get; set; }
public bool IsLocal { get; set; } public bool IsLocal { get; set; }
[JsonConverter(typeof(PlaylistElementConverter))] [JsonConverter(typeof(PlaylistElementConverter))]
public IPlaylistItem Track { get; set; } public T Track { get; set; }
} }
} }

View File

@ -16,7 +16,7 @@ namespace SpotifyAPI.Web
public PublicUser Owner { get; set; } public PublicUser Owner { get; set; }
public bool? Public { get; set; } public bool? Public { get; set; }
public string SnapshotId { get; set; } public string SnapshotId { get; set; }
public Paging<PlaylistTrack> Tracks { get; set; } public Paging<PlaylistTrack<IPlaylistItem>> Tracks { get; set; }
public string Type { get; set; } public string Type { get; set; }
public string Uri { get; set; } public string Uri { get; set; }
} }

View File

@ -33,6 +33,8 @@ namespace SpotifyAPI.Web
public static Uri PlaylistTracks(string playlistId) => EUri($"playlists/{playlistId}/tracks"); public static Uri PlaylistTracks(string playlistId) => EUri($"playlists/{playlistId}/tracks");
public static Uri UserPlaylists(string userId) => EUri($"users/{userId}/playlists");
private static Uri EUri(FormattableString path) => new Uri(path.ToString(_provider), UriKind.Relative); private static Uri EUri(FormattableString path) => new Uri(path.ToString(_provider), UriKind.Relative);
} }
} }