Allow Enum in QueryParams and added more endpoints

This commit is contained in:
Jonas Dellinger 2020-05-03 22:34:03 +02:00
parent c9a374080b
commit 7c94de2110
18 changed files with 222 additions and 15 deletions

View File

@ -29,5 +29,7 @@ namespace SpotifyAPI.Web
Task<Paging<SimplePlaylist>> CurrentUsers(PlaylistCurrentUsersRequest request);
Task<bool> ChangeDetails(string playlistId, PlaylistChangeDetailsRequest request);
Task<SnapshotResponse> ReorderItems(string playlistId, PlaylistReorderItemsRequest request);
}
}

View File

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace SpotifyAPI.Web
{
public interface ISearchClient
{
Task<SearchResponse> Item(SearchRequest request);
}
}

View File

@ -9,5 +9,7 @@ namespace SpotifyAPI.Web
IShowsClient Shows { get; }
IPlaylistsClient Playlists { get; }
ISearchClient Search { get; }
}
}

View File

@ -126,5 +126,13 @@ namespace SpotifyAPI.Web
var statusCode = await API.Put(URLs.Playlist(playlistId), null, request.BuildBodyParams());
return statusCode == HttpStatusCode.OK;
}
public Task<SnapshotResponse> ReorderItems(string playlistId, PlaylistReorderItemsRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Put<SnapshotResponse>(URLs.PlaylistTracks(playlistId), null, request.BuildBodyParams());
}
}
}

View File

@ -0,0 +1,18 @@
using System.Threading.Tasks;
using SpotifyAPI.Web.Http;
using URLs = SpotifyAPI.Web.SpotifyUrls;
namespace SpotifyAPI.Web
{
public class SearchClient : APIClient, ISearchClient
{
public SearchClient(IAPIConnector apiConnector) : base(apiConnector) { }
public Task<SearchResponse> Item(SearchRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<SearchResponse>(URLs.Search(), request.BuildQueryParams());
}
}
}

View File

@ -19,6 +19,7 @@ namespace SpotifyAPI.Web
Browse = new BrowseClient(_apiConnector);
Shows = new ShowsClient(_apiConnector);
Playlists = new PlaylistsClient(_apiConnector);
Search = new SearchClient(_apiConnector);
}
public IUserProfileClient UserProfile { get; }
@ -28,5 +29,7 @@ namespace SpotifyAPI.Web
public IShowsClient Shows { get; }
public IPlaylistsClient Playlists { get; }
public ISearchClient Search { get; }
}
}

View File

@ -43,8 +43,7 @@ namespace SpotifyAPI.Web
internal IAPIConnector CreateAPIConnector()
{
Ensure.ArgumentNotNull(BaseAddress, nameof(BaseAddress));
Ensure.ArgumentNotNull(Authenticator, nameof(Authenticator),
". Use WithToken or WithAuthenticator to specify a authentication");
Ensure.ArgumentNotNull(Authenticator, nameof(Authenticator));
Ensure.ArgumentNotNull(JSONSerializer, nameof(JSONSerializer));
Ensure.ArgumentNotNull(HTTPClient, nameof(HTTPClient));

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
namespace SpotifyAPI.Web
@ -6,7 +7,7 @@ namespace SpotifyAPI.Web
{
public PlaylistGetItemsRequest()
{
AdditionalTypes = new List<string>() { "track", "episode" };
AdditionalTypes = AdditionalType.All;
}
[QueryParam("fields")]
@ -26,6 +27,16 @@ namespace SpotifyAPI.Web
/// </summary>
/// <value></value>
[QueryParam("additional_types")]
public List<string> AdditionalTypes { get; set; }
public AdditionalType AdditionalTypes { get; set; }
[Flags]
public enum AdditionalType
{
[String("track")]
Track = 0,
[String("episode")]
Episode = 1,
All = Track | Episode
}
}
}

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
namespace SpotifyAPI.Web
@ -6,7 +7,7 @@ namespace SpotifyAPI.Web
{
public PlaylistGetRequest()
{
AdditionalTypes = new List<string> { "track", "episode" };
AdditionalTypes = AdditionalType.All;
}
/// <summary>
@ -14,6 +15,16 @@ namespace SpotifyAPI.Web
/// </summary>
/// <value></value>
[QueryParam("additional_types")]
public List<string> AdditionalTypes { get; set; }
public AdditionalType AdditionalTypes { get; set; }
[Flags]
public enum AdditionalType
{
[String("track")]
Track = 0,
[String("episode")]
Episode = 1,
All = Track | Episode
}
}
}

View File

@ -5,13 +5,9 @@ namespace SpotifyAPI.Web
{
public class PlaylistRemoveItemsRequest : RequestParams
{
public PlaylistRemoveItemsRequest(List<Item> tracks)
{
Tracks = tracks;
}
[BodyParam("tracks")]
public List<Item> Tracks { get; set; }
[BodyParam("snapshot_id")]
public string SnapshotId { get; set; }
@ -26,8 +22,12 @@ namespace SpotifyAPI.Web
{
Uri = uri;
}
[JsonProperty("uri")]
public string Uri { get; set; }
[JsonProperty("positions", NullValueHandling = NullValueHandling.Ignore)]
public List<int> Positions { get; set; }
}
}
}

