Add Proxy Support, Add IOptions Pattern, Add Extensions Method
Currently Changes Http / Https proxy support has been added , To disguise the Ip address or if a proxy service is required IOptions pattern has been implemented. Better options handling Extensions methods have been implemented to make Program.cs smaller Added a global logger for static and extension methods appsettings.json now contains "default" data for the applications and proxy settings. The Docker variables are also specified above it. This also fixes the bug that you have to set all variables, although you only want to use Sonarr, for example
This commit is contained in:
19
UmlautAdaptarr/Utilities/GlobalStaticLogger.cs
Normal file
19
UmlautAdaptarr/Utilities/GlobalStaticLogger.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace UmlautAdaptarr.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Service for providing a static logger to log errors and information.
|
||||
/// The GlobalStaticLogger is designed to provide a static logger that can be used to log errors and information.
|
||||
/// It facilitates logging for both static classes and extension methods.
|
||||
/// </summary>
|
||||
public static class GlobalStaticLogger
|
||||
{
|
||||
|
||||
public static ILogger Logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the GlobalStaticLogger with the provided logger factory.
|
||||
/// </summary>
|
||||
/// <param name="loggerFactory">The ILoggerFactory instance used to create loggers.</param>
|
||||
public static void Initialize(ILoggerFactory loggerFactory) => Logger = loggerFactory.CreateLogger("GlobalStaticLogger");
|
||||
}
|
||||
}
|
||||
53
UmlautAdaptarr/Utilities/ProxyExtension.cs
Normal file
53
UmlautAdaptarr/Utilities/ProxyExtension.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using UmlautAdaptarr.Options;
|
||||
|
||||
namespace UmlautAdaptarr.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for configuring proxies.
|
||||
/// </summary>
|
||||
public static class ProxyExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Logger instance for logging proxy configurations.
|
||||
/// </summary>
|
||||
private static ILogger Logger = GlobalStaticLogger.Logger;
|
||||
|
||||
/// <summary>
|
||||
/// Configures the proxy settings for the provided HttpClientHandler instance.
|
||||
/// </summary>
|
||||
/// <param name="handler">The HttpClientHandler instance to configure.</param>
|
||||
/// <param name="proxyOptions">ProxyOptions options to be used for configuration.</param>
|
||||
/// <returns>The configured HttpClientHandler instance.</returns>
|
||||
public static HttpClientHandler ConfigureProxy(this HttpClientHandler handler, ProxyOptions? proxyOptions)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (proxyOptions != null && proxyOptions.Enabled)
|
||||
{
|
||||
Logger.LogInformation("Use Proxy {0}", proxyOptions.Address);
|
||||
handler.UseProxy = true;
|
||||
handler.Proxy = new WebProxy(proxyOptions.Address, proxyOptions.BypassOnLocal);
|
||||
|
||||
if (!string.IsNullOrEmpty(proxyOptions.Username) && !string.IsNullOrEmpty(proxyOptions.Password))
|
||||
{
|
||||
Logger.LogInformation("Use Proxy Credentials from User {0}", proxyOptions.Username);
|
||||
handler.DefaultProxyCredentials =
|
||||
new NetworkCredential(proxyOptions.Username, proxyOptions.Password);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.LogDebug("No proxy was set");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "Error occurred while configuring proxy, no Proxy will be used!");
|
||||
}
|
||||
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
}
|
||||
96
UmlautAdaptarr/Utilities/ServicesExtensions.cs
Normal file
96
UmlautAdaptarr/Utilities/ServicesExtensions.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using UmlautAdaptarr.Options;
|
||||
using UmlautAdaptarr.Options.ArrOptions;
|
||||
using UmlautAdaptarr.Providers;
|
||||
using UmlautAdaptarr.Services;
|
||||
|
||||
namespace UmlautAdaptarr.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for configuring services related to ARR Applications
|
||||
/// </summary>
|
||||
public static class ServicesExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Adds a service with specified options and service to the service collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="TOptions">The options type for the service.</typeparam>
|
||||
/// <typeparam name="TService">The service type for the service.</typeparam>
|
||||
/// <param name="builder">The <see cref="WebApplicationBuilder"/> to configure the service collection.</param>
|
||||
/// <param name="sectionName">The name of the configuration section containing service options.</param>
|
||||
/// <returns>The configured <see cref="WebApplicationBuilder"/>.</returns>
|
||||
private static WebApplicationBuilder AddServiceWithOptions<TOptions, TService>(this WebApplicationBuilder builder, string sectionName)
|
||||
where TOptions : class
|
||||
where TService : class
|
||||
{
|
||||
if (builder.Services == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(builder), "Service collection is null.");
|
||||
}
|
||||
|
||||
var options = builder.Configuration.GetSection(sectionName).Get<TOptions>();
|
||||
if (options == null)
|
||||
{
|
||||
throw new InvalidOperationException($"{typeof(TService).Name} options could not be loaded from Configuration or ENV Variable.");
|
||||
}
|
||||
|
||||
builder.Services.Configure<TOptions>(builder.Configuration.GetSection(sectionName));
|
||||
builder.Services.AddSingleton<TService>();
|
||||
return builder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds support for Sonarr with default options and client.
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="WebApplicationBuilder"/> to configure the service collection.</param>
|
||||
/// <returns>The configured <see cref="WebApplicationBuilder"/>.</returns>
|
||||
public static WebApplicationBuilder AddSonarrSupport(this WebApplicationBuilder builder)
|
||||
{
|
||||
return builder.AddServiceWithOptions<SonarrInstanceOptions, SonarrClient>("Sonarr");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds support for Lidarr with default options and client.
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="WebApplicationBuilder"/> to configure the service collection.</param>
|
||||
/// <returns>The configured <see cref="WebApplicationBuilder"/>.</returns>
|
||||
public static WebApplicationBuilder AddLidarrSupport(this WebApplicationBuilder builder)
|
||||
{
|
||||
return builder.AddServiceWithOptions<LidarrInstanceOptions, LidarrClient>("Lidarr");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds support for Readarr with default options and client.
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="WebApplicationBuilder"/> to configure the service collection.</param>
|
||||
/// <returns>The configured <see cref="WebApplicationBuilder"/>.</returns>
|
||||
public static WebApplicationBuilder AddReadarrSupport(this WebApplicationBuilder builder)
|
||||
{
|
||||
return builder.AddServiceWithOptions<ReadarrInstanceOptions, ReadarrClient>("Readarr");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a title lookup service to the service collection.
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="WebApplicationBuilder"/> to configure the service collection.</param>
|
||||
/// <returns>The configured <see cref="WebApplicationBuilder"/>.</returns>
|
||||
public static WebApplicationBuilder AddTitleLookupService(this WebApplicationBuilder builder)
|
||||
{
|
||||
return builder.AddServiceWithOptions<GlobalOptions, TitleApiService>("Settings");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a proxy service to the service collection.
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="WebApplicationBuilder"/> to configure the service collection.</param>
|
||||
/// <returns>The configured <see cref="WebApplicationBuilder"/>.</returns>
|
||||
public static WebApplicationBuilder AddProxyService(this WebApplicationBuilder builder)
|
||||
{
|
||||
return builder.AddServiceWithOptions<GlobalOptions, ProxyService>("Settings");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user