call api key in repo genaric

This commit is contained in:
kittapath 2024-08-16 10:39:27 +07:00
parent 451d5d28bc
commit 169b3428e7
3 changed files with 40 additions and 24 deletions

View file

@ -10777,8 +10777,9 @@ namespace BMA.EHR.Application.Repositories.Commands
});
var apiPath = $"{_configuration["API"]}/org/dotnet/user-oc/{UserId}";
var apiKey = _configuration["API_KEY"];
var apiResult = GetExternalAPIAsync(apiPath, token ?? "");
var apiResult = GetExternalAPIAsync(apiPath, token ?? "", apiKey);
if (apiResult.Result != null)
{

View file

@ -20,7 +20,6 @@ namespace BMA.EHR.Application.Repositories
private readonly IApplicationDBContext _dbContext;
private readonly DbSet<T> _dbSet;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IConfiguration _configuration;
#endregion
@ -53,14 +52,14 @@ namespace BMA.EHR.Application.Repositories
#region " For Call External API "
protected async Task<string> GetExternalAPIAsync(string apiPath, string accessToken)
protected async Task<string> GetExternalAPIAsync(string apiPath, string accessToken, string apiKey)
{
try
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]);
client.DefaultRequestHeaders.Add("api_key", apiKey);
var _res = await client.GetAsync(apiPath);
if (_res.IsSuccessStatusCode)
{
@ -77,7 +76,7 @@ namespace BMA.EHR.Application.Repositories
}
}
protected async Task<string> PostExternalAPIAsync(string apiPath, string accessToken, object? body)
protected async Task<string> PostExternalAPIAsync(string apiPath, string accessToken, object? body, string apiKey)
{
try
{
@ -88,7 +87,7 @@ namespace BMA.EHR.Application.Repositories
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]);
client.DefaultRequestHeaders.Add("api_key", apiKey);
var _res = await client.PostAsync(apiPath, stringContent);
if (_res.IsSuccessStatusCode)
{
@ -105,7 +104,7 @@ namespace BMA.EHR.Application.Repositories
}
}
protected async Task<bool> PostExternalAPIBooleanAsync(string apiPath, string accessToken, object? body)
protected async Task<bool> PostExternalAPIBooleanAsync(string apiPath, string accessToken, object? body, string apiKey)
{
try
{
@ -116,7 +115,7 @@ namespace BMA.EHR.Application.Repositories
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]);
client.DefaultRequestHeaders.Add("api_key", apiKey);
var _res = await client.PostAsync(apiPath, stringContent);
return _res.IsSuccessStatusCode;
}

View file

@ -68,9 +68,10 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/root/officer/{rootId}";
var apiKey = _configuration["API_KEY"];
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "");
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<GetProfileByRootIdResultDto>(apiResult);
@ -100,9 +101,10 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/root/employee/{rootId}";
var apiKey = _configuration["API_KEY"];
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "");
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<GetProfileByRootIdResultDto>(apiResult);
@ -132,9 +134,10 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/keycloak/{keycloakId}";
var apiKey = _configuration["API_KEY"];
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "");
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<GetProfileByKeycloakIdResultDto>(apiResult);
@ -164,6 +167,7 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/update-dutytime";
var apiKey = _configuration["API_KEY"];
var body = new
{
@ -172,7 +176,7 @@ namespace BMA.EHR.Application.Repositories
ProfileId = profileId
};
var apiResult = await PostExternalAPIBooleanAsync(apiPath, accessToken ?? "", body);
var apiResult = await PostExternalAPIBooleanAsync(apiPath, accessToken ?? "", body, apiKey);
return apiResult;
//var profile = await _dbContext.Set<Profile>()
@ -208,8 +212,9 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/keycloak";
var apiKey = _configuration["API_KEY"];
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "");
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<GetListProfileByKeycloakIdResultDto>(apiResult);
@ -230,6 +235,7 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/search";
var apiKey = _configuration["API_KEY"];
var body = new
{
citizenId = citizenId,
@ -239,7 +245,7 @@ namespace BMA.EHR.Application.Repositories
var profiles = new List<SearchProfileDto>();
var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body);
var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey);
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<SearchProfileResultDto>(apiResult);
@ -282,6 +288,7 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/search-employee";
var apiKey = _configuration["API_KEY"];
var body = new
{
citizenId = citizenId,
@ -291,7 +298,7 @@ namespace BMA.EHR.Application.Repositories
var profiles = new List<SearchProfileDto>();
var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body);
var apiResult = await PostExternalAPIAsync(apiPath, accessToken, body, apiKey);
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<SearchProfileResultDto>(apiResult);
@ -334,8 +341,9 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/user-fullname/{keycloakId}";
var apiKey = _configuration["API_KEY"];
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "");
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult.Result != null)
{
var raw = JsonConvert.DeserializeObject<GetUserFullNameResultDto>(apiResult.Result);
@ -358,8 +366,9 @@ namespace BMA.EHR.Application.Repositories
{
var apiPath = $"{_configuration["API"]}/org/dotnet/user-oc/{keycloakId}";
var apiKey = _configuration["API_KEY"];
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "");
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult.Result != null)
{
var raw = JsonConvert.DeserializeObject<GetUserOCIdResultDto>(apiResult.Result);
@ -392,8 +401,9 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/root-oc/{ocId}";
var apiKey = _configuration["API_KEY"];
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "");
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult.Result != null)
{
var raw = JsonConvert.DeserializeObject<GetRootOCIdResultDto>(apiResult.Result);
@ -421,6 +431,7 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/find/all";
var apiKey = _configuration["API_KEY"];
var body = new
{
nodeId = ocId,
@ -428,7 +439,7 @@ namespace BMA.EHR.Application.Repositories
};
var apiResult = PostExternalAPIAsync(apiPath, accessToken ?? "", body).Result;
var apiResult = PostExternalAPIAsync(apiPath, accessToken ?? "", body, apiKey).Result;
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<GetOrganizationResponseResultDTO>(apiResult);
@ -451,8 +462,9 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/profile/{id}";
var apiKey = _configuration["API_KEY"];
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "");
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult.Result != null)
{
var raw = JsonConvert.DeserializeObject<GetProfileByIdResultDto>(apiResult.Result);
@ -473,8 +485,9 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/profile-employee/{id}";
var apiKey = _configuration["API_KEY"];
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "");
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult.Result != null)
{
var raw = JsonConvert.DeserializeObject<GetProfileByIdResultDto>(apiResult.Result);
@ -495,8 +508,9 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/dotnet/citizenId/{citizenId}";
var apiKey = _configuration["API_KEY"];
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "");
var apiResult = GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult.Result != null)
{
var raw = JsonConvert.DeserializeObject<GetProfileByIdResultDto>(apiResult.Result);
@ -517,8 +531,9 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/active/root";
var apiKey = _configuration["API_KEY"];
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "");
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
if (apiResult != null)
{
var raw = JsonConvert.DeserializeObject<GetActiveRootResultDto>(apiResult);
@ -540,10 +555,11 @@ namespace BMA.EHR.Application.Repositories
try
{
var apiPath = $"{_configuration["API"]}/org/profile/insignia";
var apiKey = _configuration["API_KEY"];
var profiles = new List<SearchProfileDto>();
var apiResult = await PostExternalAPIBooleanAsync(apiPath, accessToken ?? "", body);
var apiResult = await PostExternalAPIBooleanAsync(apiPath, accessToken ?? "", body, apiKey);
}
catch