Added Follow Endpoint

This commit is contained in:
Jonas Dellinger 2020-05-04 23:02:53 +02:00
parent 7c94de2110
commit 9a7d46fb5a
16 changed files with 296 additions and 0 deletions

View File

@ -0,0 +1,83 @@
using System.Net;
using System.Collections.Generic;
using System.Threading.Tasks;
using SpotifyAPI.Web.Http;
using URLs = SpotifyAPI.Web.SpotifyUrls;
namespace SpotifyAPI.Web
{
public class FollowClient : APIClient, IFollowClient
{
public FollowClient(IAPIConnector apiConnector) : base(apiConnector) { }
public Task<List<bool>> CheckCurrentUser(FollowCheckCurrentUserRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<List<bool>>(URLs.CurrentUserFollowerContains(), request.BuildQueryParams());
}
public Task<List<bool>> CheckPlaylist(string playlistId, FollowCheckPlaylistRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<List<bool>>(URLs.PlaylistFollowersContains(playlistId), request.BuildQueryParams());
}
public async Task<bool> Follow(FollowRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
var statusCode = await API.Put(URLs.CurrentUserFollower(), request.BuildQueryParams(), request.BuildBodyParams());
return statusCode == HttpStatusCode.OK;
}
public async Task<bool> FollowPlaylist(string playlistId)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
var statusCode = await API.Put(URLs.PlaylistFollowers(playlistId), null, null);
return statusCode == HttpStatusCode.OK;
}
public async Task<bool> FollowPlaylist(string playlistId, FollowPlaylistRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
Ensure.ArgumentNotNull(request, nameof(request));
var statusCode = await API.Put(URLs.PlaylistFollowers(playlistId), null, request.BuildBodyParams());
return statusCode == HttpStatusCode.OK;
}
public Task<FollowedArtistsResponse> OfCurrentUser()
{
var request = new FollowOfCurrentUserRequest();
return API.Get<FollowedArtistsResponse>(URLs.CurrentUserFollower(), request.BuildQueryParams());
}
public Task<FollowedArtistsResponse> OfCurrentUser(FollowOfCurrentUserRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
return API.Get<FollowedArtistsResponse>(URLs.CurrentUserFollower());
}
public async Task<bool> Unfollow(UnfollowRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
var statusCode = await API.Delete(URLs.CurrentUserFollower(), request.BuildQueryParams(), request.BuildBodyParams());
return statusCode == HttpStatusCode.NoContent;
}
public async Task<bool> UnfollowPlaylist(string playlistId)
{
Ensure.ArgumentNotNullOrEmptyString(playlistId, nameof(playlistId));
var statusCode = await API.Delete(URLs.PlaylistFollowers(playlistId), null, null);
return statusCode == HttpStatusCode.OK;
}
}
}

View File

@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SpotifyAPI.Web
{
public interface IFollowClient
{
Task<List<bool>> CheckCurrentUser(FollowCheckCurrentUserRequest request);
Task<List<bool>> CheckPlaylist(string playlistId, FollowCheckPlaylistRequest request);
Task<bool> Follow(FollowRequest request);
Task<bool> FollowPlaylist(string playlistId);
Task<bool> FollowPlaylist(string playlistId, FollowPlaylistRequest request);
Task<FollowedArtistsResponse> OfCurrentUser();
Task<FollowedArtistsResponse> OfCurrentUser(FollowOfCurrentUserRequest request);
Task<bool> Unfollow(UnfollowRequest request);
Task<bool> UnfollowPlaylist(string playlistId);
}
}

View File

@ -11,5 +11,7 @@ namespace SpotifyAPI.Web
IPlaylistsClient Playlists { get; } IPlaylistsClient Playlists { get; }
ISearchClient Search { get; } ISearchClient Search { get; }
IFollowClient Follow { get; }
} }
} }

View File

@ -20,6 +20,7 @@ namespace SpotifyAPI.Web
Shows = new ShowsClient(_apiConnector); Shows = new ShowsClient(_apiConnector);
Playlists = new PlaylistsClient(_apiConnector); Playlists = new PlaylistsClient(_apiConnector);
Search = new SearchClient(_apiConnector); Search = new SearchClient(_apiConnector);
Follow = new FollowClient(_apiConnector);
} }
public IUserProfileClient UserProfile { get; } public IUserProfileClient UserProfile { get; }
@ -31,5 +32,7 @@ namespace SpotifyAPI.Web
public IPlaylistsClient Playlists { get; } public IPlaylistsClient Playlists { get; }
public ISearchClient Search { get; } public ISearchClient Search { get; }
public IFollowClient Follow { get; }
} }
} }

View File

