@ -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 , I SettingsService< OmbiSettings > s , ICacheService cache , I OmbiHttpClient client )
public Api ( ILogger < Api > log , I OmbiHttpClient 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,28 +29,17 @@ namespace Ombi.Api
{
using ( var httpRequestMessage = new HttpRequestMessage ( request . HttpMethod , request . FullUri ) )
{
// 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
}
AddHeadersBody ( request , httpRequestMessage ) ;
// Add headers
foreach ( var header in request . Headers )
{
httpRequestMessage . Headers . Add ( header . Key , header . Value ) ;
var httpResponseMessage = await _client . SendAsync ( httpRequestMessage ) ;
}
using ( var httpResponseMessage = await _client . SendAsync ( httpRequestMessage ) )
{
if ( ! httpResponseMessage . IsSuccessStatusCode )
{
LogError ( request , httpResponseMessage ) ;
}
// do something with the response
var data = httpResponseMessage . Content ;
var receivedString = await data . ReadAsStringAsync ( ) ;
var receivedString = await httpResponseMessage . Content . ReadAsStringAsync ( ) ;
if ( request . ContentType = = ContentType . Json )
{
request . OnBeforeDeserialization ? . Invoke ( receivedString ) ;
@ -74,7 +54,6 @@ namespace Ombi.Api
return value ;
}
}
}
}
@ -82,20 +61,9 @@ namespace Ombi.Api
{
using ( var httpRequestMessage = new HttpRequestMessage ( request . HttpMethod , request . FullUri ) )
{
// 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 ) ;
AddHeadersBody ( request , httpRequestMessage ) ;
}
using ( var httpResponseMessage = await _client . SendAsync ( httpRequestMessage ) )
{
var httpResponseMessage = await _client . SendAsync ( httpRequestMessage ) ;
if ( ! httpResponseMessage . IsSuccessStatusCode )
{
LogError ( request , httpResponseMessage ) ;
@ -103,36 +71,38 @@ namespace Ombi.Api
// do something with the response
var data = httpResponseMessage . Content ;
return await data . ReadAsStringAsync ( ) ;
}
}
}
public async Task Request ( Request request )
{
using ( var httpRequestMessage = new HttpRequestMessage ( request . HttpMethod , request . FullUri ) )
{
AddHeadersBody ( request , httpRequestMessage ) ;
var httpResponseMessage = await _client . SendAsync ( httpRequestMessage ) ;
if ( ! httpResponseMessage . IsSuccessStatusCode )
{
LogError ( request , httpResponseMessage ) ;
}
}
}
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
}
// 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 ) ;
}
}
}
}