- 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.
42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using ForgejoReleaseSync.DTOs;
|
|
using ForgejoReleaseSync.DTOs.Forgejo;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace ForgejoReleaseSync.Services;
|
|
|
|
public class GithubService(IHttpClientFactory httpClientFactory, ILogger<GithubService> logger)
|
|
{
|
|
public async Task<List<GithubReleaseResponseDto>> FetchRepoReleasesAsync(
|
|
string owner, string repo, CancellationToken token = default)
|
|
{
|
|
try
|
|
{
|
|
var httpClient = httpClientFactory.CreateClient("github");
|
|
|
|
using var response = await httpClient.GetAsync(
|
|
$"repos/{owner}/{repo}/releases?per_page=3&page=1", token);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(token);
|
|
var releases = await JsonSerializer.DeserializeAsync(
|
|
stream, AppJsonSerializerContext.Default.ListGithubReleaseResponseDto, token);
|
|
|
|
return releases ?? [];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Failed to fetch GitHub releases for {Owner}/{Repo}", owner, repo);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
[JsonSerializable(typeof(List<GithubReleaseResponseDto>))]
|
|
[JsonSerializable(typeof(ForgejoCreateReleaseDto))]
|
|
[JsonSerializable(typeof(ForgejoReleaseResponseDto))]
|
|
[JsonSerializable(typeof(List<ForgejoReleaseResponseDto>))]
|
|
internal partial class AppJsonSerializerContext : JsonSerializerContext
|
|
{
|
|
} |