Skip to content

Commit

Permalink
refactor: change HttpClient registration in DI (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardSmit authored Feb 10, 2024
1 parent b56deba commit 6f04e91
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
18 changes: 11 additions & 7 deletions backend/MangaMagnet.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MangaDexApiService>();
builder.Services.AddSingleton<MangaDexConverterService>();
builder.Services.AddSingleton<IMetadataFetcher, MangaDexMetadataFetcher>();
Expand Down
44 changes: 40 additions & 4 deletions backend/MangaMagnet.Api/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -942,6 +977,7 @@
"type": "Project",
"dependencies": {
"Microsoft.EntityFrameworkCore": "[8.0.1, )",
"Microsoft.Extensions.Http": "[8.0.0, )",
"System.Threading.RateLimiting": "[8.0.0, )"
}
}
Expand Down
1 change: 1 addition & 0 deletions backend/MangaMagnet.Core/MangaMagnet.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="System.Threading.RateLimiting" Version="8.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace MangaMagnet.Core.Providers.MangaDex;

public class MangaDexApiService(HttpClient httpClient, ILogger<MangaDexApiService> logger) : SimpleRatelimitedProvider
public class MangaDexApiService(IHttpClientFactory httpClientFactory, ILogger<MangaDexApiService> logger) : SimpleRatelimitedProvider
{
public async Task<List<MangaDexMangaData>> SearchMangaByNameAsync(string input, CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -89,15 +89,19 @@ public async Task<List<MangaDexChapter>> FetchMangaChapters(string mangaDexId, C
return chapters;
}

private Task<T> SendAndParseGetRequestAsync<T>(string requestUri, CancellationToken cancellationToken = default)
=> SendAndParseRequestAsync<T>(new HttpRequestMessage(HttpMethod.Get, requestUri), cancellationToken);
private async Task<T> SendAndParseGetRequestAsync<T>(string requestUri, CancellationToken cancellationToken = default)
{
using var request = new HttpRequestMessage(HttpMethod.Get, requestUri);

return await SendAndParseRequestAsync<T>(request, cancellationToken);
}

private async Task<T> SendAndParseRequestAsync<T>(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<T>(cancellationToken: cancellationToken) ??
throw new Exception("Failed to deserialize response");
Expand All @@ -110,6 +114,7 @@ private async Task<HttpResponseMessage> SendRequestAsync(HttpRequestMessage requ

await RateLimiter.AcquireAsync(cancellationToken: cancellationToken);

using var httpClient = httpClientFactory.CreateClient("MangaDex");
var response = await httpClient.SendAsync(request, cancellationToken);

if (!response.IsSuccessStatusCode)
Expand Down

0 comments on commit 6f04e91

Please sign in to comment.