コード例 #1
0
        public async Task <IMessage <T> > ReceiveAsAsync()
        {
            while (true)
            {
                return(await _executionPolicy.ExecuteAsync(async() =>
                {
                    try
                    {
                        if (!_clientOpen)
                        {
                            OpenClient();
                        }

                        BrokeredMessage brokeredMessage;
                        IMessage <T> message;

                        do
                        {
                            brokeredMessage = await _client.ReceiveAsync();
                            message = await DeserialiseAzureBrokeredMessage(brokeredMessage);
                        } while (brokeredMessage != null && message == null);

                        return message;
                    }
                    catch (SessionLockLostException)
                    {
                        RefreshClient();
                        throw;
                    }
                }));
            }
        }
        public async Task <CompanyInformation> GetInformation(string id)
        {
            return(await _executionPolicy.ExecuteAsync(async() =>
            {
                _logger.Info($"GetInformation({id})");

                id = id?.ToUpper();

                var result = await _httpClientWrapper.Get <CompanyInformation>(
                    $"{Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(_configuration.CompaniesHouse.ApiKey))}",
                    $"{_configuration.CompaniesHouse.BaseUrl}/company/{id}");
                return result;
            }));
        }
        private async Task <OAuthAccessToken> GetTokenFromServiceUsingRefreshTokenAsync(OAuthAccessToken token)
        {
            try
            {
                _logger.Debug($"Refreshing token (expired {token.ExpiresAt})");
                var oneTimePassword = await GetOneTimePassword();

                var newToken = await _executionPolicy.ExecuteAsync(async() => await _tokenService.GetAccessTokenFromRefreshToken(oneTimePassword, token.RefreshToken));

                _logger.Debug($"Refresh token successful (new expiry {newToken?.ExpiresAt.ToString("yy-MMM-dd ddd HH:mm:ss") ?? "not available - new token is null"})");
                return(newToken);
            }
            catch (Exception ex)
            {
                _logger.Warn(ex, $"Error trying to refresh access token - {ex.Message}");
                return(null);
            }
        }
コード例 #4
0
        public Task SendAsync(SmsMessage message)
        {
            var notifyMessage = new NotifyMessage
            {
                To              = message.RecipientsNumber,
                Template        = message.TemplateId,
                Personalisation = (message.Tokens ?? new Dictionary <string, string>()).ToDictionary(item => item.Key.ToLower(), item => item.Value)
            };

            return(_executionPolicy.ExecuteAsync(() => _httpClientWrapper.SendSms(notifyMessage)));
        }
コード例 #5
0
        public Task SendAsync(EmailMessage message)
        {
            Logger.Info($"Sending email to {message.RecipientsAddress}");

            var notifyMessage = new NotifyMessage
            {
                To              = message.RecipientsAddress,
                Template        = message.TemplateId,
                Personalisation = (message.Tokens ?? new Dictionary <string, string>()).ToDictionary(item => item.Key.ToLower(), item => item.Value),
                Reference       = message.Reference
            };

            return(_executionPolicy.ExecuteAsync(() => _clientWrapper.SendEmail(notifyMessage)));
        }
        private async Task <string> GetString(string url, string accessToken)
        {
            var result = string.Empty;

            try
            {
                await _executionPolicy.ExecuteAsync(
                    async() =>
                {
                    _httpClientWrapper.AuthScheme = "Bearer";
                    result = await _httpClientWrapper.GetString(url, accessToken);
                });
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error getting idams emails");
            }
            return(result);
        }
コード例 #7
0
        public async Task<HmrcTokenResponse> GetAuthenticationToken(string redirectUrl, string accessCode)
        {
            return await _executionPolicy.ExecuteAsync(async () =>
            {
                var requestParams = new
                {
                    client_secret = _configuration.Hmrc.ClientSecret,
                    client_id = _configuration.Hmrc.ClientId,
                    grant_type = "authorization_code",
                    redirect_uri = redirectUrl,
                    code = accessCode
                };

                var response = await _httpClientWrapper.SendMessage(requestParams, "oauth/token");

                return JsonConvert.DeserializeObject<HmrcTokenResponse>(response);
            });
        }