-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCensusService.cs
More file actions
96 lines (83 loc) · 4.77 KB
/
Copy pathCensusService.cs
File metadata and controls
96 lines (83 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using LantanaGroup.Link.LinkAdmin.BFF.Infrastructure.Logging;
using LantanaGroup.Link.Shared.Application.Models.Configs;
using Microsoft.Extensions.Options;
using System.Net.Http.Headers;
using System.Security.Claims;
using LantanaGroup.Link.LinkAdmin.BFF.Application.Commands.Security;
using LantanaGroup.Link.LinkAdmin.BFF.Application.Models.Configuration;
using LantanaGroup.Link.LinkAdmin.BFF.Application.Models.Health;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace LantanaGroup.Link.LinkAdmin.BFF.Application.Clients
{
public class CensusService
{
private readonly ILogger<CensusService> _logger;
private readonly HttpClient _client;
private readonly IOptions<ServiceRegistry> _serviceRegistry;
private readonly IOptions<AuthenticationSchemaConfig> _authenticationSchemaConfig;
private readonly IServiceScopeFactory _scopeFactory;
public CensusService(ILogger<CensusService> logger, HttpClient client, IOptions<ServiceRegistry> serviceRegistry, IOptions<AuthenticationSchemaConfig> authenticationSchemaConfig, IServiceScopeFactory scopeFactory)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_client = client ?? throw new ArgumentNullException(nameof(client));
_serviceRegistry = serviceRegistry ?? throw new ArgumentNullException(nameof(serviceRegistry));
_authenticationSchemaConfig = authenticationSchemaConfig ?? throw new ArgumentNullException(nameof(authenticationSchemaConfig));
_scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory));
InitHttpClient();
}
public async Task<HttpResponseMessage> ServiceHealthCheck(CancellationToken cancellationToken)
{
// HTTP GET
HttpResponseMessage response = await _client.GetAsync($"health", cancellationToken);
return response;
}
public async Task<LinkServiceHealthReport> LinkServiceHealthCheck(CancellationToken cancellationToken)
{
// HTTP GET
try
{
var response = await _client.GetAsync($"health", cancellationToken);
var healthResult = await response.Content.ReadFromJsonAsync<LinkServiceHealthReport>(cancellationToken: cancellationToken);
if (healthResult is not null) healthResult.Service = "Census";
return healthResult;
}
catch (Exception ex)
{
_logger.LogError(ex, "Census service health check failed");
return new LinkServiceHealthReport { Service = "Census", Status = HealthStatus.Unhealthy };
}
}
public async Task<HttpResponseMessage> DeleteCensusJobsAsync(ClaimsPrincipal user, string facilityId, CancellationToken cancellationToken)
{
var request = new HttpRequestMessage(HttpMethod.Delete, $"api/census/config/{Uri.EscapeDataString(facilityId)}/jobs");
await SetAuthHeaderAsync(user, request, cancellationToken);
return await _client.SendAsync(request, cancellationToken);
}
public async Task<HttpResponseMessage> RestoreCensusJobsAsync(ClaimsPrincipal user, string facilityId, CancellationToken cancellationToken)
{
var request = new HttpRequestMessage(HttpMethod.Patch, $"api/census/config/{Uri.EscapeDataString(facilityId)}/jobs/restore");
await SetAuthHeaderAsync(user, request, cancellationToken);
return await _client.SendAsync(request, cancellationToken);
}
private async Task SetAuthHeaderAsync(ClaimsPrincipal user, HttpRequestMessage request, CancellationToken cancellationToken)
{
if (_authenticationSchemaConfig.Value.EnableAnonymousAccess) return;
using var scope = _scopeFactory.CreateScope();
var createLinkBearerToken = scope.ServiceProvider.GetRequiredService<ICreateLinkBearerToken>();
var token = await createLinkBearerToken.ExecuteAsync(user, 2, cancellationToken);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
private void InitHttpClient()
{
//check if the service uri is set
if (string.IsNullOrEmpty(_serviceRegistry.Value.CensusServiceUrl))
{
_logger.LogGatewayServiceUriException("Census", "Census service uri is not set");
throw new ArgumentNullException("Census Service URL is missing.");
}
_client.BaseAddress = new Uri(_serviceRegistry.Value.CensusServiceUrl);
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
}
}