Skip to content

Commit 4178695

Browse files
author
Johnny @PC
committed
Added additional constructor to WebAPIFactory
1 parent 409a444 commit 4178695

3 files changed

Lines changed: 55 additions & 6 deletions

File tree

‎SpotifyAPI.Example/WebControl.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ private async void RunAuthentication()
133133
8000,
134134
"26d287105e31491889f3cd293d85bfea",
135135
Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead |
136-
Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead,
137-
TimeSpan.FromSeconds(20));
136+
Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead);
138137

139138
try
140139
{
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using SpotifyAPI.Web.Enums;
5+
using SpotifyAPI.Web.Models;
6+
7+
namespace SpotifyAPI.Web.Auth.Factories
8+
{
9+
public class CredentialWebFactory
10+
{
11+
private readonly string _secretId;
12+
private readonly string _clientId;
13+
private readonly Scope _scope;
14+
15+
public CredentialWebFactory(string secretId, string clientId, Scope scope)
16+
{
17+
_secretId = secretId;
18+
_clientId = clientId;
19+
_scope = scope;
20+
}
21+
22+
public async Task<SpotifyWebAPI> GetWebApi()
23+
{
24+
var authentication = new ClientCredentialsAuth()
25+
{
26+
ClientId = _clientId,
27+
Scope = _scope,
28+
ClientSecret = _secretId
29+
};
30+
31+
AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false);
32+
SpotifyWebAPI spotifyWebApi = null;
33+
34+
Token token = await authentication.DoAuthAsync();
35+
36+
if(token.Error != null)
37+
throw new SpotifyWebApiException(token.Error);
38+
39+
return Task.FromResult(spotifyWebApi);
40+
}
41+
}
42+
}

‎SpotifyAPI/Web/Auth/WebApiFactory.cs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,22 @@ public class WebAPIFactory
1313
private readonly string _clientId;
1414
private readonly TimeSpan _timeout;
1515
private readonly Scope _scope;
16+
private readonly string _xss;
1617

17-
public WebAPIFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, TimeSpan timeout)
18+
public WebAPIFactory(string redirectUrl, int listeningPort, string clientId, Scope scope)
19+
: this(redirectUrl, listeningPort, clientId, scope, TimeSpan.FromSeconds(20))
20+
{
21+
22+
}
23+
24+
public WebAPIFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, TimeSpan timeout, string xss = "XSS")
1825
{
1926
_redirectUrl = redirectUrl;
2027
_listeningPort = listeningPort;
2128
_clientId = clientId;
2229
_scope = scope;
2330
_timeout = timeout;
31+
_xss = xss;
2432
}
2533

2634
public Task<SpotifyWebAPI> GetWebApi()
@@ -30,7 +38,7 @@ public Task<SpotifyWebAPI> GetWebApi()
3038
RedirectUri = $"{_redirectUrl}:{_listeningPort}",
3139
ClientId = _clientId,
3240
Scope = _scope,
33-
State = "XSS"
41+
State = _xss
3442
};
3543

3644
AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false);
@@ -59,9 +67,9 @@ public Task<SpotifyWebAPI> GetWebApi()
5967
return Task.FromResult(spotifyWebApi);
6068
}
6169

62-
private static SpotifyWebAPI HandleSpotifyResponse(string state, Token token)
70+
private SpotifyWebAPI HandleSpotifyResponse(string state, Token token)
6371
{
64-
if (state != "XSS")
72+
if (state != _xss)
6573
throw new SpotifyWebApiException($"Wrong state '{state}' received.");
6674

6775
if (token.Error != null)

0 commit comments

Comments
 (0)