Added additional constructor to WebAPIFactory

This commit is contained in:
Johnny @PC 2016-07-31 16:13:08 +02:00
parent 409a444d53
commit 417869548a
3 changed files with 55 additions and 6 deletions

View File

@ -133,8 +133,7 @@ namespace SpotifyAPI.Example
8000, 8000,
"26d287105e31491889f3cd293d85bfea", "26d287105e31491889f3cd293d85bfea",
Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead | Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead |
Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead, Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead);
TimeSpan.FromSeconds(20));
try try
{ {

View File

@ -0,0 +1,42 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
namespace SpotifyAPI.Web.Auth.Factories
{
public class CredentialWebFactory
{
private readonly string _secretId;
private readonly string _clientId;
private readonly Scope _scope;
public CredentialWebFactory(string secretId, string clientId, Scope scope)
{
_secretId = secretId;
_clientId = clientId;
_scope = scope;
}
public async Task<SpotifyWebAPI> GetWebApi()
{
var authentication = new ClientCredentialsAuth()
{
ClientId = _clientId,
Scope = _scope,
ClientSecret = _secretId
};
AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false);
SpotifyWebAPI spotifyWebApi = null;
Token token = await authentication.DoAuthAsync();
if(token.Error != null)
throw new SpotifyWebApiException(token.Error);
return Task.FromResult(spotifyWebApi);
}
}
}

View File

@ -13,14 +13,22 @@ namespace SpotifyAPI.Web.Auth
private readonly string _clientId; private readonly string _clientId;
private readonly TimeSpan _timeout; private readonly TimeSpan _timeout;
private readonly Scope _scope; private readonly Scope _scope;
private readonly string _xss;
public WebAPIFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, TimeSpan timeout) public WebAPIFactory(string redirectUrl, int listeningPort, string clientId, Scope scope)
: this(redirectUrl, listeningPort, clientId, scope, TimeSpan.FromSeconds(20))
{
}
public WebAPIFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, TimeSpan timeout, string xss = "XSS")
{ {
_redirectUrl = redirectUrl; _redirectUrl = redirectUrl;
_listeningPort = listeningPort; _listeningPort = listeningPort;
_clientId = clientId; _clientId = clientId;
_scope = scope; _scope = scope;
_timeout = timeout; _timeout = timeout;
_xss = xss;
} }
public Task<SpotifyWebAPI> GetWebApi() public Task<SpotifyWebAPI> GetWebApi()
@ -30,7 +38,7 @@ namespace SpotifyAPI.Web.Auth
RedirectUri = $"{_redirectUrl}:{_listeningPort}", RedirectUri = $"{_redirectUrl}:{_listeningPort}",
ClientId = _clientId, ClientId = _clientId,
Scope = _scope, Scope = _scope,
State = "XSS" State = _xss
}; };
AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false); AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false);
@ -59,9 +67,9 @@ namespace SpotifyAPI.Web.Auth
return Task.FromResult(spotifyWebApi); return Task.FromResult(spotifyWebApi);
} }
private static SpotifyWebAPI HandleSpotifyResponse(string state, Token token) private SpotifyWebAPI HandleSpotifyResponse(string state, Token token)
{ {
if (state != "XSS") if (state != _xss)
throw new SpotifyWebApiException($"Wrong state '{state}' received."); throw new SpotifyWebApiException($"Wrong state '{state}' received.");
if (token.Error != null) if (token.Error != null)