Added Library Client #451

This commit is contained in:
Jonas Dellinger 2020-05-20 08:48:08 +02:00
parent 39d0260bbd
commit 320aa5f134
20 changed files with 387 additions and 0 deletions

View File

@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SpotifyAPI.Web
{
public interface ILibraryClient
{
Task<bool> RemoveAlbums(LibraryRemoveAlbumsRequest request);
Task<bool> RemoveTracks(LibraryRemoveTracksRequest request);
Task<bool> RemoveShows(LibraryRemoveShowsRequest request);
Task<bool> SaveTracks(LibrarySaveTracksRequest request);
Task<bool> SaveAlbums(LibrarySaveAlbumsRequest request);
Task<bool> SaveShows(LibrarySaveShowsRequest request);
Task<List<bool>> CheckTracks(LibraryCheckTracksRequest request);
Task<List<bool>> CheckAlbums(LibraryCheckAlbumsRequest request);
Task<List<bool>> CheckShows(LibraryCheckShowsRequest request);
Task<Paging<SavedTrack>> GetTracks();
Task<Paging<SavedTrack>> GetTracks(LibraryTracksRequest request);
Task<Paging<SavedAlbum>> GetAlbums();
Task<Paging<SavedAlbum>> GetAlbums(LibraryAlbumsRequest request);
Task<Paging<SavedShow>> GetShows();
Task<Paging<SavedShow>> GetShows(LibraryShowsRequest request);
}
}

View File

@ -32,6 +32,8 @@ namespace SpotifyAPI.Web
IEpisodesClient Episodes { get; }
ILibraryClient Library { get; }
Task<List<T>> Paginate<T>(Paging<T> firstPage);
Task<List<T>> Paginate<T, TNext>(Paging<T, TNext> firstPage, Func<TNext, Paging<T, TNext>> mapper);
Task<List<T>> Paginate<T>(Func<Task<Paging<T>>> getFirstPage);

View File

@ -0,0 +1,117 @@
using System.Net;
using System.Collections.Generic;
using System.Threading.Tasks;
using SpotifyAPI.Web.Http;
namespace SpotifyAPI.Web
{
public class LibraryClient : APIClient, ILibraryClient
{
public LibraryClient(IAPIConnector apiConnector) : base(apiConnector) { }
public Task<List<bool>> CheckAlbums(LibraryCheckAlbumsRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<List<bool>>(SpotifyUrls.LibraryAlbumsContains(), request.BuildQueryParams());
}
public Task<List<bool>> CheckShows(LibraryCheckShowsRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<List<bool>>(SpotifyUrls.LibraryShowsContains(), request.BuildQueryParams());
}
public Task<List<bool>> CheckTracks(LibraryCheckTracksRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<List<bool>>(SpotifyUrls.LibraryTracksContains(), request.BuildQueryParams());
}
public Task<Paging<SavedAlbum>> GetAlbums()
{
return API.Get<Paging<SavedAlbum>>(SpotifyUrls.LibraryAlbums());
}
public Task<Paging<SavedAlbum>> GetAlbums(LibraryAlbumsRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<Paging<SavedAlbum>>(SpotifyUrls.LibraryAlbums(), request.BuildQueryParams());
}
public Task<Paging<SavedShow>> GetShows()
{
return API.Get<Paging<SavedShow>>(SpotifyUrls.LibraryShows());
}
public Task<Paging<SavedShow>> GetShows(LibraryShowsRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<Paging<SavedShow>>(SpotifyUrls.LibraryShows(), request.BuildQueryParams());
}
public Task<Paging<SavedTrack>> GetTracks()
{
return API.Get<Paging<SavedTrack>>(SpotifyUrls.LibraryTracks());
}
public Task<Paging<SavedTrack>> GetTracks(LibraryTracksRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<Paging<SavedTrack>>(SpotifyUrls.LibraryTracks(), request.BuildQueryParams());
}
public async Task<bool> RemoveAlbums(LibraryRemoveAlbumsRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
var statusCode = await API.Delete(SpotifyUrls.LibraryAlbums(), null, request.BuildBodyParams()).ConfigureAwait(false);
return statusCode == HttpStatusCode.OK;
}
public async Task<bool> RemoveShows(LibraryRemoveShowsRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
var statusCode = await API.Delete(SpotifyUrls.LibraryShows(), null, request.BuildBodyParams()).ConfigureAwait(false);
return statusCode == HttpStatusCode.OK;
}
public async Task<bool> RemoveTracks(LibraryRemoveTracksRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
var statusCode = await API.Delete(SpotifyUrls.LibraryTracks(), null, request.BuildBodyParams()).ConfigureAwait(false);
return statusCode == HttpStatusCode.OK;
}
public async Task<bool> SaveAlbums(LibrarySaveAlbumsRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
var statusCode = await API.Put(SpotifyUrls.LibraryAlbums(), request.BuildQueryParams(), null).ConfigureAwait(false);
return statusCode == HttpStatusCode.Created;
}
public async Task<bool> SaveShows(LibrarySaveShowsRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
var statusCode = await API.Put(SpotifyUrls.LibraryShows(), request.BuildQueryParams(), null).ConfigureAwait(false);
return statusCode == HttpStatusCode.Created;
}
public async Task<bool> SaveTracks(LibrarySaveTracksRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
var statusCode = await API.Put(SpotifyUrls.LibraryTracks(), request.BuildQueryParams(), null).ConfigureAwait(false);
return statusCode == HttpStatusCode.Created;
}
}
}

