| | | 1 | | using Chronicis.Shared.DTOs; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Api.Services; |
| | | 4 | | |
| | | 5 | | public class Open5eHealthCheckService : HealthCheckServiceBase |
| | | 6 | | { |
| | | 7 | | private readonly HttpClient _httpClient; |
| | | 8 | | private readonly IConfiguration _configuration; |
| | | 9 | | |
| | | 10 | | public Open5eHealthCheckService( |
| | | 11 | | HttpClient httpClient, |
| | | 12 | | IConfiguration configuration, |
| | | 13 | | ILogger<Open5eHealthCheckService> logger) |
| | 0 | 14 | | : base(logger) |
| | | 15 | | { |
| | 0 | 16 | | _httpClient = httpClient; |
| | 0 | 17 | | _configuration = configuration; |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | protected override async Task<(string Status, string? Message)> PerformHealthCheckAsync() |
| | | 21 | | { |
| | 0 | 22 | | var baseUrl = _configuration["ExternalLinks:Open5e:BaseUrl"] ?? "https://api.open5e.com"; |
| | | 23 | | |
| | | 24 | | try |
| | | 25 | | { |
| | | 26 | | // Try a lightweight endpoint |
| | 0 | 27 | | var testUrl = $"{baseUrl}/v2/monsters/?limit=1"; |
| | 0 | 28 | | var response = await _httpClient.GetAsync(testUrl); |
| | | 29 | | |
| | 0 | 30 | | if (response.IsSuccessStatusCode) |
| | | 31 | | { |
| | 0 | 32 | | return (HealthStatus.Healthy, "Open5e API accessible"); |
| | | 33 | | } |
| | | 34 | | else |
| | | 35 | | { |
| | 0 | 36 | | return (HealthStatus.Degraded, $"Open5e API returned {response.StatusCode}"); |
| | | 37 | | } |
| | | 38 | | } |
| | 0 | 39 | | catch (Exception ex) |
| | | 40 | | { |
| | 0 | 41 | | return (HealthStatus.Unhealthy, $"Open5e API error: {ex.Message}"); |
| | | 42 | | } |
| | 0 | 43 | | } |
| | | 44 | | } |