2017-04-07 12:48:22 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Polly;
|
|
|
|
|
using Polly.Wrap;
|
|
|
|
|
using System;
|
2017-03-27 14:05:28 +02:00
|
|
|
|
using System.Net;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
using System.Net.Http;
|
2017-05-04 13:01:14 +02:00
|
|
|
|
using System.Net.Http.Headers;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-04-06 16:59:17 +02:00
|
|
|
|
namespace Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http
|
2017-03-17 10:00:18 +01:00
|
|
|
|
{
|
2017-03-22 12:24:55 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// HttpClient wrapper that integrates Retry and Circuit
|
2017-03-28 19:48:04 -07:00
|
|
|
|
/// breaker policies when invoking HTTP services.
|
2017-03-29 13:16:00 -07:00
|
|
|
|
/// Based on Polly library: https://github.com/App-vNext/Polly
|
2017-03-22 12:24:55 +01:00
|
|
|
|
/// </summary>
|
2017-03-28 14:30:30 -07:00
|
|
|
|
public class ResilientHttpClient : IHttpClient
|
2017-03-17 10:00:18 +01:00
|
|
|
|
{
|
|
|
|
|
private HttpClient _client;
|
|
|
|
|
private PolicyWrap _policyWrapper;
|
2017-03-29 11:20:33 +02:00
|
|
|
|
private ILogger<ResilientHttpClient> _logger;
|
2017-05-04 13:01:14 +02:00
|
|
|
|
//public HttpClient Inst => _client;
|
2017-03-31 14:20:03 +02:00
|
|
|
|
|
2017-04-07 12:48:22 +02:00
|
|
|
|
public ResilientHttpClient(Policy[] policies, ILogger<ResilientHttpClient> logger)
|
2017-03-17 10:00:18 +01:00
|
|
|
|
{
|
|
|
|
|
_client = new HttpClient();
|
2017-03-29 11:20:33 +02:00
|
|
|
|
_logger = logger;
|
2017-03-17 10:00:18 +01:00
|
|
|
|
|
|
|
|
|
// Add Policies to be applied
|
2017-04-07 12:48:22 +02:00
|
|
|
|
_policyWrapper = Policy.WrapAsync(policies);
|
2017-05-04 13:01:14 +02:00
|
|
|
|
}
|
2017-03-28 19:48:04 -07:00
|
|
|
|
|
2017-05-04 13:01:14 +02:00
|
|
|
|
public Task<string> GetStringAsync(string uri, string authorizationToken = null, string authorizationMethod = "Bearer")
|
|
|
|
|
{
|
|
|
|
|
return HttpInvoker(async () =>
|
|
|
|
|
{
|
|
|
|
|
var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
|
|
|
|
|
|
|
|
|
|
if (authorizationToken != null)
|
|
|
|
|
{
|
|
|
|
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(authorizationMethod, authorizationToken);
|
|
|
|
|
}
|
2017-03-20 14:18:20 -04:00
|
|
|
|
|
2017-05-04 13:01:14 +02:00
|
|
|
|
var response = await _client.SendAsync(requestMessage);
|
|
|
|
|
|
|
|
|
|
return await response.Content.ReadAsStringAsync();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<HttpResponseMessage> PostAsync<T>(string uri, T item, string authorizationToken = null, string requestId = null, string authorizationMethod = "Bearer")
|
|
|
|
|
{
|
2017-03-17 10:00:18 +01:00
|
|
|
|
// a new StringContent must be created for each retry
|
|
|
|
|
// as it is disposed after each call
|
2017-05-04 13:01:14 +02:00
|
|
|
|
return HttpInvoker(async () =>
|
2017-03-27 14:05:28 +02:00
|
|
|
|
{
|
2017-05-04 13:01:14 +02:00
|
|
|
|
var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri);
|
|
|
|
|
|
|
|
|
|
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(item), System.Text.Encoding.UTF8, "application/json");
|
|
|
|
|
|
|
|
|
|
if (authorizationToken != null)
|
|
|
|
|
{
|
|
|
|
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(authorizationMethod, authorizationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (requestId != null)
|
|
|
|
|
{
|
|
|
|
|
requestMessage.Headers.Add("x-requestid", requestId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response = await _client.SendAsync(requestMessage);
|
|
|
|
|
|
2017-03-27 14:05:28 +02:00
|
|
|
|
// raise exception if HttpResponseCode 500
|
|
|
|
|
// needed for circuit breaker to track fails
|
2017-05-04 13:01:14 +02:00
|
|
|
|
|
|
|
|
|
if (response.StatusCode == HttpStatusCode.InternalServerError)
|
2017-03-22 12:24:55 +01:00
|
|
|
|
{
|
2017-03-27 14:05:28 +02:00
|
|
|
|
throw new HttpRequestException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
});
|
2017-05-04 13:01:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Task<HttpResponseMessage> DeleteAsync(string uri, string authorizationToken = null, string requestId = null, string authorizationMethod = "Bearer")
|
|
|
|
|
{
|
|
|
|
|
return HttpInvoker(async () =>
|
|
|
|
|
{
|
|
|
|
|
var requestMessage = new HttpRequestMessage(HttpMethod.Delete, uri);
|
|
|
|
|
|
|
|
|
|
if (authorizationToken != null)
|
|
|
|
|
{
|
|
|
|
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(authorizationMethod, authorizationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (requestId != null)
|
|
|
|
|
{
|
|
|
|
|
requestMessage.Headers.Add("x-requestid", requestId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await _client.SendAsync(requestMessage);
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-03-17 10:00:18 +01:00
|
|
|
|
|
|
|
|
|
|
2017-03-20 14:18:20 -04:00
|
|
|
|
|
2017-05-04 13:01:14 +02:00
|
|
|
|
private Task<T> HttpInvoker<T>(Func<Task<T>> action)
|
|
|
|
|
{
|
2017-03-17 10:00:18 +01:00
|
|
|
|
// Executes the action applying all
|
|
|
|
|
// the policies defined in the wrapper
|
2017-05-04 13:01:14 +02:00
|
|
|
|
return _policyWrapper.ExecuteAsync(() => action());
|
|
|
|
|
}
|
2017-03-17 10:00:18 +01:00
|
|
|
|
}
|
|
|
|
|
}
|