View File

@ -42,6 +42,7 @@ namespace SpotifyAPI.Web
Artists = new ArtistsClient(_apiConnector);
Personalization = new PersonalizationClient(_apiConnector);
Episodes = new EpisodesClient(_apiConnector);
Library = new LibraryClient(_apiConnector);
}
public IPaginator DefaultPaginator { get; }
@ -70,6 +71,8 @@ namespace SpotifyAPI.Web
public IEpisodesClient Episodes { get; }
public ILibraryClient Library { get; }
public Task<List<T>> Paginate<T>(Paging<T> firstPage)
{
return DefaultPaginator.Paginate(firstPage, _apiConnector);

View File

@ -0,0 +1,14 @@
namespace SpotifyAPI.Web
{
public class LibraryAlbumsRequest : RequestParams
{
[QueryParam("limit")]
public int? Limit { get; set; }
[QueryParam("offset")]
public int? Offset { get; set; }
[QueryParam("market")]
public string Market { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class LibraryCheckAlbumsRequest : RequestParams
{
public LibraryCheckAlbumsRequest(IList<string> ids)
{
Ensure.ArgumentNotNull(ids, nameof(ids));
Ids = ids;
}
[QueryParam("ids")]
public IList<string> Ids { get; }
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class LibraryCheckShowsRequest : RequestParams
{
public LibraryCheckShowsRequest(IList<string> ids)
{
Ensure.ArgumentNotNull(ids, nameof(ids));
Ids = ids;
}
[QueryParam("ids")]
public IList<string> Ids { get; }
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class LibraryCheckTracksRequest : RequestParams
{
public LibraryCheckTracksRequest(IList<string> ids)
{
Ensure.ArgumentNotNull(ids, nameof(ids));
Ids = ids;
}
[QueryParam("ids")]
public IList<string> Ids { get; }
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class LibraryRemoveAlbumsRequest : RequestParams
{
public LibraryRemoveAlbumsRequest(IList<string> ids)
{
Ensure.ArgumentNotNullOrEmptyList(ids, nameof(ids));
Ids = ids;
}
[BodyParam("ids")]
public IList<string> Ids { get; }
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class LibraryRemoveShowsRequest : RequestParams
{
public LibraryRemoveShowsRequest(IList<string> ids)
{
Ensure.ArgumentNotNullOrEmptyList(ids, nameof(ids));
Ids = ids;
}
[BodyParam("ids")]
public IList<string> Ids { get; }
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class LibraryRemoveTracksRequest : RequestParams
{
public LibraryRemoveTracksRequest(IList<string> ids)
{
Ensure.ArgumentNotNullOrEmptyList(ids, nameof(ids));
Ids = ids;
}
[BodyParam("ids")]
public IList<string> Ids { get; }
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class LibrarySaveAlbumsRequest : RequestParams
{
public LibrarySaveAlbumsRequest(IList<string> ids)
{
Ensure.ArgumentNotNullOrEmptyList(ids, nameof(ids));
Ids = ids;
}
[QueryParam("ids")]
public IList<string> Ids { get; }
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class LibrarySaveShowsRequest : RequestParams
{
public LibrarySaveShowsRequest(IList<string> ids)
{
Ensure.ArgumentNotNullOrEmptyList(ids, nameof(ids));
Ids = ids;
}
[QueryParam("ids")]
public IList<string> Ids { get; }
}
}

View File

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class LibrarySaveTracksRequest : RequestParams
{
public LibrarySaveTracksRequest(IList<string> ids)
{
Ensure.ArgumentNotNullOrEmptyList(ids, nameof(ids));
Ids = ids;
}
[QueryParam("ids")]
public IList<string> Ids { get; }
}
}

View File

@ -0,0 +1,14 @@
namespace SpotifyAPI.Web
{
public class LibraryShowsRequest : RequestParams
{
[QueryParam("limit")]
public int? Limit { get; set; }
[QueryParam("offset")]
public int? Offset { get; set; }
[QueryParam("market")]
public string Market { get; set; }
}
}

View File

@ -0,0 +1,14 @@
namespace SpotifyAPI.Web
{
public class LibraryTracksRequest : RequestParams
{
[QueryParam("limit")]
public int? Limit { get; set; }
[QueryParam("offset")]
public int? Offset { get; set; }
[QueryParam("market")]
public string Market { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using System;
namespace SpotifyAPI.Web
{
public class SavedAlbum
{
public DateTime AddedAt { get; set; }
public FullAlbum Album { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using System;
namespace SpotifyAPI.Web
{
public class SavedShow
{
public DateTime AddedAt { get; set; }
public FullShow Show { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using System;
namespace SpotifyAPI.Web
{
public class SavedTrack
{
public DateTime AddedAt { get; set; }
public FullTrack Track { get; set; }
}
}

View File

@ -113,6 +113,18 @@ namespace SpotifyAPI.Web
public static Uri Episodes() => EUri($"episodes");
public static Uri LibraryAlbumsContains() => EUri($"me/albums/contains");
public static Uri LibraryAlbums() => EUri($"me/albums");
public static Uri LibraryTracksContains() => EUri($"me/tracks/contains");
public static Uri LibraryTracks() => EUri($"me/tracks");
public static Uri LibraryShowsContains() => EUri($"me/shows/contains");
public static Uri LibraryShows() => EUri($"me/shows");
private static Uri EUri(FormattableString path) => new Uri(path.ToString(_provider), UriKind.Relative);
}
}