refactor(config): make IServiceConfiguration available via DI

recyclarr
Robert Dailey 3 years ago
parent b3ab16b75b
commit bb07aec749

@ -30,7 +30,7 @@ namespace Trash.Tests.Config
public void Load_UsingStream_CorrectParsing() public void Load_UsingStream_CorrectParsing()
{ {
var configLoader = new ConfigurationLoader<SonarrConfiguration>( var configLoader = new ConfigurationLoader<SonarrConfiguration>(
Substitute.For<IConfigurationProvider<SonarrConfiguration>>(), Substitute.For<IConfigurationProvider>(),
Substitute.For<IFileSystem>(), Substitute.For<IFileSystem>(),
new DefaultObjectFactory()); new DefaultObjectFactory());
@ -81,7 +81,7 @@ namespace Trash.Tests.Config
fs.File.OpenText(Arg.Any<string>()) fs.File.OpenText(Arg.Any<string>())
.Returns(MockYaml(1, 2), MockYaml(3)); .Returns(MockYaml(1, 2), MockYaml(3));
var provider = Substitute.For<IConfigurationProvider<SonarrConfiguration>>(); var provider = Substitute.For<IConfigurationProvider>();
// var objectFactory = Substitute.For<IObjectFactory>(); // var objectFactory = Substitute.For<IObjectFactory>();
// objectFactory.Create(Arg.Any<Type>()) // objectFactory.Create(Arg.Any<Type>())
// .Returns(t => Substitute.For(new[] {(Type)t[0]}, Array.Empty<object>())); // .Returns(t => Substitute.For(new[] {(Type)t[0]}, Array.Empty<object>()));

@ -35,7 +35,7 @@ test_service:
- api_key: b - api_key: b
"; ";
var loader = new ConfigurationLoader<TestServiceConfiguration>( var loader = new ConfigurationLoader<TestServiceConfiguration>(
Substitute.For<IConfigurationProvider<TestServiceConfiguration>>(), Substitute.For<IConfigurationProvider>(),
Substitute.For<IFileSystem>(), Substitute.For<IFileSystem>(),
new DefaultObjectFactory()); new DefaultObjectFactory());
@ -53,7 +53,7 @@ test_service:
- base_url: a - base_url: a
"; ";
var loader = new ConfigurationLoader<TestServiceConfiguration>( var loader = new ConfigurationLoader<TestServiceConfiguration>(
Substitute.For<IConfigurationProvider<TestServiceConfiguration>>(), Substitute.For<IConfigurationProvider>(),
Substitute.For<IFileSystem>(), Substitute.For<IFileSystem>(),
new DefaultObjectFactory()); new DefaultObjectFactory());

@ -1,4 +1,4 @@
using System; using System;
using System.IO; using System.IO;
using System.IO.Abstractions; using System.IO.Abstractions;
using FluentAssertions; using FluentAssertions;
@ -26,7 +26,7 @@ radarr:
preferred_ratio: 0.5 preferred_ratio: 0.5
"; ";
var loader = new ConfigurationLoader<RadarrConfiguration>( var loader = new ConfigurationLoader<RadarrConfiguration>(
Substitute.For<IConfigurationProvider<RadarrConfiguration>>(), Substitute.For<IConfigurationProvider>(),
Substitute.For<IFileSystem>(), Substitute.For<IFileSystem>(),
new DefaultObjectFactory()); new DefaultObjectFactory());

@ -26,7 +26,7 @@ sonarr:
- strict_negative_scores: true - strict_negative_scores: true
"; ";
var loader = new ConfigurationLoader<SonarrConfiguration>( var loader = new ConfigurationLoader<SonarrConfiguration>(
Substitute.For<IConfigurationProvider<SonarrConfiguration>>(), Substitute.For<IConfigurationProvider>(),
Substitute.For<IFileSystem>(), Substitute.For<IFileSystem>(),
new DefaultObjectFactory()); new DefaultObjectFactory());

@ -1,19 +0,0 @@
using FluentAssertions.Formatting;
using Trash.Sonarr.ReleaseProfile;
namespace Trash.Tests.ValueFormatters
{
public class ProfileDataValueFormatter : IValueFormatter
{
public bool CanHandle(object value)
{
return value is ProfileData;
}
public string Format(object value, FormattingContext context, FormatChild formatChild)
{
var profileData = (ProfileData) value;
return $"{profileData.Ignored}";
}
}
}

@ -56,11 +56,18 @@ namespace Trash
private static void ConfigurationRegistrations(ContainerBuilder builder) private static void ConfigurationRegistrations(ContainerBuilder builder)
{ {
builder.RegisterType<ObjectFactory>().As<IObjectFactory>(); builder.RegisterType<ObjectFactory>()
builder.RegisterGeneric(typeof(ConfigurationLoader<>)).As(typeof(IConfigurationLoader<>)); .As<IObjectFactory>();
builder.RegisterGeneric(typeof(ConfigurationProvider<>))
.As(typeof(IConfigurationProvider<>)) builder.RegisterGeneric(typeof(ConfigurationLoader<>))
.As(typeof(IConfigurationLoader<>));
builder.RegisterType<ConfigurationProvider>()
.As<IConfigurationProvider>()
.SingleInstance(); .SingleInstance();
builder.Register(c => c.Resolve<IConfigurationProvider>().ActiveConfiguration)
.As<IServiceConfiguration>();
} }
private static void CommandRegistrations(ContainerBuilder builder) private static void CommandRegistrations(ContainerBuilder builder)

@ -13,11 +13,11 @@ namespace Trash.Config
public class ConfigurationLoader<T> : IConfigurationLoader<T> public class ConfigurationLoader<T> : IConfigurationLoader<T>
where T : ServiceConfiguration where T : ServiceConfiguration
{ {
private readonly IConfigurationProvider<T> _configProvider; private readonly IConfigurationProvider _configProvider;
private readonly IDeserializer _deserializer; private readonly IDeserializer _deserializer;
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
public ConfigurationLoader(IConfigurationProvider<T> configProvider, IFileSystem fileSystem, public ConfigurationLoader(IConfigurationProvider configProvider, IFileSystem fileSystem,
IObjectFactory objectFactory) IObjectFactory objectFactory)
{ {
_configProvider = configProvider; _configProvider = configProvider;

@ -1,8 +1,15 @@
namespace Trash.Config using System;
namespace Trash.Config
{ {
internal class ConfigurationProvider<T> : IConfigurationProvider<T> internal class ConfigurationProvider : IConfigurationProvider
where T : ServiceConfiguration
{ {
public T? ActiveConfiguration { get; set; } private IServiceConfiguration? _activeConfiguration;
public IServiceConfiguration ActiveConfiguration
{
get => _activeConfiguration ?? throw new NullReferenceException("Active configuration has not been set");
set => _activeConfiguration = value;
}
} }
} }

@ -1,8 +1,7 @@
namespace Trash.Config namespace Trash.Config
{ {
public interface IConfigurationProvider<T> public interface IConfigurationProvider
where T : ServiceConfiguration
{ {
T? ActiveConfiguration { get; set; } IServiceConfiguration ActiveConfiguration { get; set; }
} }
} }

@ -0,0 +1,9 @@
namespace Trash.Config
{
public interface IServiceConfiguration
{
string BaseUrl { get; init; }
string ApiKey { get; init; }
string BuildUrl();
}
}

@ -2,7 +2,7 @@
namespace Trash.Config namespace Trash.Config
{ {
public abstract class ServiceConfiguration public abstract class ServiceConfiguration : IServiceConfiguration
{ {
[Required(ErrorMessage = "Property 'base_url' is required")] [Required(ErrorMessage = "Property 'base_url' is required")]
public string BaseUrl { get; init; } = ""; public string BaseUrl { get; init; } = "";

@ -1,5 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Flurl; using Flurl;
using Flurl.Http; using Flurl.Http;
@ -10,11 +9,11 @@ namespace Trash.Radarr.Api
{ {
public class RadarrApi : IRadarrApi public class RadarrApi : IRadarrApi
{ {
private readonly IConfigurationProvider<RadarrConfiguration> _config; private readonly IServiceConfiguration _serviceConfig;
public RadarrApi(IConfigurationProvider<RadarrConfiguration> config) public RadarrApi(IServiceConfiguration serviceConfig)
{ {
_config = config; _serviceConfig = serviceConfig;
} }
public async Task<List<RadarrQualityDefinitionItem>> GetQualityDefinition() public async Task<List<RadarrQualityDefinitionItem>> GetQualityDefinition()
@ -35,12 +34,7 @@ namespace Trash.Radarr.Api
private string BaseUrl() private string BaseUrl()
{ {
if (_config.ActiveConfiguration == null) return _serviceConfig.BuildUrl();
{
throw new InvalidOperationException("No active configuration available for API method");
}
return _config.ActiveConfiguration.BuildUrl();
} }
} }
} }

@ -10,9 +10,9 @@ namespace Trash.Sonarr.Api
{ {
public class SonarrApi : ISonarrApi public class SonarrApi : ISonarrApi
{ {
private readonly IConfigurationProvider<SonarrConfiguration> _config; private readonly IServiceConfiguration _config;
public SonarrApi(IConfigurationProvider<SonarrConfiguration> config) public SonarrApi(IServiceConfiguration config)
{ {
_config = config; _config = config;
} }
@ -80,12 +80,7 @@ namespace Trash.Sonarr.Api
private string BaseUrl() private string BaseUrl()
{ {
if (_config.ActiveConfiguration == null) return _config.BuildUrl();
{
throw new InvalidOperationException("No active configuration available for API method");
}
return _config.ActiveConfiguration.BuildUrl();
} }
} }
} }

Loading…
Cancel
Save