From 01eea26312767e6c22b5a12295bdfbe5e581d5ee Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Tue, 9 Apr 2024 08:42:44 -0500 Subject: [PATCH] wip --- Directory.Packages.props | 2 + .../ServiceInformation.cs | 5 +- .../ApiServicesAutofacModule.cs | 4 +- .../Recyclarr.ServarrApi.csproj | 2 + .../System/ISystemApiService.cs | 61 ++++++++++++++++++- .../System/SystemApiService.cs | 14 ----- 6 files changed, 70 insertions(+), 18 deletions(-) delete mode 100644 src/Recyclarr.ServarrApi/System/SystemApiService.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 0a9a239e..8b9d0acc 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -14,8 +14,10 @@ + + diff --git a/src/Recyclarr.Compatibility/ServiceInformation.cs b/src/Recyclarr.Compatibility/ServiceInformation.cs index 521d4068..08306dff 100644 --- a/src/Recyclarr.Compatibility/ServiceInformation.cs +++ b/src/Recyclarr.Compatibility/ServiceInformation.cs @@ -5,13 +5,14 @@ using Serilog; namespace Recyclarr.Compatibility; -public class ServiceInformation(ISystemApiService api, ILogger log) : IServiceInformation +public class ServiceInformation(ServarrApiServiceFactory apiFactory, ILogger log) : IServiceInformation { public async Task GetVersion(IServiceConfiguration config) { try { - var status = await api.GetStatus(config); + var api = apiFactory.Create(config); + var status = await api.GetStatus(); log.Debug("{Service} Version: {Version}", status.AppName, status.Version); return new Version(status.Version); } diff --git a/src/Recyclarr.ServarrApi/ApiServicesAutofacModule.cs b/src/Recyclarr.ServarrApi/ApiServicesAutofacModule.cs index 8a669c94..751bb717 100644 --- a/src/Recyclarr.ServarrApi/ApiServicesAutofacModule.cs +++ b/src/Recyclarr.ServarrApi/ApiServicesAutofacModule.cs @@ -19,7 +19,9 @@ public class ApiServicesAutofacModule : Module .As() .SingleInstance(); - builder.RegisterType().As(); + // Single instance because it holds a cache of HttpClient objects + builder.RegisterType().SingleInstance(); + builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); diff --git a/src/Recyclarr.ServarrApi/Recyclarr.ServarrApi.csproj b/src/Recyclarr.ServarrApi/Recyclarr.ServarrApi.csproj index 00af1d7d..150f7dd1 100644 --- a/src/Recyclarr.ServarrApi/Recyclarr.ServarrApi.csproj +++ b/src/Recyclarr.ServarrApi/Recyclarr.ServarrApi.csproj @@ -2,6 +2,8 @@ + + diff --git a/src/Recyclarr.ServarrApi/System/ISystemApiService.cs b/src/Recyclarr.ServarrApi/System/ISystemApiService.cs index c719f0c7..ae361511 100644 --- a/src/Recyclarr.ServarrApi/System/ISystemApiService.cs +++ b/src/Recyclarr.ServarrApi/System/ISystemApiService.cs @@ -1,8 +1,67 @@ +using System.Diagnostics.CodeAnalysis; +using Flurl; using Recyclarr.Config.Models; +using Recyclarr.Json; +using Refit; namespace Recyclarr.ServarrApi.System; public interface ISystemApiService { - Task GetStatus(IServiceConfiguration config); + [Get("/system/status")] + Task GetStatus(); +} + +public class ServarrApiServiceFactory(IHttpClientFactory clientFactory) +{ + // private readonly SocketsHttpHandler _clientHandler = new(); + // private readonly Dictionary _clientCache = new(); + // + // private HttpClient MakeHttpClient(IServiceConfiguration config) + // { + // // ReSharper disable once InvertIf + // if (!_clientCache.TryGetValue(config.InstanceName, out var client)) + // { + // client = new HttpClient(new ServarrApiAuthHandler(config, _clientHandler)) + // { + // BaseAddress = config.BaseUrl.AppendPathSegments("api", "v3").ToUri() + // }; + // + // _clientCache.Add(config.InstanceName, client); + // } + // + // return client; + // } + + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public TService Create(IServiceConfiguration config) + { + var client = clientFactory.CreateClient(); + client.BaseAddress = config.BaseUrl.AppendPathSegments("api", "v3").ToUri(); + + // var client = new HttpClient(new ServarrApiAuthHandler(config, new HttpClientHandler())) + // { + // BaseAddress = config.BaseUrl.AppendPathSegments("api", "v3").ToUri() + // }; + + var settings = new RefitSettings + { + ContentSerializer = new SystemTextJsonContentSerializer(GlobalJsonSerializerSettings.Services) + }; + + return RestService.For(client, settings); + } +} + +[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] +public class ServarrApiAuthHandler(IServiceConfiguration config, HttpMessageHandler innerHandler) + : DelegatingHandler(innerHandler) +{ + protected override async Task SendAsync( + HttpRequestMessage request, + CancellationToken cancellationToken) + { + request.Headers.Add("X-Api-Key", config.ApiKey); + return await base.SendAsync(request, cancellationToken); + } } diff --git a/src/Recyclarr.ServarrApi/System/SystemApiService.cs b/src/Recyclarr.ServarrApi/System/SystemApiService.cs deleted file mode 100644 index ad61cf33..00000000 --- a/src/Recyclarr.ServarrApi/System/SystemApiService.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Flurl.Http; -using Recyclarr.Config.Models; -using Recyclarr.ServarrApi.Http.Servarr; - -namespace Recyclarr.ServarrApi.System; - -public class SystemApiService(IServarrRequestBuilder service) : ISystemApiService -{ - public async Task GetStatus(IServiceConfiguration config) - { - return await service.Request(config, "system", "status") - .GetJsonAsync(); - } -}