22{
33 using System ;
44 using System . Collections . Generic ;
5+ using System . Diagnostics . Eventing . Reader ;
56 using System . Linq ;
67 using System . Threading ;
78 using System . Threading . Tasks ;
@@ -259,7 +260,7 @@ private async Task AddDeviceToMerchant(Guid estateId,
259260 String deviceIdentifier ,
260261 CancellationToken cancellationToken )
261262 {
262- await this . GetToken ( cancellationToken ) ;
263+ this . TokenResponse = await this . GetToken ( cancellationToken ) ;
263264
264265 // Add the device to the merchant
265266 await this . EstateClient . AddDeviceToMerchant ( this . TokenResponse . AccessToken ,
@@ -290,7 +291,7 @@ private String GenerateTransactionReference()
290291 private async Task < EstateResponse > GetEstate ( Guid estateId ,
291292 CancellationToken cancellationToken )
292293 {
293- await this . GetToken ( cancellationToken ) ;
294+ this . TokenResponse = await this . GetToken ( cancellationToken ) ;
294295
295296 EstateResponse estate = await this . EstateClient . GetEstate ( this . TokenResponse . AccessToken , estateId , cancellationToken ) ;
296297
@@ -301,28 +302,42 @@ private async Task<MerchantResponse> GetMerchant(Guid estateId,
301302 Guid merchantId ,
302303 CancellationToken cancellationToken )
303304 {
304- await this . GetToken ( cancellationToken ) ;
305+ this . TokenResponse = await this . GetToken ( cancellationToken ) ;
305306
306307 MerchantResponse merchant = await this . EstateClient . GetMerchant ( this . TokenResponse . AccessToken , estateId , merchantId , cancellationToken ) ;
307308
308309 return merchant ;
309310 }
310311
311- private async Task GetToken ( CancellationToken cancellationToken )
312+ /// <summary>
313+ /// Gets the token.
314+ /// </summary>
315+ /// <param name="cancellationToken">The cancellation token.</param>
316+ /// <returns></returns>
317+ private async Task < TokenResponse > GetToken ( CancellationToken cancellationToken )
312318 {
319+ // Get a token to talk to the estate service
320+ String clientId = ConfigurationReader . GetValue ( "AppSettings" , "ClientId" ) ;
321+ String clientSecret = ConfigurationReader . GetValue ( "AppSettings" , "ClientSecret" ) ;
322+ Logger . LogInformation ( $ "Client Id is { clientId } ") ;
323+ Logger . LogInformation ( $ "Client Secret is { clientSecret } ") ;
324+
313325 if ( this . TokenResponse == null )
314326 {
315- // Get a token to talk to the estate service
316- String clientId = ConfigurationReader . GetValue ( "AppSettings" , "ClientId" ) ;
317- String clientSecret = ConfigurationReader . GetValue ( "AppSettings" , "ClientSecret" ) ;
318-
319- Logger . LogInformation ( $ "Client Id is { clientId } ") ;
320- Logger . LogInformation ( $ "Client Secret is { clientSecret } ") ;
327+ TokenResponse token = await this . SecurityServiceClient . GetToken ( clientId , clientSecret , cancellationToken ) ;
328+ Logger . LogInformation ( $ "Token is { token . AccessToken } ") ;
329+ return token ;
330+ }
321331
332+ if ( this . TokenResponse . Expires . UtcDateTime . Subtract ( DateTime . UtcNow ) < TimeSpan . FromMinutes ( 2 ) )
333+ {
334+ Logger . LogInformation ( $ "Token is about to expire at { this . TokenResponse . Expires . DateTime : O} ") ;
322335 TokenResponse token = await this . SecurityServiceClient . GetToken ( clientId , clientSecret , cancellationToken ) ;
323336 Logger . LogInformation ( $ "Token is { token . AccessToken } ") ;
324- this . TokenResponse = token ;
337+ return token ;
325338 }
339+
340+ return this . TokenResponse ;
326341 }
327342
328343 /// <summary>
0 commit comments