@ -55,6 +55,14 @@ namespace SpotifyAPI.Web.Http
return SendAPIRequest<T>(uri, HttpMethod.Delete, parameters, body); return SendAPIRequest<T>(uri, HttpMethod.Delete, parameters, body);
} }
public async Task<HttpStatusCode> Delete(Uri uri, IDictionary<string, string> parameters, object body)
{
Ensure.ArgumentNotNull(uri, nameof(uri));
var response = await SendAPIRequestDetailed(uri, HttpMethod.Delete, parameters, body);
return response.StatusCode;
}
public Task<T> Get<T>(Uri uri) public Task<T> Get<T>(Uri uri)
{ {
Ensure.ArgumentNotNull(uri, nameof(uri)); Ensure.ArgumentNotNull(uri, nameof(uri));
@ -90,6 +98,14 @@ namespace SpotifyAPI.Web.Http
return SendAPIRequest<T>(uri, HttpMethod.Post, parameters, body); return SendAPIRequest<T>(uri, HttpMethod.Post, parameters, body);
} }
public async Task<HttpStatusCode> Post(Uri uri, IDictionary<string, string> parameters, object body)
{
Ensure.ArgumentNotNull(uri, nameof(uri));
var response = await SendAPIRequestDetailed(uri, HttpMethod.Post, parameters, body);
return response.StatusCode;
}
public Task<T> Put<T>(Uri uri) public Task<T> Put<T>(Uri uri)
{ {
Ensure.ArgumentNotNull(uri, nameof(uri)); Ensure.ArgumentNotNull(uri, nameof(uri));

View File

@ -20,6 +20,7 @@ namespace SpotifyAPI.Web.Http
Task<T> Post<T>(Uri uri); Task<T> Post<T>(Uri uri);
Task<T> Post<T>(Uri uri, IDictionary<string, string> parameters); Task<T> Post<T>(Uri uri, IDictionary<string, string> parameters);
Task<T> Post<T>(Uri uri, IDictionary<string, string> parameters, object body); Task<T> Post<T>(Uri uri, IDictionary<string, string> parameters, object body);
Task<HttpStatusCode> Post(Uri uri, IDictionary<string, string> parameters, object body);
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);
@ -30,6 +31,7 @@ namespace SpotifyAPI.Web.Http
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);
Task<T> Delete<T>(Uri uri, IDictionary<string, string> parameters, object body); Task<T> Delete<T>(Uri uri, IDictionary<string, string> parameters, object body);
Task<HttpStatusCode> Delete(Uri uri, IDictionary<string, string> parameters, object body);
Task<T> SendAPIRequest<T>(Uri uri, HttpMethod method, IDictionary<string, string> parameters = null, object body = null); Task<T> SendAPIRequest<T>(Uri uri, HttpMethod method, IDictionary<string, string> parameters = null, object body = null);

View File

@ -0,0 +1,27 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class FollowCheckCurrentUserRequest : RequestParams
{
[QueryParam("type")]
public Types? Type { get; set; }
[QueryParam("ids")]
public List<string> Ids { get; set; }
protected override void CustomEnsure()
{
Ensure.ArgumentNotNull(Type, nameof(Type));
Ensure.ArgumentNotNullOrEmptyList(Ids, nameof(Ids));
}
public enum Types
{
[String("artist")]
Artist,
[String("user")]
User
}
}
}

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class FollowCheckPlaylistRequest : RequestParams
{
[QueryParam("ids")]
public List<string> Ids { get; set; }
protected override void CustomEnsure()
{
Ensure.ArgumentNotNullOrEmptyList(Ids, nameof(Ids));
}
}
}

View File

@ -0,0 +1,26 @@
namespace SpotifyAPI.Web
{
public class FollowOfCurrentUserRequest : RequestParams
{
public FollowOfCurrentUserRequest()
{
Type = Types.Artist;
}
[QueryParam("type")]
public Types Type { get; set; }
[QueryParam("limit")]
public int? Limit { get; set; }
[QueryParam("after")]
public string After { get; set; }
public enum Types
{
[String("artist")]
Artist
}
}
}

View File

@ -0,0 +1,8 @@
namespace SpotifyAPI.Web
{
public class FollowPlaylistRequest : RequestParams
{
[BodyParam("public")]
public bool Public { get; set; }
}
}

View File

@ -0,0 +1,27 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class FollowRequest : RequestParams
{
[QueryParam("type")]
public Types? Type { get; set; }
[BodyParam("ids")]
public List<string> Ids { get; set; }
protected override void CustomEnsure()
{
Ensure.ArgumentNotNull(Type, nameof(Type));
Ensure.ArgumentNotNullOrEmptyList(Ids, nameof(Ids));
}
public enum Types
{
[String("artist")]
Artist,
[String("user")]
User
}
}
}

View File

@ -0,0 +1,26 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class UnfollowRequest : RequestParams
{
[QueryParam("type")]
public Types? Type { get; set; }
[BodyParam("ids")]
public List<string> Ids { get; set; }
protected override void CustomEnsure()
{
Ensure.ArgumentNotNull(Type, nameof(Type));
Ensure.ArgumentNotNullOrEmptyList(Ids, nameof(Ids));
}
public enum Types
{
[String("artist")]
Artist,
[String("user")]
User
}
}
}

View File

@ -0,0 +1,8 @@
namespace SpotifyAPI.Web
{
public class Cursor
{
public string Before { get; set; }
public string After { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace SpotifyAPI.Web
{
public class CursorPaging<T>
{
public string Href { get; set; }
public List<T> Items { get; set; }
public int Limit { get; set; }
public string Next { get; set; }
public Cursor Cursors { get; set; }
public int Total { get; set; }
}
}

View File

@ -0,0 +1,7 @@
namespace SpotifyAPI.Web
{
public class FollowedArtistsResponse
{
public CursorPaging<FullArtist> Artists { get; set; }
}
}

View File

@ -43,6 +43,14 @@ namespace SpotifyAPI.Web
public static Uri Search() => EUri($"search"); public static Uri Search() => EUri($"search");
public static Uri CurrentUserFollowerContains() => EUri($"me/following/contains");
public static Uri PlaylistFollowersContains(string playlistId) => EUri($"playlists/{playlistId}/followers/contains");
public static Uri CurrentUserFollower() => EUri($"me/following");
public static Uri PlaylistFollowers(string playlistId) => EUri($"playlists/{playlistId}/followers");
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);
} }
} }