View File

@ -0,0 +1,23 @@
namespace SpotifyAPI.Web
{
public class PlaylistReorderItemsRequest : RequestParams
{
[BodyParam("range_start")]
public int? RangeStart { get; set; }
[BodyParam("insert_before")]
public int? InsertBefore { get; set; }
[BodyParam("range_length")]
public int? RangeLength { get; set; }
[BodyParam("snapshot_id")]
public string SnapshotId { get; set; }
protected override void CustomEnsure()
{
Ensure.ArgumentNotNull(RangeStart, nameof(RangeStart));
Ensure.ArgumentNotNull(InsertBefore, nameof(InsertBefore));
}
}
}

View File

@ -47,10 +47,31 @@ namespace SpotifyAPI.Web
{
if (value is List<string>)
{
List<string> list = value as List<string>;
var list = value as List<string>;
var str = string.Join(",", list);
queryParams.Add(attribute.Key ?? prop.Name, str);
}
else if (value is Enum)
{
var valueAsEnum = value as Enum;
var enumType = valueAsEnum.GetType();
var valueList = new List<string>();
foreach (Enum enumVal in Enum.GetValues(valueAsEnum.GetType()))
{
if (valueAsEnum.HasFlag(enumVal))
{
if (enumType
.GetMember(enumVal.ToString())[0]
.GetCustomAttributes(typeof(StringAttribute))
.FirstOrDefault() is StringAttribute stringAttr)
{
valueList.Add(stringAttr.Value);
}
}
}
queryParams.Add(attribute.Key ?? prop.Name, string.Join(",", valueList));
}
else
{
queryParams.Add(attribute.Key ?? prop.Name, value.ToString());

View File

@ -0,0 +1,56 @@
using System;
namespace SpotifyAPI.Web
{
public class SearchRequest : RequestParams
{
[QueryParam("type")]
public Type? Types { get; set; }
[QueryParam("q")]
public string Query { get; set; }
[QueryParam("market")]
public string Market { get; set; }
[QueryParam("limit")]
public int? Limit { get; set; }
[QueryParam("offset")]
public int? Offset { get; set; }
[QueryParam("include_external")]
public External? IncludeExternal { get; set; }
protected override void CustomEnsure()
{
Ensure.ArgumentNotNull(Types, nameof(Types));
Ensure.ArgumentNotNullOrEmptyString(Query, nameof(Query));
}
[Flags]
public enum External
{
[String("audio")]
Audio = 0,
}
[Flags]
public enum Type
{
[String("album")]
Album = 0,
[String("artist")]
Artist = 1,
[String("playlist")]
Playlist = 2,
[String("track")]
Track = 4,
[String("show")]
Show = 8,
[String("episode")]
Episode = 16,
All = Album | Artist | Playlist | Track | Show | Episode
}
}
}

View File

@ -0,0 +1,18 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class FullArtist
{
public Dictionary<string, string> ExternalUrls { get; set; }
public Followers Followers { get; set; }
public List<string> Genres { get; set; }
public string Href { get; set; }
public string Id { get; set; }
public List<Image> Images { get; set; }
public string Name { get; set; }
public int Popularity { get; set; }
public string Type { get; set; }
public string Uri { get; set; }
}
}

View File

@ -0,0 +1,11 @@
namespace SpotifyAPI.Web
{
public class SearchResponse
{
public Paging<FullArtist> Artists { get; set; }
public Paging<SimpleAlbum> Albums { get; set; }
public Paging<FullTrack> Tracks { get; set; }
public Paging<SimpleShow> Shows { get; set; }
public Paging<SimpleEpisode> Episodes { get; set; }
}
}

View File

@ -41,6 +41,8 @@ namespace SpotifyAPI.Web
public static Uri CurrentUserPlaylists() => EUri($"me/playlists");
public static Uri Search() => EUri($"search");
private static Uri EUri(FormattableString path) => new Uri(path.ToString(_provider), UriKind.Relative);
}
}

View File

@ -14,15 +14,14 @@ namespace SpotifyAPI.Web
/// </summary>
/// <param name = "value">The argument value to check</param>
/// <param name = "name">The name of the argument</param>
/// <param name = "additional">Additional Exception Text</param>
public static void ArgumentNotNull(object value, string name, string additional = null)
public static void ArgumentNotNull(object value, string name)
{
if (value != null)
{
return;
}
throw new ArgumentNullException($"{name}{additional}");
throw new ArgumentNullException(name);
}
/// <summary>

View File

@ -0,0 +1,14 @@
using System;
namespace SpotifyAPI.Web
{
public class StringAttribute : Attribute
{
public StringAttribute(string value)
{
Value = value;
}
public string Value { get; set; }
}
}