48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
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<GiteaPackageService> logger)
|
|
: IGiteaPackageService
|
|
{
|
|
public async Task<IEnumerable<GiteaPackage>> 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<List<GiteaPackage>>(cancellationToken: cancellationToken)
|
|
?? new List<GiteaPackage>();
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |