namespace sh.actions.package_cleanup.Service; using System.Net.Http.Json; using Microsoft.Extensions.Logging; using sh.actions.package_cleanup.Models; public class GiteaPackageService(HttpClient httpClient, GiteaConfig config, ILogger logger) : IGiteaPackageService { public async Task> GetPackagesByOwnerAsync(CancellationToken cancellationToken = default) { try { var url = $"{config.GiteaUrl.TrimEnd('/')}/api/v1/packages/{config.Owner}"; logger.LogInformation("Fetching packages from Gitea: {Url}", url); var request = new HttpRequestMessage(HttpMethod.Get, url); // Add authentication if token is provided if (!string.IsNullOrEmpty(config.ApiToken)) { request.Headers.Add("Authorization", $"token {config.ApiToken}"); } var response = await httpClient.SendAsync(request, cancellationToken); response.EnsureSuccessStatusCode(); var packages = await response.Content.ReadFromJsonAsync>(cancellationToken: cancellationToken) ?? new List(); logger.LogInformation("Successfully fetched {Count} packages", packages.Count); return packages; } catch (HttpRequestException ex) { logger.LogError(ex, "Error fetching packages from Gitea"); throw; } catch (Exception ex) { logger.LogError(ex, "Unexpected error fetching packages"); throw; } } }