|
| 1 | +using LantanaGroup.Link.LinkAdmin.BFF.Application.Models.Health; |
| 2 | +using LantanaGroup.Link.LinkAdmin.BFF.Infrastructure.Logging; |
| 3 | +using LantanaGroup.Link.Shared.Application.Models.Configs; |
| 4 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 5 | +using Microsoft.Extensions.Options; |
| 6 | +using System.Net.Http.Headers; |
| 7 | + |
| 8 | +namespace LantanaGroup.Link.LinkAdmin.BFF.Application.Clients |
| 9 | +{ |
| 10 | + public class TerminologyService |
| 11 | + { |
| 12 | + private readonly ILogger<TerminologyService> _logger; |
| 13 | + private readonly HttpClient _client; |
| 14 | + private readonly IOptions<ServiceRegistry> _serviceRegistry; |
| 15 | + |
| 16 | + public TerminologyService(ILogger<TerminologyService> logger, HttpClient client, IOptions<ServiceRegistry> serviceRegistry) |
| 17 | + { |
| 18 | + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 19 | + _client = client ?? throw new ArgumentNullException(nameof(client)); |
| 20 | + _serviceRegistry = serviceRegistry ?? throw new ArgumentNullException(nameof(serviceRegistry)); |
| 21 | + |
| 22 | + InitHttpClient(); |
| 23 | + } |
| 24 | + |
| 25 | + public async Task<HttpResponseMessage> ServiceHealthCheck(CancellationToken cancellationToken) |
| 26 | + { |
| 27 | + // HTTP GET |
| 28 | + HttpResponseMessage response = await _client.GetAsync($"health", cancellationToken); |
| 29 | + |
| 30 | + return response; |
| 31 | + } |
| 32 | + |
| 33 | + public async Task<LinkServiceHealthReport> LinkServiceHealthCheck(CancellationToken cancellationToken) |
| 34 | + { |
| 35 | + // HTTP GET |
| 36 | + try |
| 37 | + { |
| 38 | + var response = await _client.GetAsync($"health", cancellationToken); |
| 39 | + var healthResult = await response.Content.ReadFromJsonAsync<LinkServiceHealthReport>(cancellationToken: cancellationToken); |
| 40 | + |
| 41 | + if (healthResult is null) |
| 42 | + { |
| 43 | + _logger.LogWarning("Terminology service health check returned null or invalid response"); |
| 44 | + return new LinkServiceHealthReport() { Service = "Terminology", Status = HealthStatus.Unhealthy }; |
| 45 | + } |
| 46 | + |
| 47 | + healthResult.Service = "Terminology"; |
| 48 | + return healthResult; |
| 49 | + } |
| 50 | + catch (Exception ex) |
| 51 | + { |
| 52 | + _logger.LogError(ex, "Terminology service health check failed"); |
| 53 | + return new LinkServiceHealthReport { Service = "Terminology", Status = HealthStatus.Unhealthy }; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private void InitHttpClient() |
| 58 | + { |
| 59 | + //check if the service uri is set |
| 60 | + if (string.IsNullOrEmpty(_serviceRegistry.Value.TerminologyServiceUrl)) |
| 61 | + { |
| 62 | + _logger.LogGatewayServiceUriException("Terminology", "Terminology service uri is not set"); |
| 63 | + throw new ArgumentNullException("Terminology Service URL is missing."); |
| 64 | + } |
| 65 | + |
| 66 | + _client.BaseAddress = new Uri(_serviceRegistry.Value.TerminologyServiceUrl); |
| 67 | + _client.DefaultRequestHeaders.Accept.Clear(); |
| 68 | + _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments