| | | 1 | | using Chronicis.Shared.DTOs; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Client.Services; |
| | | 4 | | |
| | | 5 | | public interface IHealthStatusApiService |
| | | 6 | | { |
| | | 7 | | Task<SystemHealthStatusDto?> GetSystemHealthAsync(); |
| | | 8 | | } |
| | | 9 | | |
| | | 10 | | public class HealthStatusApiService : IHealthStatusApiService |
| | | 11 | | { |
| | | 12 | | private readonly HttpClient _httpClient; |
| | | 13 | | private readonly ILogger<HealthStatusApiService> _logger; |
| | | 14 | | |
| | 0 | 15 | | public HealthStatusApiService(HttpClient httpClient, ILogger<HealthStatusApiService> logger) |
| | | 16 | | { |
| | 0 | 17 | | _httpClient = httpClient; |
| | 0 | 18 | | _logger = logger; |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | public async Task<SystemHealthStatusDto?> GetSystemHealthAsync() |
| | | 22 | | { |
| | | 23 | | try |
| | | 24 | | { |
| | 0 | 25 | | _logger.LogInformation("Requesting health status from /health/status"); |
| | 0 | 26 | | var response = await _httpClient.GetAsync("/health/status"); |
| | | 27 | | |
| | 0 | 28 | | _logger.LogInformation("Health status API returned {StatusCode}", response.StatusCode); |
| | | 29 | | |
| | 0 | 30 | | if (response.IsSuccessStatusCode) |
| | | 31 | | { |
| | 0 | 32 | | var content = await response.Content.ReadAsStringAsync(); |
| | 0 | 33 | | _logger.LogDebug("Health status response: {Content}", content); |
| | | 34 | | |
| | 0 | 35 | | return System.Text.Json.JsonSerializer.Deserialize<SystemHealthStatusDto>(content, new System.Text.Json. |
| | 0 | 36 | | { |
| | 0 | 37 | | PropertyNameCaseInsensitive = true |
| | 0 | 38 | | }); |
| | | 39 | | } |
| | | 40 | | else |
| | | 41 | | { |
| | 0 | 42 | | var errorContent = await response.Content.ReadAsStringAsync(); |
| | 0 | 43 | | _logger.LogWarning("Health status API returned {StatusCode}: {ErrorContent}", response.StatusCode, error |
| | 0 | 44 | | return null; |
| | | 45 | | } |
| | | 46 | | } |
| | 0 | 47 | | catch (HttpRequestException httpEx) |
| | | 48 | | { |
| | 0 | 49 | | _logger.LogError(httpEx, "HTTP error while fetching system health status"); |
| | 0 | 50 | | return null; |
| | | 51 | | } |
| | 0 | 52 | | catch (TaskCanceledException tcEx) |
| | | 53 | | { |
| | 0 | 54 | | _logger.LogError(tcEx, "Timeout while fetching system health status"); |
| | 0 | 55 | | return null; |
| | | 56 | | } |
| | 0 | 57 | | catch (Exception ex) |
| | | 58 | | { |
| | 0 | 59 | | _logger.LogError(ex, "Failed to fetch system health status"); |
| | 0 | 60 | | return null; |
| | | 61 | | } |
| | 0 | 62 | | } |
| | | 63 | | } |