2017-03-17 13:12:34 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
2017-05-04 13:01:14 +02:00
|
|
|
|
using System.Net;
|
2017-03-17 13:12:34 +01:00
|
|
|
|
using System.Net.Http;
|
2017-05-04 13:01:14 +02:00
|
|
|
|
using System.Net.Http.Headers;
|
2017-03-17 13:12:34 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-04-06 16:59:17 +02:00
|
|
|
|
namespace Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http
|
2017-03-17 13:12:34 +01:00
|
|
|
|
{
|
2017-03-28 14:30:30 -07:00
|
|
|
|
public class StandardHttpClient : IHttpClient
|
2017-03-17 13:12:34 +01:00
|
|
|
|
{
|
|
|
|
|
private HttpClient _client;
|
2017-03-29 11:20:33 +02:00
|
|
|
|
private ILogger<StandardHttpClient> _logger;
|
2017-05-04 13:01:14 +02:00
|
|
|
|
|
2017-03-29 11:20:33 +02:00
|
|
|
|
public StandardHttpClient(ILogger<StandardHttpClient> logger)
|
2017-03-17 13:12:34 +01:00
|
|
|
|
{
|
|
|
|
|
_client = new HttpClient();
|
2017-03-29 11:20:33 +02:00
|
|
|
|
_logger = logger;
|
2017-03-17 13:12:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-04 13:01:14 +02:00
|
|
|
|
public async Task<string> GetStringAsync(string uri, string authorizationToken = null, string authorizationMethod = "Bearer")
|
2017-03-17 13:12:34 +01:00
|
|
|
|
{
|
2017-05-04 13:01:14 +02:00
|
|
|
|
var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
|
|
|
|
|
|
|
|
|
|
if (authorizationToken != null)
|
|
|
|
|
{
|
|
|
|
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(authorizationMethod, authorizationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response = await _client.SendAsync(requestMessage);
|
|
|
|
|
|
|
|
|
|
return await response.Content.ReadAsStringAsync();
|
2017-03-17 13:12:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-04 13:01:14 +02:00
|
|
|
|
public async Task<HttpResponseMessage> PostAsync<T>(string uri, T item, string authorizationToken = null, string requestId = null, string authorizationMethod = "Bearer")
|
|
|
|
|
{
|
|
|
|
|
// a new StringContent must be created for each retry
|
|
|
|
|
// as it is disposed after each call
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// raise exception if HttpResponseCode 500
|
|
|
|
|
// needed for circuit breaker to track fails
|
|
|
|
|
|
|
|
|
|
if (response.StatusCode == HttpStatusCode.InternalServerError)
|
|
|
|
|
{
|
|
|
|
|
throw new HttpRequestException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<HttpResponseMessage> DeleteAsync(string uri, string authorizationToken = null, string requestId = null, string authorizationMethod = "Bearer")
|
|
|
|
|
{
|
|
|
|
|
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 13:12:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|