Switch to use a single HTTPClient rather than a new one every request !dev

This commit is contained in:
tidusjar 2017-12-31 00:04:43 +00:00
commit bec5604720
4 changed files with 153 additions and 75 deletions

View file

@ -16,29 +16,18 @@ namespace Ombi.Api
{
public class Api : IApi
{
public Api(ILogger<Api> log, ISettingsService<OmbiSettings> s, ICacheService cache)
public Api(ILogger<Api> log, ISettingsService<OmbiSettings> s, ICacheService cache, IOmbiHttpClient client)
{
Logger = log;
_settings = s;
_cache = cache;
_client = client;
}
private ILogger<Api> Logger { get; }
private readonly ISettingsService<OmbiSettings> _settings;
private readonly ICacheService _cache;
private async Task<HttpMessageHandler> GetHandler()
{
var settings = await _cache.GetOrAdd(CacheKeys.OmbiSettings, async () => await _settings.GetSettingsAsync(), DateTime.Now.AddHours(1));
if (settings.IgnoreCertificateErrors)
{
return new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true,
};
}
return new HttpClientHandler();
}
private readonly IOmbiHttpClient _client;
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
@ -47,89 +36,82 @@ namespace Ombi.Api
public async Task<T> Request<T>(Request request)
{
using(var handler = await GetHandler())
using (var httpClient = new HttpClient(handler))
using (var httpRequestMessage = new HttpRequestMessage(request.HttpMethod, request.FullUri))
{
using (var httpRequestMessage = new HttpRequestMessage(request.HttpMethod, request.FullUri))
// Add the Json Body
if (request.JsonBody != null)
{
// Add the Json Body
if (request.JsonBody != null)
{
httpRequestMessage.Content = new JsonContent(request.JsonBody);
httpRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // Emby connect fails if we have the charset in the header
}
httpRequestMessage.Content = new JsonContent(request.JsonBody);
httpRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); // Emby connect fails if we have the charset in the header
}
// Add headers
foreach (var header in request.Headers)
{
httpRequestMessage.Headers.Add(header.Key, header.Value);
// Add headers
foreach (var header in request.Headers)
{
httpRequestMessage.Headers.Add(header.Key, header.Value);
}
using (var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage))
}
using (var httpResponseMessage = await _client.SendAsync(httpRequestMessage))
{
if (!httpResponseMessage.IsSuccessStatusCode)
{
if (!httpResponseMessage.IsSuccessStatusCode)
{
Logger.LogError(LoggingEvents.Api, $"StatusCode: {httpResponseMessage.StatusCode}, Reason: {httpResponseMessage.ReasonPhrase}");
}
// do something with the response
var data = httpResponseMessage.Content;
var receivedString = await data.ReadAsStringAsync();
if (request.ContentType == ContentType.Json)
{
request.OnBeforeDeserialization?.Invoke(receivedString);
return JsonConvert.DeserializeObject<T>(receivedString, Settings);
}
else
{
// XML
XmlSerializer serializer = new XmlSerializer(typeof(T));
StringReader reader = new StringReader(receivedString);
var value = (T)serializer.Deserialize(reader);
return value;
}
Logger.LogError(LoggingEvents.Api, $"StatusCode: {httpResponseMessage.StatusCode}, Reason: {httpResponseMessage.ReasonPhrase}");
}
// do something with the response
var data = httpResponseMessage.Content;
var receivedString = await data.ReadAsStringAsync();
if (request.ContentType == ContentType.Json)
{
request.OnBeforeDeserialization?.Invoke(receivedString);
return JsonConvert.DeserializeObject<T>(receivedString, Settings);
}
else
{
// XML
XmlSerializer serializer = new XmlSerializer(typeof(T));
StringReader reader = new StringReader(receivedString);
var value = (T)serializer.Deserialize(reader);
return value;
}
}
}
}
public async Task<string> RequestContent(Request request)
{
using (var httpClient = new HttpClient(await GetHandler()))
using (var httpRequestMessage = new HttpRequestMessage(request.HttpMethod, request.FullUri))
{
using (var httpRequestMessage = new HttpRequestMessage(request.HttpMethod, request.FullUri))
// Add the Json Body
if (request.JsonBody != null)
{
// Add the Json Body
if (request.JsonBody != null)
httpRequestMessage.Content = new JsonContent(request.JsonBody);
}
// Add headers
foreach (var header in request.Headers)
{
httpRequestMessage.Headers.Add(header.Key, header.Value);
}
using (var httpResponseMessage = await _client.SendAsync(httpRequestMessage))
{
if (!httpResponseMessage.IsSuccessStatusCode)
{
httpRequestMessage.Content = new JsonContent(request.JsonBody);
Logger.LogError(LoggingEvents.Api, $"StatusCode: {httpResponseMessage.StatusCode}, Reason: {httpResponseMessage.ReasonPhrase}");
}
// Add headers
foreach (var header in request.Headers)
{
httpRequestMessage.Headers.Add(header.Key, header.Value);
}
using (var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage))
{
if (!httpResponseMessage.IsSuccessStatusCode)
{
Logger.LogError(LoggingEvents.Api, $"StatusCode: {httpResponseMessage.StatusCode}, Reason: {httpResponseMessage.ReasonPhrase}");
}
// do something with the response
var data = httpResponseMessage.Content;
// do something with the response
var data = httpResponseMessage.Content;
return await data.ReadAsStringAsync();
}
return await data.ReadAsStringAsync();
}
}
}
public async Task Request(Request request)
{
using (var httpClient = new HttpClient(await GetHandler()))
{
using (var httpRequestMessage = new HttpRequestMessage(request.HttpMethod, request.FullUri))
{
// Add the Json Body
@ -144,7 +126,7 @@ namespace Ombi.Api
httpRequestMessage.Headers.Add(header.Key, header.Value);
}
using (var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage))
using (var httpResponseMessage = await _client.SendAsync(httpRequestMessage))
{
if (!httpResponseMessage.IsSuccessStatusCode)
{
@ -152,7 +134,6 @@ namespace Ombi.Api
}
}
}
}
}
}
}