@@ -29,25 +29,15 @@ public class TwitchApiProxy : ITwitchApiProxy {
29
29
/// </summary>
30
30
private static readonly ILog Log = LogManager . GetLogger ( typeof ( TwitchApiProxy ) ) ;
31
31
32
- /// <summary>
33
- /// The, public, twitch client id.
34
- /// </summary>
35
- protected readonly string ? _clientId ;
36
-
37
- /// <summary>
38
- /// The, private, twitch client secret.
39
- /// </summary>
40
- protected readonly string ? _clientSecret ;
41
-
42
- /// <summary>
43
- /// The redirect url.
44
- /// </summary>
45
- protected readonly string ? _clientRedirect ;
46
-
47
32
/// <summary>
48
33
/// Initializes a new instance of the <see cref="TwitchApiProxy" /> class.
49
34
/// </summary>
50
35
public TwitchApiProxy ( ) {
36
+ TwitchAppConfig = new ( ) {
37
+ ClientId = Environment . GetEnvironmentVariable ( "TWITCH_BOT_CLIENT_ID" ) ,
38
+ ClientSecret = Environment . GetEnvironmentVariable ( "TWITCH_BOT_CLIENT_SECRET" ) ,
39
+ ClientRedirect = Environment . GetEnvironmentVariable ( "TWITCH_BOT_CLIENT_REDIRECT" )
40
+ } ;
51
41
}
52
42
53
43
/// <summary>
@@ -64,15 +54,17 @@ public TwitchApiProxy() {
64
54
/// "TWITCH_BOT_CLIENT_REDIRECT" when null.</param>
65
55
public TwitchApiProxy ( string token , string refreshToken , DateTime tokenExpires , string ? clientId = null ,
66
56
string ? clientSecret = null , string ? clientRedirect = null ) {
67
- _clientId = clientId ?? Environment . GetEnvironmentVariable ( "TWITCH_BOT_CLIENT_ID" ) ;
68
- _clientSecret = clientSecret ?? Environment . GetEnvironmentVariable ( "TWITCH_BOT_CLIENT_SECRET" ) ;
69
- _clientRedirect = clientRedirect ?? Environment . GetEnvironmentVariable ( "TWITCH_BOT_CLIENT_REDIRECT" ) ;
70
-
71
57
OAuth = new TwitchAccessToken {
72
58
AccessToken = token ,
73
59
RefreshToken = refreshToken ,
74
60
ExpiresUtc = tokenExpires
75
61
} ;
62
+
63
+ TwitchAppConfig = new ( ) {
64
+ ClientId = clientId ?? Environment . GetEnvironmentVariable ( "TWITCH_BOT_CLIENT_ID" ) ,
65
+ ClientSecret = clientSecret ?? Environment . GetEnvironmentVariable ( "TWITCH_BOT_CLIENT_SECRET" ) ,
66
+ ClientRedirect = clientRedirect ?? Environment . GetEnvironmentVariable ( "TWITCH_BOT_CLIENT_REDIRECT" )
67
+ } ;
76
68
}
77
69
78
70
/// <summary>
@@ -81,12 +73,16 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires,
81
73
public int Retries { get ; set ; } = 3 ;
82
74
83
75
/// <inheritdoc />
84
- public TwitchAccessToken ? OAuth { get ; set ; }
76
+ public virtual TwitchAccessToken ? OAuth { get ; set ; }
77
+
78
+ /// <inheritdoc />
79
+ public virtual TwitchAppConfig ? TwitchAppConfig { get ; set ; }
85
80
86
81
/// <inheritdoc />
87
82
public virtual async Task < TwitchAccessToken ? > CreateAccessToken ( string code , CancellationToken token = new ( ) ) {
88
83
ITwitchAPI api = GetApi ( ) ;
89
- AuthCodeResponse ? response = await api . Auth . GetAccessTokenFromCodeAsync ( code , _clientSecret , _clientRedirect ) ;
84
+ AuthCodeResponse ? response = await api . Auth . GetAccessTokenFromCodeAsync ( code , TwitchAppConfig ? . ClientSecret ,
85
+ TwitchAppConfig ? . ClientRedirect ) ;
90
86
if ( null == response ) {
91
87
return null ;
92
88
}
@@ -102,12 +98,12 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires,
102
98
/// <inheritdoc />
103
99
public virtual async Task < TwitchAccessToken ? > RefreshAccessToken ( CancellationToken token = new ( ) ) {
104
100
try {
105
- if ( string . IsNullOrWhiteSpace ( _clientSecret ) || string . IsNullOrWhiteSpace ( _clientId ) ) {
101
+ if ( string . IsNullOrWhiteSpace ( TwitchAppConfig ? . ClientSecret ) || string . IsNullOrWhiteSpace ( TwitchAppConfig ? . ClientId ) ) {
106
102
return null ;
107
103
}
108
104
109
105
ITwitchAPI api = GetApi ( ) ;
110
- RefreshResponse ? response = await api . Auth . RefreshAuthTokenAsync ( OAuth ? . RefreshToken , _clientSecret , _clientId ) ;
106
+ RefreshResponse ? response = await api . Auth . RefreshAuthTokenAsync ( OAuth ? . RefreshToken , TwitchAppConfig ? . ClientSecret , TwitchAppConfig ? . ClientId ) ;
111
107
if ( null == response ) {
112
108
return null ;
113
109
}
@@ -175,7 +171,7 @@ public virtual async Task<IEnumerable<TwitchModeratedChannel>> GetUserModChanne
175
171
176
172
var request = new HttpRequestMessage ( HttpMethod . Get , url ) ;
177
173
request . Headers . Add ( "Authorization" , $ "Bearer { OAuth ? . AccessToken } ") ;
178
- request . Headers . Add ( "Client-Id" , _clientId ) ;
174
+ request . Headers . Add ( "Client-Id" , TwitchAppConfig ? . ClientId ) ;
179
175
180
176
using HttpResponseMessage response = await client . SendAsync ( request ) ;
181
177
response . EnsureSuccessStatusCode ( ) ;
@@ -317,7 +313,7 @@ public virtual async Task<IEnumerable<string>> GetChannelsLive(IEnumerable<stri
317
313
protected virtual ITwitchAPI GetApi ( ) {
318
314
var api = new TwitchAPI {
319
315
Settings = {
320
- ClientId = _clientId ,
316
+ ClientId = TwitchAppConfig ? . ClientId ,
321
317
AccessToken = OAuth ? . AccessToken
322
318
}
323
319
} ;
0 commit comments