Skip to content

Commit 2a9b033

Browse files
committed
Moar docs and TooManyRequests exception
1 parent ef60a9f commit 2a9b033

6 files changed

Lines changed: 104 additions & 6 deletions

File tree

‎SpotifyAPI.Web.Examples/Example.TokenSwap/Client/Program.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Program
1111
private static readonly string clientId = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID");
1212
private static EmbedIOAuthServer _server;
1313

14-
public static async Task Main(string[] args)
14+
public static async Task Main()
1515
{
1616
_server = new EmbedIOAuthServer(new Uri("http://localhost:5000/callback"), 5000);
1717
await _server.Start();

‎SpotifyAPI.Web/Clients/Interfaces/IOAuthClient.cs‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IOAuthClient
1111
/// Requests a new token using client_ids and client_secrets.
1212
/// If the token is expired, simply call the funtion again to get a new token
1313
/// </summary>
14-
/// <param name="request"></param>
14+
/// <param name="request">The request-model which contains required and optional parameters.</param>
1515
/// <remarks>
1616
/// https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow
1717
/// </remarks>
@@ -21,7 +21,7 @@ public interface IOAuthClient
2121
/// <summary>
2222
/// Refresh an already received token via Authorization Code Auth
2323
/// </summary>
24-
/// <param name="request"></param>
24+
/// <param name="request">The request-model which contains required and optional parameters.</param>
2525
/// <remarks>
2626
/// https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow
2727
/// </remarks>
@@ -31,11 +31,31 @@ public interface IOAuthClient
3131
/// <summary>
3232
/// Reequest an initial token via Authorization Code Auth
3333
/// </summary>
34-
/// <param name="request"></param>
34+
/// <param name="request">The request-model which contains required and optional parameters.</param>
3535
/// <remarks>
3636
/// https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow
3737
/// </remarks>
3838
/// <returns></returns>
3939
Task<AuthorizationCodeTokenResponse> RequestToken(AuthorizationCodeTokenRequest request);
40+
41+
/// <summary>
42+
/// Swaps out a received code with a access token using a remote server
43+
/// </summary>
44+
/// <param name="request">The request-model which contains required and optional parameters.</param>
45+
/// <remarks>
46+
/// https://developer.spotify.com/documentation/ios/guides/token-swap-and-refresh/
47+
/// </remarks>
48+
/// <returns></returns>
49+
Task<AuthorizationCodeTokenResponse> RequestToken(TokenSwapTokenRequest request);
50+
51+
/// <summary>
52+
/// Gets a refreshed access token using an already received refresh token using a remote server
53+
/// </summary>
54+
/// <param name="request"></param>
55+
/// <remarks>
56+
/// https://developer.spotify.com/documentation/ios/guides/token-swap-and-refresh/
57+
/// </remarks>
58+
/// <returns></returns>
59+
Task<AuthorizationCodeRefreshResponse> RequestToken(TokenSwapRefreshRequest request);
4060
}
4161
}

‎SpotifyAPI.Web/Clients/OAuthClient.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,67 @@ public OAuthClient(IAPIConnector apiConnector) : base(apiConnector) { }
1515
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062")]
1616
public OAuthClient(SpotifyClientConfig config) : base(ValidateConfig(config)) { }
1717

18+
/// <summary>
19+
/// Requests a new token using client_ids and client_secrets.
20+
/// If the token is expired, simply call the funtion again to get a new token
21+
/// </summary>
22+
/// <param name="request">The request-model which contains required and optional parameters.</param>
23+
/// <remarks>
24+
/// https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow
25+
/// </remarks>
26+
/// <returns></returns>1
1827
public Task<CredentialsTokenResponse> RequestToken(ClientCredentialsRequest request)
1928
{
2029
return RequestToken(request, API);
2130
}
2231

32+
/// <summary>
33+
/// Refresh an already received token via Authorization Code Auth
34+
/// </summary>
35+
/// <param name="request">The request-model which contains required and optional parameters.</param>
36+
/// <remarks>
37+
/// https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow
38+
/// </remarks>
39+
/// <returns></returns>
2340
public Task<AuthorizationCodeRefreshResponse> RequestToken(AuthorizationCodeRefreshRequest request)
2441
{
2542
return RequestToken(request, API);
2643
}
2744

