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); }
public BankHttpClient(HttpClient httpClient, IAppLogger logger, JsonSerializer jsonSerializer, BankClientOptions bankOptions) { _httpClient = httpClient; _logger = logger; _jsonSerializer = jsonSerializer; _bankOptions = bankOptions; }
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; }
public void Setup() { _loggerMock = new Mock <IAppLogger>(); _httpClientMock = new Mock <HttpClient>(); _jsonSerializer = new JsonSerializer(); _BankClientOptions = new BankClientOptions() { Url = "http://localhost" }; }
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)); }
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}"); })); }