@@ -29,25 +29,15 @@ public class TwitchApiProxy : ITwitchApiProxy {
2929 /// </summary>
3030 private static readonly ILog Log = LogManager . GetLogger ( typeof ( TwitchApiProxy ) ) ;
3131
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-
4732 /// <summary>
4833 /// Initializes a new instance of the <see cref="TwitchApiProxy" /> class.
4934 /// </summary>
5035 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+ } ;
5141 }
5242
5343 /// <summary>
@@ -64,15 +54,17 @@ public TwitchApiProxy() {
6454 /// "TWITCH_BOT_CLIENT_REDIRECT" when null.</param>
6555 public TwitchApiProxy ( string token , string refreshToken , DateTime tokenExpires , string ? clientId = null ,
6656 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-
7157 OAuth = new TwitchAccessToken {
7258 AccessToken = token ,
7359 RefreshToken = refreshToken ,
7460 ExpiresUtc = tokenExpires
7561 } ;
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+ } ;
7668 }
7769
7870 /// <summary>
@@ -81,12 +73,16 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires,
8173 public int Retries { get ; set ; } = 3 ;
8274
8375 /// <inheritdoc />
84- public TwitchAccessToken ? OAuth { get ; set ; }
76+ public virtual TwitchAccessToken ? OAuth { get ; set ; }
77+
78+ /// <inheritdoc />
79+ public virtual TwitchAppConfig ? TwitchAppConfig { get ; set ; }
8580
8681 /// <inheritdoc />
8782 public virtual async Task < TwitchAccessToken ? > CreateAccessToken ( string code , CancellationToken token = new ( ) ) {
8883 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 ) ;
9086 if ( null == response ) {
9187 return null ;
9288 }
@@ -102,12 +98,12 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires,
10298 /// <inheritdoc />
10399 public virtual async Task < TwitchAccessToken ? > RefreshAccessToken ( CancellationToken token = new ( ) ) {
104100 try {
105- if ( string . IsNullOrWhiteSpace ( _clientSecret ) || string . IsNullOrWhiteSpace ( _clientId ) ) {
101+ if ( string . IsNullOrWhiteSpace ( TwitchAppConfig ? . ClientSecret ) || string . IsNullOrWhiteSpace ( TwitchAppConfig ? . ClientId ) ) {
106102 return null ;
107103 }
108104
109105 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 ) ;
111107 if ( null == response ) {
112108 return null ;
113109 }
@@ -175,7 +171,7 @@ public virtual async Task<IEnumerable<TwitchModeratedChannel>> GetUserModChanne
175171
176172 var request = new HttpRequestMessage ( HttpMethod . Get , url ) ;
177173 request . Headers . Add ( "Authorization" , $ "Bearer { OAuth ? . AccessToken } ") ;
178- request . Headers . Add ( "Client-Id" , _clientId ) ;
174+ request . Headers . Add ( "Client-Id" , TwitchAppConfig ? . ClientId ) ;
179175
180176 using HttpResponseMessage response = await client . SendAsync ( request ) ;
181177 response . EnsureSuccessStatusCode ( ) ;
@@ -317,7 +313,7 @@ public virtual async Task<IEnumerable<string>> GetChannelsLive(IEnumerable<stri
317313 protected virtual ITwitchAPI GetApi ( ) {
318314 var api = new TwitchAPI {
319315 Settings = {
320- ClientId = _clientId ,
316+ ClientId = TwitchAppConfig ? . ClientId ,
321317 AccessToken = OAuth ? . AccessToken
322318 }
323319 } ;
0 commit comments