mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-12 08:16:05 -07:00
Some memory management improvements
This commit is contained in:
parent
059b083359
commit
6ac9da8bed
30 changed files with 362 additions and 100 deletions
|
@ -1,32 +1,23 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Security.Authentication;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ombi.Core.Settings;
|
||||
using Ombi.Helpers;
|
||||
using Ombi.Settings.Settings.Models;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class Api : IApi
|
||||
{
|
||||
public Api(ILogger<Api> log, ISettingsService<OmbiSettings> s, ICacheService cache, IOmbiHttpClient client)
|
||||
public Api(ILogger<Api> log, 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 readonly IOmbiHttpClient _client;
|
||||
|
||||
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
|
||||
|
@ -38,41 +29,29 @@ namespace Ombi.Api
|
|||
{
|
||||
using (var httpRequestMessage = new HttpRequestMessage(request.HttpMethod, request.FullUri))
|
||||
{
|
||||
// Add the Json Body
|
||||
if (request.JsonBody != null)
|
||||
AddHeadersBody(request, httpRequestMessage);
|
||||
|
||||
var httpResponseMessage = await _client.SendAsync(httpRequestMessage);
|
||||
|
||||
if (!httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
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
|
||||
LogError(request, httpResponseMessage);
|
||||
}
|
||||
|
||||
// Add headers
|
||||
foreach (var header in request.Headers)
|
||||
// do something with the response
|
||||
var receivedString = await httpResponseMessage.Content.ReadAsStringAsync();
|
||||
if (request.ContentType == ContentType.Json)
|
||||
{
|
||||
httpRequestMessage.Headers.Add(header.Key, header.Value);
|
||||
|
||||
request.OnBeforeDeserialization?.Invoke(receivedString);
|
||||
return JsonConvert.DeserializeObject<T>(receivedString, Settings);
|
||||
}
|
||||
using (var httpResponseMessage = await _client.SendAsync(httpRequestMessage))
|
||||
else
|
||||
{
|
||||
if (!httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
LogError(request, httpResponseMessage);
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
// XML
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(T));
|
||||
StringReader reader = new StringReader(receivedString);
|
||||
var value = (T)serializer.Deserialize(reader);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,30 +61,17 @@ namespace Ombi.Api
|
|||
{
|
||||
using (var httpRequestMessage = new HttpRequestMessage(request.HttpMethod, request.FullUri))
|
||||
{
|
||||
// Add the Json Body
|
||||
if (request.JsonBody != null)
|
||||
AddHeadersBody(request, httpRequestMessage);
|
||||
|
||||
var httpResponseMessage = await _client.SendAsync(httpRequestMessage);
|
||||
if (!httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
httpRequestMessage.Content = new JsonContent(request.JsonBody);
|
||||
LogError(request, httpResponseMessage);
|
||||
}
|
||||
// do something with the response
|
||||
var data = httpResponseMessage.Content;
|
||||
|
||||
// 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)
|
||||
{
|
||||
LogError(request, httpResponseMessage);
|
||||
}
|
||||
// do something with the response
|
||||
var data = httpResponseMessage.Content;
|
||||
|
||||
|
||||
return await data.ReadAsStringAsync();
|
||||
}
|
||||
return await data.ReadAsStringAsync();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -114,25 +80,29 @@ namespace Ombi.Api
|
|||
{
|
||||
using (var httpRequestMessage = new HttpRequestMessage(request.HttpMethod, request.FullUri))
|
||||
{
|
||||
// Add the Json Body
|
||||
if (request.JsonBody != null)
|
||||
AddHeadersBody(request, httpRequestMessage);
|
||||
var httpResponseMessage = await _client.SendAsync(httpRequestMessage);
|
||||
if (!httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
httpRequestMessage.Content = new JsonContent(request.JsonBody);
|
||||
LogError(request, httpResponseMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add headers
|
||||
foreach (var header in request.Headers)
|
||||
{
|
||||
httpRequestMessage.Headers.Add(header.Key, header.Value);
|
||||
private static void AddHeadersBody(Request request, HttpRequestMessage httpRequestMessage)
|
||||
{
|
||||
// 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
|
||||
}
|
||||
|
||||
}
|
||||
using (var httpResponseMessage = await _client.SendAsync(httpRequestMessage))
|
||||
{
|
||||
if (!httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
LogError(request, httpResponseMessage);
|
||||
}
|
||||
}
|
||||
// Add headers
|
||||
foreach (var header in request.Headers)
|
||||
{
|
||||
httpRequestMessage.Headers.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue