mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-24 06:56:27 +00:00
Added Follow Endpoint
This commit is contained in:
parent
7c94de2110
commit
9a7d46fb5a
83
SpotifyAPI.Web/Clients/FollowClient.cs
Normal file
83
SpotifyAPI.Web/Clients/FollowClient.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
SpotifyAPI.Web/Clients/Interfaces/IFollowClient.cs
Normal file
24
SpotifyAPI.Web/Clients/Interfaces/IFollowClient.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -11,5 +11,7 @@ namespace SpotifyAPI.Web
|
|||||||
IPlaylistsClient Playlists { get; }
|
IPlaylistsClient Playlists { get; }
|
||||||
|
|
||||||
ISearchClient Search { get; }
|
ISearchClient Search { get; }
|
||||||
|
|
||||||
|
IFollowClient Follow { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
SpotifyAPI.Web/Models/Request/FollowCheckPlaylistRequest.cs
Normal file
15
SpotifyAPI.Web/Models/Request/FollowCheckPlaylistRequest.cs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
SpotifyAPI.Web/Models/Request/FollowGetCurrentUserRequest.cs
Normal file
26
SpotifyAPI.Web/Models/Request/FollowGetCurrentUserRequest.cs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
SpotifyAPI.Web/Models/Request/FollowPlaylistRequest.cs
Normal file
8
SpotifyAPI.Web/Models/Request/FollowPlaylistRequest.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace SpotifyAPI.Web
|
||||||
|
{
|
||||||
|
public class FollowPlaylistRequest : RequestParams
|
||||||
|
{
|
||||||
|
[BodyParam("public")]
|
||||||
|
public bool Public { get; set; }
|
||||||
|
}
|
||||||
|
}
|
27
SpotifyAPI.Web/Models/Request/FollowRequest.cs
Normal file
27
SpotifyAPI.Web/Models/Request/FollowRequest.cs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
SpotifyAPI.Web/Models/Request/UnfollowRequest.cs
Normal file
26
SpotifyAPI.Web/Models/Request/UnfollowRequest.cs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
SpotifyAPI.Web/Models/Response/Cursor.cs
Normal file
8
SpotifyAPI.Web/Models/Response/Cursor.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace SpotifyAPI.Web
|
||||||
|
{
|
||||||
|
public class Cursor
|
||||||
|
{
|
||||||
|
public string Before { get; set; }
|
||||||
|
public string After { get; set; }
|
||||||
|
}
|
||||||
|
}
|
14
SpotifyAPI.Web/Models/Response/CursorPaging.cs
Normal file
14
SpotifyAPI.Web/Models/Response/CursorPaging.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
namespace SpotifyAPI.Web
|
||||||
|
{
|
||||||
|
public class FollowedArtistsResponse
|
||||||
|
{
|
||||||
|
public CursorPaging<FullArtist> Artists { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user