simplified how the download client selection works (#22)

This commit is contained in:
Marius Nechifor
2024-12-15 18:37:32 +02:00
parent 74c49f041d
commit 0a6ec21c95
14 changed files with 41 additions and 85 deletions
@@ -21,6 +21,7 @@ public sealed class DelugeService : IDownloadService
)
{
_logger = logger;
config.Value.Validate();
_client = new (config, httpClientFactory);
_filenameEvaluator = filenameEvaluator;
}
@@ -3,6 +3,7 @@ using Common.Configuration.DownloadClient;
using Infrastructure.Verticals.DownloadClient.Deluge;
using Infrastructure.Verticals.DownloadClient.QBittorrent;
using Infrastructure.Verticals.DownloadClient.Transmission;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
@@ -10,57 +11,25 @@ namespace Infrastructure.Verticals.DownloadClient;
public sealed class DownloadServiceFactory
{
private readonly QBitConfig _qBitConfig;
private readonly DelugeConfig _delugeConfig;
private readonly TransmissionConfig _transmissionConfig;
private readonly IServiceProvider _serviceProvider;
private readonly Domain.Enums.DownloadClient _downloadClient;
public DownloadServiceFactory(
IOptions<QBitConfig> qBitConfig,
IOptions<DelugeConfig> delugeConfig,
IOptions<TransmissionConfig> transmissionConfig,
IServiceProvider serviceProvider)
public DownloadServiceFactory(IServiceProvider serviceProvider, IConfiguration configuration)
{
_qBitConfig = qBitConfig.Value;
_delugeConfig = delugeConfig.Value;
_transmissionConfig = transmissionConfig.Value;
_serviceProvider = serviceProvider;
_qBitConfig.Validate();
_delugeConfig.Validate();
_transmissionConfig.Validate();
int enabledCount = new[] { _qBitConfig.Enabled, _delugeConfig.Enabled, _transmissionConfig.Enabled }
.Count(enabled => enabled);
if (enabledCount > 1)
{
throw new Exception("only one download client can be enabled");
}
if (enabledCount == 0)
{
throw new Exception("no download client is enabled");
}
_downloadClient = (Domain.Enums.DownloadClient)Enum.Parse(
typeof(Domain.Enums.DownloadClient),
configuration[EnvironmentVariables.DownloadClient] ?? Domain.Enums.DownloadClient.QBittorrent.ToString(),
true
);
}
public IDownloadService CreateDownloadClient()
{
if (_qBitConfig.Enabled)
public IDownloadService CreateDownloadClient() =>
_downloadClient switch
{
return _serviceProvider.GetRequiredService<QBitService>();
}
if (_delugeConfig.Enabled)
{
return _serviceProvider.GetRequiredService<DelugeService>();
}
if (_transmissionConfig.Enabled)
{
return _serviceProvider.GetRequiredService<TransmissionService>();
}
throw new NotSupportedException();
}
Domain.Enums.DownloadClient.QBittorrent => _serviceProvider.GetRequiredService<QBitService>(),
Domain.Enums.DownloadClient.Deluge => _serviceProvider.GetRequiredService<DelugeService>(),
Domain.Enums.DownloadClient.Transmission => _serviceProvider.GetRequiredService<TransmissionService>(),
_ => throw new ArgumentOutOfRangeException()
};
}
@@ -22,6 +22,7 @@ public sealed class QBitService : IDownloadService
{
_logger = logger;
_config = config.Value;
_config.Validate();
_client = new(_config.Url);
_filenameEvaluator = filenameEvaluator;
}
@@ -25,6 +25,7 @@ public sealed class TransmissionService : IDownloadService
{
_logger = logger;
_config = config.Value;
_config.Validate();
_client = new(
new Uri(_config.Url, "/transmission/rpc").ToString(),
login: _config.Username,