refactor: dedicated server info url builder

recyclarr
Robert Dailey 3 years ago
parent 7c16d5fcbf
commit e197fb2bd0

@ -21,11 +21,6 @@ namespace Trash.Tests.Config
{
public const string ServiceName = "test_service";
public override string BuildUrl()
{
throw new NotImplementedException();
}
public override bool IsValid(out string msg)
{
throw new NotImplementedException();

@ -63,11 +63,18 @@ namespace Trash
private static void RadarrRegistrations(ContainerBuilder builder)
{
// Api Services
// Services
builder.RegisterType<QualityDefinitionService>().As<IQualityDefinitionService>();
builder.RegisterType<CustomFormatService>().As<ICustomFormatService>();
builder.RegisterType<QualityProfileService>().As<IQualityProfileService>();
builder.Register(c =>
{
var config = c.Resolve<IConfigurationProvider>().ActiveConfiguration;
return new ServerInfo(config.BaseUrl, config.ApiKey);
})
.As<IServerInfo>();
// Quality Definition Support
builder.RegisterType<RadarrQualityDefinitionUpdater>();
builder.RegisterType<RadarrQualityDefinitionGuideParser>().As<IRadarrQualityDefinitionGuideParser>();
@ -78,7 +85,7 @@ namespace Trash
builder.RegisterType<CachePersister>().As<ICachePersister>();
// Guide Processor
builder.RegisterType<GuideProcessor>().As<IGuideProcessor>();
builder.RegisterType<GuideProcessor>().As<IGuideProcessor>(); // todo: register as singleton to avoid parsing guide multiple times when using 2 or more instances in config
builder.RegisterAggregateService<IGuideProcessorSteps>();
builder.RegisterType<CustomFormatStep>().As<ICustomFormatStep>();
builder.RegisterType<ConfigStep>().As<IConfigStep>();

@ -0,0 +1,7 @@
namespace Trash.Config
{
public interface IServerInfo
{
string BuildUrl();
}
}

@ -4,7 +4,6 @@ namespace Trash.Config
{
string BaseUrl { get; init; }
string ApiKey { get; init; }
string BuildUrl();
bool IsValid(out string msg);
}
}

@ -0,0 +1,23 @@
using Flurl;
namespace Trash.Config
{
public class ServerInfo : IServerInfo
{
private readonly string _apiKey;
private readonly string _baseUrl;
public ServerInfo(string baseUrl, string apiKey)
{
_baseUrl = baseUrl;
_apiKey = apiKey;
}
public string BuildUrl()
{
return _baseUrl
.AppendPathSegment("api/v3")
.SetQueryParams(new {apikey = _apiKey});
}
}
}

@ -10,7 +10,6 @@ namespace Trash.Config
[Required(ErrorMessage = "Property 'api_key' is required")]
public string ApiKey { get; init; } = "";
public abstract string BuildUrl();
public abstract bool IsValid(out string msg);
}
}

