forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebApiFactory.cs
More file actions
107 lines (91 loc) · 3.59 KB
/
WebApiFactory.cs
File metadata and controls
107 lines (91 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Threading;
using System.Threading.Tasks;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
namespace SpotifyAPI.Web.Auth
{
public class WebAPIFactory
{
private readonly string _redirectUrl;
private readonly int _listeningPort;
private readonly string _clientId;
private readonly TimeSpan _timeout;
private readonly Scope _scope;
private readonly ProxyConfig _proxyConfig;
private readonly string _xss;
public WebAPIFactory(string redirectUrl, int listeningPort, string clientId, Scope scope)
: this(redirectUrl, listeningPort, clientId, scope, null)
{
}
public WebAPIFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, ProxyConfig proxyConfig)
: this(redirectUrl, listeningPort, clientId, scope, TimeSpan.FromSeconds(20), proxyConfig)
{
}
public WebAPIFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, TimeSpan timeout, string xss = "XSS")
: this(redirectUrl, listeningPort, clientId, scope, timeout, null, xss)
{
}
public WebAPIFactory(string redirectUrl, int listeningPort, string clientId, Scope scope, TimeSpan timeout, ProxyConfig proxyConfig, string xss = "XSS")
{
_redirectUrl = redirectUrl;
_listeningPort = listeningPort;
_clientId = clientId;
_scope = scope;
_timeout = timeout;
_proxyConfig = proxyConfig;
_xss = xss;
}
public Task<SpotifyWebAPI> GetWebApi(bool showDialog = false)
{
var authentication = new ImplicitGrantAuth
{
RedirectUri = new UriBuilder(_redirectUrl) { Port = _listeningPort }.Uri.OriginalString.TrimEnd('/'),
ClientId = _clientId,
Scope = _scope,
ShowDialog = showDialog,
State = _xss
};
AutoResetEvent authenticationWaitFlag = new AutoResetEvent(false);
SpotifyWebAPI spotifyWebApi = null;
authentication.OnResponseReceivedEvent += (token, state) =>
{
spotifyWebApi = HandleSpotifyResponse(state, token);
authenticationWaitFlag.Set();
};
try
{
authentication.StartHttpServer(_listeningPort);
authentication.DoAuth();
authenticationWaitFlag.WaitOne(_timeout);
if (spotifyWebApi == null)
throw new TimeoutException($"No valid response received for the last {_timeout.TotalSeconds} seconds");
}
finally
{
authentication.StopHttpServer();
}
return Task.FromResult(spotifyWebApi);
}
private SpotifyWebAPI HandleSpotifyResponse(string state, Token token)
{
if (state != _xss)
throw new SpotifyWebApiException($"Wrong state '{state}' received.");
if (token.Error != null)
throw new SpotifyWebApiException($"Error: {token.Error}");
var spotifyWebApi = new SpotifyWebAPI(_proxyConfig)
{
UseAuth = true,
AccessToken = token.AccessToken,
TokenType = token.TokenType
};
return spotifyWebApi;
}
}
[Serializable]
public class SpotifyWebApiException : Exception
{
public SpotifyWebApiException(string message) : base(message)
{ }
}
}