public static async Task <IGenesysAccessToken> GetAuthTokenAsync(this CloudTable cloudTable, HttpClient apiClient
                                                                         , DateTime date
                                                                         , IGenesysClientCredentials clientCredentials
                                                                         , bool reload = false)
        {
            if (!reload)
            {
                try
                {
                    var existedToken = await cloudTable.GetAuthTokenAsync(clientCredentials);

                    if (existedToken?.ExpiresIn > date)
                    {
                        existedToken.Environment = clientCredentials.Environment;
                        return(existedToken);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception($"Cant load AccessToken from the Azure CloudTable '{cloudTable.Name}'.", ex);
                }
            }
            GenesysAuthTokenInfo authTokenInfo = null;

            authTokenInfo = await apiClient.GetTokenAsync(clientCredentials);

            if (authTokenInfo == null)
            {
                throw new Exception($"Genesys API Token is null. ClientId '{clientCredentials.ClientId}'.");
            }
            else if (!string.IsNullOrEmpty(authTokenInfo.Error))
            {
                throw new Exception($"Genesys API Token error. ClientId '{clientCredentials.ClientId}'. {authTokenInfo.Error}.");
            }

            var newToken = new GenesysAccessToken(clientCredentials)
            {
                Value     = authTokenInfo.AccessToken,
                ExpiresIn = date.AddSeconds(authTokenInfo.ExpiresIn ?? 1000),
                ETag      = "*"
            };

            await cloudTable.SetAuthTokenAsync(newToken);

            newToken.Environment = clientCredentials.Environment;
            return(newToken);
        }
示例#2
0
 public static Task SetAuthTokenAsync(this CloudTable cloudTable, GenesysAccessToken accessToken)
 {
     return(cloudTable.ExecuteAsync(TableOperation.InsertOrReplace(accessToken)));
 }