@ -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,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 httpR esponseMessage = await _c lient. SendA sync( httpR equestMessage) )
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 )
{
httpRequestMessage . Content = new JsonContent ( request . JsonBody ) ;
}
AddHeadersBody ( request , httpRequestMessage ) ;
// Add headers
foreach ( var header in request . Headers )
var httpResponseMessage = await _client . SendAsync ( httpRequestMessage ) ;
if ( ! httpResponseMessage . IsSuccessStatusCode )
{
httpRequestMessage . Headers . Add ( header . Key , header . Value ) ;
LogError ( request , httpResponseMessage ) ;
}
using ( var httpResponseMessage = await _client . SendAsync ( httpRequestMessage ) )
{
if ( ! httpResponseMessage . IsSuccessStatusCode )
{
LogError ( request , httpResponseMessage ) ;
}
// 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 ( ) ;
}
}
@ -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 ) ;
}
}