From 6f04e913e69a55a179d2ed4b7ef22e47a369156e Mon Sep 17 00:00:00 2001 From: Gerard Smit Date: Sat, 10 Feb 2024 15:12:37 +0100 Subject: [PATCH] refactor: change HttpClient registration in DI (#4) --- backend/MangaMagnet.Api/Program.cs | 18 +++++--- backend/MangaMagnet.Api/packages.lock.json | 44 +++++++++++++++++-- .../MangaMagnet.Core/MangaMagnet.Core.csproj | 1 + .../Providers/MangaDex/MangaDexApiService.cs | 13 ++++-- 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/backend/MangaMagnet.Api/Program.cs b/backend/MangaMagnet.Api/Program.cs index d741deb..3bec40a 100644 --- a/backend/MangaMagnet.Api/Program.cs +++ b/backend/MangaMagnet.Api/Program.cs @@ -50,15 +50,19 @@ theme: SystemConsoleTheme.Colored); }); -var handler = new HttpClientHandler(); -if (handler.SupportsAutomaticDecompression) -{ - handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.Brotli; -} +builder.Services.AddHttpClient("MangaDex") + .ConfigurePrimaryHttpMessageHandler(() => + { + var handler = new HttpClientHandler(); + + if (handler.SupportsAutomaticDecompression) + { + handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.Brotli; + } -var httpClient = new HttpClient(handler); + return handler; + }); -builder.Services.AddSingleton(httpClient); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/backend/MangaMagnet.Api/packages.lock.json b/backend/MangaMagnet.Api/packages.lock.json index 0135145..1988c2c 100644 --- a/backend/MangaMagnet.Api/packages.lock.json +++ b/backend/MangaMagnet.Api/packages.lock.json @@ -353,11 +353,11 @@ }, "Microsoft.Extensions.Configuration": { "type": "Transitive", - "resolved": "7.0.0", - "contentHash": "tldQUBWt/xeH2K7/hMPPo5g8zuLc3Ro9I5d4o/XrxvxOCA2EZBtW7bCHHTc49fcBtvB8tLAb/Qsmfrq+2SJ4vA==", + "resolved": "8.0.0", + "contentHash": "0J/9YNXTMWSZP2p2+nvl8p71zpSwokZXZuJW+VjdErkegAnFdO1XlqtA62SJtgVYHdKu3uPxJHcMR/r35HwFBA==", "dependencies": { - "Microsoft.Extensions.Configuration.Abstractions": "7.0.0", - "Microsoft.Extensions.Primitives": "7.0.0" + "Microsoft.Extensions.Configuration.Abstractions": "8.0.0", + "Microsoft.Extensions.Primitives": "8.0.0" } }, "Microsoft.Extensions.Configuration.Abstractions": { @@ -422,6 +422,16 @@ "System.Text.Json": "8.0.0" } }, + "Microsoft.Extensions.Diagnostics": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "3PZp/YSkIXrF7QK7PfC1bkyRYwqOHpWFad8Qx+4wkuumAeXo1NHaxpS9LboNA9OvNSAu+QOVlXbMyoY+pHSqcw==", + "dependencies": { + "Microsoft.Extensions.Configuration": "8.0.0", + "Microsoft.Extensions.Diagnostics.Abstractions": "8.0.0", + "Microsoft.Extensions.Options.ConfigurationExtensions": "8.0.0" + } + }, "Microsoft.Extensions.Diagnostics.Abstractions": { "type": "Transitive", "resolved": "8.0.0", @@ -483,6 +493,19 @@ "Microsoft.Extensions.Logging.Abstractions": "8.0.0" } }, + "Microsoft.Extensions.Http": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "cWz4caHwvx0emoYe7NkHPxII/KkTI8R/LC9qdqJqnKv2poTJ4e2qqPGQqvRoQ5kaSA4FU5IV3qFAuLuOhoqULQ==", + "dependencies": { + "Microsoft.Extensions.Configuration.Abstractions": "8.0.0", + "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0", + "Microsoft.Extensions.Diagnostics": "8.0.0", + "Microsoft.Extensions.Logging": "8.0.0", + "Microsoft.Extensions.Logging.Abstractions": "8.0.0", + "Microsoft.Extensions.Options": "8.0.0" + } + }, "Microsoft.Extensions.Logging": { "type": "Transitive", "resolved": "8.0.0", @@ -515,6 +538,18 @@ "Microsoft.Extensions.Primitives": "8.0.0" } }, + "Microsoft.Extensions.Options.ConfigurationExtensions": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "0f4DMRqEd50zQh+UyJc+/HiBsZ3vhAQALgdkcQEalSH1L2isdC7Yj54M3cyo5e+BeO5fcBQ7Dxly8XiBBcvRgw==", + "dependencies": { + "Microsoft.Extensions.Configuration.Abstractions": "8.0.0", + "Microsoft.Extensions.Configuration.Binder": "8.0.0", + "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0", + "Microsoft.Extensions.Options": "8.0.0", + "Microsoft.Extensions.Primitives": "8.0.0" + } + }, "Microsoft.Extensions.Primitives": { "type": "Transitive", "resolved": "8.0.0", @@ -942,6 +977,7 @@ "type": "Project", "dependencies": { "Microsoft.EntityFrameworkCore": "[8.0.1, )", + "Microsoft.Extensions.Http": "[8.0.0, )", "System.Threading.RateLimiting": "[8.0.0, )" } } diff --git a/backend/MangaMagnet.Core/MangaMagnet.Core.csproj b/backend/MangaMagnet.Core/MangaMagnet.Core.csproj index f7c7a5e..178b7c3 100644 --- a/backend/MangaMagnet.Core/MangaMagnet.Core.csproj +++ b/backend/MangaMagnet.Core/MangaMagnet.Core.csproj @@ -12,6 +12,7 @@ + diff --git a/backend/MangaMagnet.Core/Providers/MangaDex/MangaDexApiService.cs b/backend/MangaMagnet.Core/Providers/MangaDex/MangaDexApiService.cs index 38e72a2..58ad142 100644 --- a/backend/MangaMagnet.Core/Providers/MangaDex/MangaDexApiService.cs +++ b/backend/MangaMagnet.Core/Providers/MangaDex/MangaDexApiService.cs @@ -9,7 +9,7 @@ namespace MangaMagnet.Core.Providers.MangaDex; -public class MangaDexApiService(HttpClient httpClient, ILogger logger) : SimpleRatelimitedProvider +public class MangaDexApiService(IHttpClientFactory httpClientFactory, ILogger logger) : SimpleRatelimitedProvider { public async Task> SearchMangaByNameAsync(string input, CancellationToken cancellationToken = default) { @@ -89,15 +89,19 @@ public async Task> FetchMangaChapters(string mangaDexId, C return chapters; } - private Task SendAndParseGetRequestAsync(string requestUri, CancellationToken cancellationToken = default) - => SendAndParseRequestAsync(new HttpRequestMessage(HttpMethod.Get, requestUri), cancellationToken); + private async Task SendAndParseGetRequestAsync(string requestUri, CancellationToken cancellationToken = default) + { + using var request = new HttpRequestMessage(HttpMethod.Get, requestUri); + + return await SendAndParseRequestAsync(request, cancellationToken); + } private async Task SendAndParseRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken = default) { logger.LogDebug("Request Uri: {Request}", request.RequestUri); - var responseMessage = await SendRequestAsync(request, cancellationToken); + using var responseMessage = await SendRequestAsync(request, cancellationToken); return await responseMessage.Content.ReadFromJsonAsync(cancellationToken: cancellationToken) ?? throw new Exception("Failed to deserialize response"); @@ -110,6 +114,7 @@ private async Task SendRequestAsync(HttpRequestMessage requ await RateLimiter.AcquireAsync(cancellationToken: cancellationToken); + using var httpClient = httpClientFactory.CreateClient("MangaDex"); var response = await httpClient.SendAsync(request, cancellationToken); if (!response.IsSuccessStatusCode)