Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fazzani committed Dec 28, 2024
1 parent 760cf6b commit 3c3f424
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
15 changes: 1 addition & 14 deletions src/Proxarr.Api/Configuration/AppConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class AppConfiguration

public string FULL_SCAN_CRON { get; set; } = "0 6 * * 1";

public List<ClientInstance> Clients { get; set; }
public List<ClientConfiguration> Clients { get; set; }

Check warning on line 15 in src/Proxarr.Api/Configuration/AppConfiguration.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Clients' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 15 in src/Proxarr.Api/Configuration/AppConfiguration.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Clients' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

private Dictionary<string, string[]> _watchProviders;

Check warning on line 17 in src/Proxarr.Api/Configuration/AppConfiguration.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_watchProviders' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 17 in src/Proxarr.Api/Configuration/AppConfiguration.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_watchProviders' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Expand Down Expand Up @@ -57,17 +57,4 @@ private Dictionary<string, string[]> InitProviders()
return dict;
}
}

public sealed class ClientInstance
{
/// <summary>
/// The name of the application (Sonarr or Radarr)
/// </summary>
public required string Application { get; set; }
public required string BaseUrl { get; set; }
public required string ApiKey { get; set; }

public bool IsSonarr => Application.Equals("Sonarr", StringComparison.OrdinalIgnoreCase);
public bool IsRadarr => Application.Equals("Radarr", StringComparison.OrdinalIgnoreCase);
}
}
9 changes: 9 additions & 0 deletions src/Proxarr.Api/Configuration/ClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
public sealed class ClientConfiguration
{
public const string SECTION_NAME = $"{AppConfiguration.SECTION_NAME}:Clients";

/// <summary>
/// Must be Sonarr or Radarr
/// </summary>
public required string Application { get; set; }
public required string ApiKey { get; set; }
public required string BaseUrl { get; set; }


public bool IsSonarr => Application.Equals("Sonarr", StringComparison.OrdinalIgnoreCase);
public bool IsRadarr => Application.Equals("Radarr", StringComparison.OrdinalIgnoreCase);
}
}
15 changes: 9 additions & 6 deletions src/Proxarr.Api/Core/ApiKeyDelegatingHandler.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
using Proxarr.Api.Configuration;
using Microsoft.Extensions.Options;
using Proxarr.Api.Configuration;

namespace Proxarr.Api.Core
{
public class ApiKeyDelegatingHandler : DelegatingHandler
{
private const string API_KEY_HEADER_NAME = "X-API-KEY";

private readonly IConfiguration _configuration;
private readonly AppConfiguration _configuration;
private readonly ILogger<ApiKeyDelegatingHandler> _logger;

public ApiKeyDelegatingHandler(IConfiguration configuration)
public ApiKeyDelegatingHandler(IOptions<AppConfiguration> configuration, ILogger<ApiKeyDelegatingHandler> logger)
{
_configuration = configuration;
_configuration = configuration?.Value ?? throw new ArgumentNullException(nameof(configuration));
_logger = logger;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (!request.Headers.Contains(API_KEY_HEADER_NAME) && request.RequestUri != null)
{
var key = $"{request.RequestUri.Scheme}://{request.RequestUri.Host}";
var clients = _configuration.GetSection(ClientConfiguration.SECTION_NAME).Get<List<ClientConfiguration>>();
var clientConfig = clients?.FirstOrDefault(x => x.BaseUrl.Equals(key, StringComparison.OrdinalIgnoreCase));
var clientConfig = _configuration.Clients?.FirstOrDefault(x => x.BaseUrl.Equals(key, StringComparison.OrdinalIgnoreCase));

if (clientConfig != null)
{
_logger.LogInformation("Adding API Key to request for {BaseUrl}", clientConfig.BaseUrl);
request.Headers.Add(API_KEY_HEADER_NAME, clientConfig.ApiKey);
}
}
Expand Down

0 comments on commit 3c3f424

Please sign in to comment.