コード例 #1
0
        private static void AddSecurityTokenHeader(HttpRequestMessage request, ApiResponse <Token> credentials,
                                                   BankClientOptions options)
        {
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", credentials.Content.AccessToken);

            request.Headers.Add(InternalConstants.HttpHeaderSubscriptionKey, options.ApiSubscriptionKey);
        }
コード例 #2
0
 public BankHttpClient(HttpClient httpClient,
                       IAppLogger logger,
                       JsonSerializer jsonSerializer,
                       BankClientOptions bankOptions)
 {
     _httpClient     = httpClient;
     _logger         = logger;
     _jsonSerializer = jsonSerializer;
     _bankOptions    = bankOptions;
 }
コード例 #3
0
 public BankAuthHandler(ILogger <BankAuthHandler> logger,
                        IOptions <BankClientOptions> options, AzureServiceTokenProvider tokenProvider, IAuthClient auth,
                        IAppCache cache)
 {
     this.logger        = logger;
     this.tokenProvider = tokenProvider;
     this.options       = options.Value;
     this.auth          = auth;
     this.cache         = cache;
 }
コード例 #4
0
 public void Setup()
 {
     _loggerMock        = new Mock <IAppLogger>();
     _httpClientMock    = new Mock <HttpClient>();
     _jsonSerializer    = new JsonSerializer();
     _BankClientOptions = new BankClientOptions()
     {
         Url = "http://localhost"
     };
 }
コード例 #5
0
        public void InstallServices(IServiceCollection services, IConfiguration configuration)
        {
            var bankClientOptions = new BankClientOptions();

            configuration.Bind(nameof(bankClientOptions), bankClientOptions);
            services.AddSingleton(bankClientOptions);

            services.AddTransient <HttpExceptionHandler>();

            var logger = services.BuildServiceProvider().GetService <IAppLogger>();

            services.AddHttpClient <IBankHttpClient, BankHttpClient>(x =>
            {
                x.Timeout = TimeSpan.FromSeconds(bankClientOptions.Timeout);
                x.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                x.BaseAddress = new Uri(bankClientOptions.BaseUrl);
            })
            .AddHttpMessageHandler <HttpExceptionHandler>()
            .AddPolicyHandler(GetRetryPolicy(logger, bankClientOptions));
        }
コード例 #6
0
 private static IAsyncPolicy <HttpResponseMessage> GetRetryPolicy(IAppLogger logger, BankClientOptions options)
 {
     return(HttpPolicyExtensions
            .HandleTransientHttpError()
            .Or <OperationCanceledException>()
            .OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
            .WaitAndRetryAsync(
                options.RetryCount,
                retryAttempt => TimeSpan.FromSeconds(options.RetryWait),
                (result, span) =>
     {
         logger.Write(LogLevel.Warning, $"{EventCodes.BankApiRetry} - {result.Exception.Message}");
     }));
 }