Added more playlists endpoint

This commit is contained in:
Jonas Dellinger 2020-05-03 08:10:41 +02:00
parent 93bcff5c1d
commit 5d071de190
6 changed files with 71 additions and 6 deletions

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace SpotifyAPI.Web namespace SpotifyAPI.Web
@ -12,5 +13,9 @@ namespace SpotifyAPI.Web
Task<Paging<PlaylistTrack<IPlaylistItem>>> GetItems(string playlistId, PlaylistGetItemsRequest request); Task<Paging<PlaylistTrack<IPlaylistItem>>> GetItems(string playlistId, PlaylistGetItemsRequest request);
Task<FullPlaylist> Create(string userId, PlaylistCreateRequest request); Task<FullPlaylist> Create(string userId, PlaylistCreateRequest request);
Task<bool> UploadCover(string playlistId, string base64JpgData);
Task<List<Image>> GetCovers(string playlistId);
} }
} }

View File

@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using SpotifyAPI.Web.Http; using SpotifyAPI.Web.Http;
using URLs = SpotifyAPI.Web.SpotifyUrls; using URLs = SpotifyAPI.Web.SpotifyUrls;
@ -46,5 +48,21 @@ namespace SpotifyAPI.Web
return API.Post<FullPlaylist>(URLs.UserPlaylists(userId), null, request.BuildBodyParams()); return API.Post<FullPlaylist>(URLs.UserPlaylists(userId), null, request.BuildBodyParams());
} }
public async Task<bool> UploadCover(string playlistId, string base64Jpg)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNullOrEmptyString(base64Jpg, nameof(base64Jpg));
var response = await API.PutRaw(URLs.PlaylistImages(playlistId), null, base64Jpg);
return response == HttpStatusCode.Accepted;
}
public Task<List<Image>> GetCovers(string playlistId)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
return API.Get<List<Image>>(URLs.PlaylistImages(playlistId));
}
} }
} }

View File

@ -108,22 +108,30 @@ namespace SpotifyAPI.Web.Http
return SendAPIRequest<T>(uri, HttpMethod.Put, parameters, body); return SendAPIRequest<T>(uri, HttpMethod.Put, parameters, body);
} }
public async Task<HttpStatusCode> PutRaw(Uri uri, IDictionary<string, string> parameters, object body)
{
Ensure.ArgumentNotNull(uri, nameof(uri));
var response = await SendRawRequest(uri, HttpMethod.Put, parameters, body);
return response.StatusCode;
}
public void SetRequestTimeout(TimeSpan timeout) public void SetRequestTimeout(TimeSpan timeout)
{ {
_httpClient.SetRequestTimeout(timeout); _httpClient.SetRequestTimeout(timeout);
} }
public async Task<T> SendAPIRequest<T>( private IRequest CreateRequest(
Uri uri, Uri uri,
HttpMethod method, HttpMethod method,
IDictionary<string, string> parameters = null, IDictionary<string, string> parameters,
object body = null object body
) )
{ {
Ensure.ArgumentNotNull(uri, nameof(uri)); Ensure.ArgumentNotNull(uri, nameof(uri));
Ensure.ArgumentNotNull(method, nameof(method)); Ensure.ArgumentNotNull(method, nameof(method));
var request = new Request return new Request
{ {
BaseAddress = _baseAddress, BaseAddress = _baseAddress,
Parameters = parameters, Parameters = parameters,
@ -131,8 +139,17 @@ namespace SpotifyAPI.Web.Http
Method = method, Method = method,
Body = body Body = body
}; };
}
private async Task<IAPIResponse<T>> SendSerializedRequest<T>(IRequest request)
{
_jsonSerializer.SerializeRequest(request); _jsonSerializer.SerializeRequest(request);
var response = await SendRequest(request);
return _jsonSerializer.DeserializeResponse<T>(response);
}
private async Task<IResponse> SendRequest(IRequest request)
{
await _authenticator.Apply(request).ConfigureAwait(false); await _authenticator.Apply(request).ConfigureAwait(false);
IResponse response = await _httpClient.DoRequest(request).ConfigureAwait(false); IResponse response = await _httpClient.DoRequest(request).ConfigureAwait(false);
if (_retryHandler != null) if (_retryHandler != null)
@ -144,8 +161,29 @@ namespace SpotifyAPI.Web.Http
}); });
} }
ProcessErrors(response); ProcessErrors(response);
return response;
}
IAPIResponse<T> apiResponse = _jsonSerializer.DeserializeResponse<T>(response); public Task<IResponse> SendRawRequest(
Uri uri,
HttpMethod method,
IDictionary<string, string> parameters = null,
object body = null
)
{
var request = CreateRequest(uri, method, parameters, body);
return SendRequest(request);
}
public async Task<T> SendAPIRequest<T>(
Uri uri,
HttpMethod method,
IDictionary<string, string> parameters = null,
object body = null
)
{
var request = CreateRequest(uri, method, parameters, body);
IAPIResponse<T> apiResponse = await SendSerializedRequest<T>(request);
return apiResponse.Body; return apiResponse.Body;
} }

View File

@ -1,3 +1,4 @@
using System.Net;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
@ -23,6 +24,7 @@ namespace SpotifyAPI.Web.Http
Task<T> Put<T>(Uri uri); Task<T> Put<T>(Uri uri);
Task<T> Put<T>(Uri uri, IDictionary<string, string> parameters); Task<T> Put<T>(Uri uri, IDictionary<string, string> parameters);
Task<T> Put<T>(Uri uri, IDictionary<string, string> parameters, object body); Task<T> Put<T>(Uri uri, IDictionary<string, string> parameters, object body);
Task<HttpStatusCode> PutRaw(Uri uri, IDictionary<string, string> parameters, object body);
Task<T> Delete<T>(Uri uri); Task<T> Delete<T>(Uri uri);
Task<T> Delete<T>(Uri uri, IDictionary<string, string> parameters); Task<T> Delete<T>(Uri uri, IDictionary<string, string> parameters);

View File

@ -27,7 +27,7 @@ namespace SpotifyAPI.Web.Http
{ {
Ensure.ArgumentNotNull(response, nameof(response)); Ensure.ArgumentNotNull(response, nameof(response));
if (response.ContentType?.Equals("application/json", StringComparison.Ordinal) is true) if (response.ContentType?.Equals("application/json", StringComparison.Ordinal) is true || response.ContentType == null)
{ {
var body = JsonConvert.DeserializeObject<T>(response.Body as string, _serializerSettings); var body = JsonConvert.DeserializeObject<T>(response.Body as string, _serializerSettings);
return new APIResponse<T>(response, body); return new APIResponse<T>(response, body);

View File

@ -35,6 +35,8 @@ namespace SpotifyAPI.Web
public static Uri UserPlaylists(string userId) => EUri($"users/{userId}/playlists"); public static Uri UserPlaylists(string userId) => EUri($"users/{userId}/playlists");
public static Uri PlaylistImages(string playlistId) => EUri($"playlists/{playlistId}/images");
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);
} }
} }