refactor: Remove custom FlurlClientFactory

Instead, expand the ServarrRequestBuilder service class to utilize a
built-in client factory to manage handing out clients.
pull/231/head
Robert Dailey 1 year ago
parent 5a8e5df890
commit f5c659d772

@ -14,10 +14,10 @@ public class ApiServicesAutofacModule : Module
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
builder.RegisterType<FlurlClientFactory>().As<IFlurlClientFactory>().SingleInstance();
builder.RegisterType<PerBaseUrlFlurlClientFactory>().As<IFlurlClientFactory>().SingleInstance();
builder.RegisterType<SystemApiService>().As<ISystemApiService>();
builder.RegisterType<ServiceRequestBuilder>().As<IServiceRequestBuilder>();
builder.RegisterType<ServarrRequestBuilder>().As<IServarrRequestBuilder>();
builder.RegisterType<QualityProfileApiService>().As<IQualityProfileApiService>();
builder.RegisterType<CustomFormatApiService>().As<ICustomFormatApiService>();
builder.RegisterType<QualityDefinitionApiService>().As<IQualityDefinitionApiService>();

@ -7,9 +7,9 @@ namespace Recyclarr.ServarrApi.CustomFormat;
public class CustomFormatApiService : ICustomFormatApiService
{
private readonly IServiceRequestBuilder _service;
private readonly IServarrRequestBuilder _service;
public CustomFormatApiService(IServiceRequestBuilder service)
public CustomFormatApiService(IServarrRequestBuilder service)
{
_service = service;
}

@ -3,7 +3,7 @@ using Recyclarr.Config.Models;
namespace Recyclarr.ServarrApi.Http;
public interface IServiceRequestBuilder
public interface IServarrRequestBuilder
{
IFlurlRequest Request(IServiceConfiguration config, params object[] path);
}

@ -2,30 +2,32 @@ using Flurl;
using Flurl.Http;
using Flurl.Http.Configuration;
using Recyclarr.Common.Networking;
using Recyclarr.Config.Models;
using Recyclarr.Json;
using Recyclarr.Settings;
using Serilog;
namespace Recyclarr.ServarrApi.Http;
public sealed class FlurlClientFactory : IFlurlClientFactory
public class ServarrRequestBuilder : IServarrRequestBuilder
{
private readonly ILogger _log;
private readonly ISettingsProvider _settingsProvider;
private readonly PerBaseUrlFlurlClientFactory _factory;
private readonly IFlurlClientFactory _clientFactory;
public FlurlClientFactory(ILogger log, ISettingsProvider settingsProvider)
public ServarrRequestBuilder(ILogger log, ISettingsProvider settingsProvider, IFlurlClientFactory clientFactory)
{
_log = log;
_settingsProvider = settingsProvider;
_factory = new PerBaseUrlFlurlClientFactory();
_clientFactory = clientFactory;
}
public IFlurlClient Get(Url url)
public IFlurlRequest Request(IServiceConfiguration config, params object[] path)
{
var client = _factory.Get(url);
var client = _clientFactory.Get(config.BaseUrl.AppendPathSegments("api", "v3"));
client.Settings = GetClientSettings();
return client;
return client.Request(path)
.WithHeader("X-Api-Key", config.ApiKey);
}
private ClientFlurlHttpSettings GetClientSettings()
@ -48,9 +50,4 @@ public sealed class FlurlClientFactory : IFlurlClientFactory
return settings;
}
public void Dispose()
{
_factory.Dispose();
}
}

@ -1,22 +0,0 @@
using Flurl.Http;
using Flurl.Http.Configuration;
using Recyclarr.Config.Models;
namespace Recyclarr.ServarrApi.Http;
public class ServiceRequestBuilder : IServiceRequestBuilder
{
private readonly IFlurlClientFactory _clientFactory;
public ServiceRequestBuilder(IFlurlClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public IFlurlRequest Request(IServiceConfiguration config, params object[] path)
{
var client = _clientFactory.Get(config.BaseUrl);
return client.Request(new[] {"api", "v3"}.Concat(path).ToArray())
.WithHeader("X-Api-Key", config.ApiKey);
}
}

@ -7,9 +7,9 @@ namespace Recyclarr.ServarrApi.MediaNaming;
public class MediaNamingApiService : IMediaNamingApiService
{
private readonly IServiceRequestBuilder _service;
private readonly IServarrRequestBuilder _service;
public MediaNamingApiService(IServiceRequestBuilder service)
public MediaNamingApiService(IServarrRequestBuilder service)
{
_service = service;
}

@ -6,9 +6,9 @@ namespace Recyclarr.ServarrApi.QualityDefinition;
internal class QualityDefinitionApiService : IQualityDefinitionApiService
{
private readonly IServiceRequestBuilder _service;
private readonly IServarrRequestBuilder _service;
public QualityDefinitionApiService(IServiceRequestBuilder service)
public QualityDefinitionApiService(IServarrRequestBuilder service)
{
_service = service;
}

@ -6,9 +6,9 @@ namespace Recyclarr.ServarrApi.QualityProfile;
internal class QualityProfileApiService : IQualityProfileApiService
{
private readonly IServiceRequestBuilder _service;
private readonly IServarrRequestBuilder _service;
public QualityProfileApiService(IServiceRequestBuilder service)
public QualityProfileApiService(IServarrRequestBuilder service)
{
_service = service;
}

@ -6,9 +6,9 @@ namespace Recyclarr.ServarrApi.ReleaseProfile;
public class ReleaseProfileApiService : IReleaseProfileApiService
{
private readonly IServiceRequestBuilder _service;
private readonly IServarrRequestBuilder _service;
public ReleaseProfileApiService(IServiceRequestBuilder service)
public ReleaseProfileApiService(IServarrRequestBuilder service)
{
_service = service;
}

@ -6,9 +6,9 @@ namespace Recyclarr.ServarrApi.System;
public class SystemApiService : ISystemApiService
{
private readonly IServiceRequestBuilder _service;
private readonly IServarrRequestBuilder _service;
public SystemApiService(IServiceRequestBuilder service)
public SystemApiService(IServarrRequestBuilder service)
{
_service = service;
}

@ -6,9 +6,9 @@ namespace Recyclarr.ServarrApi.Tag;
public class SonarrTagApiService : ISonarrTagApiService
{
private readonly IServiceRequestBuilder _service;
private readonly IServarrRequestBuilder _service;
public SonarrTagApiService(IServiceRequestBuilder service)
public SonarrTagApiService(IServarrRequestBuilder service)
{
_service = service;
}

Loading…
Cancel
Save