@ -10,23 +10,25 @@ namespace Trash.Radarr.CustomFormat.Api
{
internal class CustomFormatService : ICustomFormatService
{
private readonly IConfigurationProvider _configProvider;
private readonly IServerInfo _serverInfo;
public CustomFormatService(IConfigurationProvider configProvider)
public CustomFormatService(IServerInfo serverInfo)
{
_configProvider = configProvider;
_serverInfo = serverInfo;
}
private string BaseUrl => _serverInfo.BuildUrl();
public async Task<List<JObject>> GetCustomFormats()
{
return await BaseUrl()
return await BaseUrl
.AppendPathSegment("customformat")
.GetJsonAsync<List<JObject>>();
}
public async Task CreateCustomFormat(ProcessedCustomFormatData cf)
{
var response = await BaseUrl()
var response = await BaseUrl
.AppendPathSegment("customformat")
.PostJsonAsync(cf.Json)
.ReceiveJson<JObject>();
@ -36,7 +38,7 @@ namespace Trash.Radarr.CustomFormat.Api
public async Task UpdateCustomFormat(ProcessedCustomFormatData cf)
{
await BaseUrl()
await BaseUrl
.AppendPathSegment($"customformat/{cf.GetCustomFormatId()}")
.PutJsonAsync(cf.Json)
.ReceiveJson<JObject>();
@ -44,14 +46,9 @@ namespace Trash.Radarr.CustomFormat.Api
public async Task DeleteCustomFormat(int customFormatId)
{
await BaseUrl()
await BaseUrl
.AppendPathSegment($"customformat/{customFormatId}")
.DeleteAsync();
}
private string BaseUrl()
{
return _configProvider.ActiveConfiguration.BuildUrl();
}
}
}

@ -9,14 +9,14 @@ namespace Trash.Radarr.CustomFormat.Api
{
internal class QualityProfileService : IQualityProfileService
{
private readonly IConfigurationProvider _configProvider;
private readonly IServerInfo _serverInfo;
public QualityProfileService(IConfigurationProvider configProvider)
public QualityProfileService(IServerInfo serverInfo)
{
_configProvider = configProvider;
_serverInfo = serverInfo;
}
private string BaseUrl => _configProvider.ActiveConfiguration.BuildUrl();
private string BaseUrl => _serverInfo.BuildUrl();
public async Task<List<JObject>> GetQualityProfiles()
{

@ -9,14 +9,14 @@ namespace Trash.Radarr.QualityDefinition.Api
{
public class QualityDefinitionService : IQualityDefinitionService
{
private readonly IConfigurationProvider _configProvider;
private readonly IServerInfo _serverInfo;
public QualityDefinitionService(IConfigurationProvider configProvider)
public QualityDefinitionService(IServerInfo serverInfo)
{
_configProvider = configProvider;
_serverInfo = serverInfo;
}
private string BaseUrl => _configProvider.ActiveConfiguration.BuildUrl();
private string BaseUrl => _serverInfo.BuildUrl();
public async Task<List<RadarrQualityDefinitionItem>> GetQualityDefinition()
{

@ -18,7 +18,7 @@ namespace Trash.Radarr
public class RadarrCommand : ServiceCommand, IRadarrCommand
{
private readonly IConfigurationLoader<RadarrConfiguration> _configLoader;
private readonly Lazy<ICustomFormatUpdater> _customFormatUpdater;
private readonly Func<ICustomFormatUpdater> _customFormatUpdaterFactory;
private readonly Func<RadarrQualityDefinitionUpdater> _qualityUpdaterFactory;
public RadarrCommand(
@ -27,12 +27,12 @@ namespace Trash.Radarr
ILogJanitor logJanitor,
IConfigurationLoader<RadarrConfiguration> configLoader,
Func<RadarrQualityDefinitionUpdater> qualityUpdaterFactory,
Lazy<ICustomFormatUpdater> customFormatUpdater)
Func<ICustomFormatUpdater> customFormatUpdaterFactory)
: base(logger, loggingLevelSwitch, logJanitor)
{
_configLoader = configLoader;
_qualityUpdaterFactory = qualityUpdaterFactory;
_customFormatUpdater = customFormatUpdater;
_customFormatUpdaterFactory = customFormatUpdaterFactory;
}
public override string CacheStoragePath { get; } =
@ -51,7 +51,7 @@ namespace Trash.Radarr
if (config.CustomFormats.Count > 0)
{
await _customFormatUpdater.Value.Process(this, config);
await _customFormatUpdaterFactory().Process(this, config);
}
}
}

@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Flurl;
using JetBrains.Annotations;
using Trash.Config;
using Trash.Radarr.QualityDefinition;
@ -15,13 +14,6 @@ namespace Trash.Radarr
public List<CustomFormatConfig> CustomFormats { get; init; } = new();
public bool DeleteOldCustomFormats { get; init; }
public override string BuildUrl()
{
return BaseUrl
.AppendPathSegment("api/v3")
.SetQueryParams(new {apikey = ApiKey});
}
public override bool IsValid(out string msg)
{
if (CustomFormats.Any(cf => cf.TrashIds.Count + cf.Names.Count == 0))

@ -10,11 +10,11 @@ namespace Trash.Sonarr.Api
{
public class SonarrApi : ISonarrApi
{
private readonly IConfigurationProvider _configProvider;
private readonly IServerInfo _serverInfo;
public SonarrApi(IConfigurationProvider configProvider)
public SonarrApi(IServerInfo serverInfo)
{
_configProvider = configProvider;
_serverInfo = serverInfo;
}
public async Task<Version> GetVersion()
@ -78,6 +78,6 @@ namespace Trash.Sonarr.Api
.ReceiveJson<List<SonarrQualityDefinitionItem>>();
}
private string BaseUrl() => _configProvider.ActiveConfiguration.BuildUrl();
private string BaseUrl() => _serverInfo.BuildUrl();
}
}

@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Flurl;
using JetBrains.Annotations;
using Trash.Config;
using Trash.Sonarr.QualityDefinition;
@ -14,13 +13,6 @@ namespace Trash.Sonarr
public IList<ReleaseProfileConfig> ReleaseProfiles { get; set; } = new List<ReleaseProfileConfig>();
public SonarrQualityDefinitionType? QualityDefinition { get; init; }
public override string BuildUrl()
{
return BaseUrl
.AppendPathSegment("api/v3")
.SetQueryParams(new {apikey = ApiKey});
}
public override bool IsValid(out string msg)
{
msg = "";

Loading…
Cancel
Save