feat: utilize IHttpClientFactory for improved HTTP client management
- Replaced direct `HttpClient` dependencies with `IHttpClientFactory` in `GithubService`, `ForgejoService`, and `ReleaseAssetDownloadService`. - Added DI-configured HTTP client registrations for GitHub, Forgejo, and asset downloads in `Program.cs`. - Consolidated HTTP client setup to enhance maintainability, configurability, and resource lifecycle management.
This commit is contained in:
1 parent
3b644de2d5
commit
8db845f27a
4 files changed
+35
-19
No files matched your search
+12
-7
@@ -8,27 +8,32 @@ var builder = Host.CreateApplicationBuilder(args);
|
||||
var config = AppConfigLoader.LoadConfigFromEnv();
|
||||
builder.Services.AddSingleton(config);
|
||||
|
||||
var repositoryConfig = await RepoConfigLoader.LoadConfigAsync(
|
||||
config.RepositoryConfigFilePath);
|
||||
var repositoryConfig = await RepoConfigLoader.LoadConfigAsync(config.RepositoryConfigFilePath);
|
||||
builder.Services.AddSingleton(repositoryConfig);
|
||||
|
||||
builder.Services.AddHttpClient<GithubService>(client =>
|
||||
builder.Services.AddHttpClient("github", client =>
|
||||
{
|
||||
client.BaseAddress = new Uri("https://api.github.com/");
|
||||
client.DefaultRequestHeaders.UserAgent.ParseAdd("ForgejoReleaseSync");
|
||||
client.DefaultRequestHeaders.Accept.ParseAdd("application/vnd.github+json");
|
||||
});
|
||||
|
||||
builder.Services.AddHttpClient<ForgejoService>(client =>
|
||||
builder.Services.AddHttpClient("forgejo", client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(config.ForgejoInstacne.TrimEnd('/') + "/");
|
||||
client.DefaultRequestHeaders.UserAgent.ParseAdd("ForgejoReleaseSync");
|
||||
client.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("token", config.ForgejoToken);
|
||||
});
|
||||
|
||||
builder.Services.AddHttpClient("asset-download", client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(config.ForgejoInstacne);
|
||||
client.DefaultRequestHeaders.UserAgent.ParseAdd("ForgejoReleaseSync");
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("token", config.ForgejoToken);
|
||||
});
|
||||
|
||||
builder.Services.AddSingleton<ReleaseAssetDownloadService>();
|
||||
builder.Services.AddSingleton<GithubService>();
|
||||
builder.Services.AddSingleton<ForgejoService>();
|
||||
builder.Services.AddSingleton<ReleaseAssetDownloadService>();
|
||||
builder.Services.AddSingleton<SyncService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
Reference in new issue
Block a user