45+
/// <summary>
46+
/// Reequest an initial token via Authorization Code Auth
47+
/// </summary>
48+
/// <param name="request">The request-model which contains required and optional parameters.</param>
49+
/// <remarks>
50+
/// https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow
51+
/// </remarks>
52+
/// <returns></returns>
2853
public Task<AuthorizationCodeTokenResponse> RequestToken(AuthorizationCodeTokenRequest request)
2954
{
3055
return RequestToken(request, API);
3156
}
3257

58+
/// <summary>
59+
/// Swaps out a received code with a access token using a remote server
60+
/// </summary>
61+
/// <param name="request">The request-model which contains required and optional parameters.</param>
62+
/// <remarks>
63+
/// https://developer.spotify.com/documentation/ios/guides/token-swap-and-refresh/
64+
/// </remarks>
65+
/// <returns></returns>
3366
public Task<AuthorizationCodeTokenResponse> RequestToken(TokenSwapTokenRequest request)
3467
{
3568
return RequestToken(request, API);
3669
}
3770

71+
/// <summary>
72+
/// Gets a refreshed access token using an already received refresh token using a remote server
73+
/// </summary>
74+
/// <param name="request"></param>
75+
/// <remarks>
76+
/// https://developer.spotify.com/documentation/ios/guides/token-swap-and-refresh/
77+
/// </remarks>
78+
/// <returns></returns>
3879
public Task<AuthorizationCodeRefreshResponse> RequestToken(TokenSwapRefreshRequest request)
3980
{
4081
return RequestToken(request, API);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Globalization;
2+
using System.Runtime.Serialization;
3+
using System;
4+
using SpotifyAPI.Web.Http;
5+
6+
namespace SpotifyAPI.Web
7+
{
8+
[Serializable]
9+
public class APITooManyRequestsException : APIException
10+
{
11+
public TimeSpan RetryAfter { get; }
12+
13+
public APITooManyRequestsException(IResponse response) : base(response)
14+
{
15+
Ensure.ArgumentNotNull(response, nameof(response));
16+
17+
if (response.Headers.TryGetValue("Retry-After", out string retryAfter))
18+
{
19+
RetryAfter = TimeSpan.FromSeconds(int.Parse(retryAfter, CultureInfo.InvariantCulture));
20+
}
21+
}
22+
23+
public APITooManyRequestsException() { }
24+
25+
public APITooManyRequestsException(string message) : base(message) { }
26+
27+
public APITooManyRequestsException(string message, Exception innerException) : base(message, innerException) { }
28+
29+
protected APITooManyRequestsException(SerializationInfo info, StreamingContext context) : base(info, context) { }
30+
}
31+
}

‎SpotifyAPI.Web/Http/APIConnector.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ private static void ProcessErrors(IResponse response)
283283
{
284284
case HttpStatusCode.Unauthorized:
285285
throw new APIUnauthorizedException(response);
286+
case HttpStatusCode.TooManyRequests:
287+
throw new APITooManyRequestsException(response);
286288
default:
287289
throw new APIException(response);
288290
}

‎SpotifyAPI.Web/Http/Authenticators/AuthorizationCodeAuthenticator.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace SpotifyAPI.Web.Http
99
/// </summary>
1010
public class AuthorizationCodeAuthenticator : IAuthenticator
1111
{
12-
public event EventHandler<AuthorizationCodeTokenResponse>? TokenRefreshed;
13-
1412
/// <summary>
1513
/// Initiate a new instance. The token will be refreshed once it expires.
1614
/// The initialToken will be updated with the new values on refresh!
@@ -26,6 +24,12 @@ public AuthorizationCodeAuthenticator(string clientId, string clientSecret, Auth
2624
ClientSecret = clientSecret;
2725
}
2826

27+
/// <summary>
28+
/// This event is called once a new refreshed token was aquired
29+
/// </summary>
30+
public event EventHandler<AuthorizationCodeTokenResponse>? TokenRefreshed;
31+
32+
2933
/// <summary>
3034
/// The ClientID, defined in a spotify application in your Spotify Developer Dashboard
3135
/// </summary>

0 commit comments

Comments
 (0)