fix: เริ่มต่อ api โครงสร้างใหม่ ระบบลา

This commit is contained in:
Suphonchai Phoonsawat 2024-05-29 13:33:23 +07:00
parent b7f19d7077
commit 66d491a2af
5 changed files with 134 additions and 23 deletions

View file

@ -4,7 +4,11 @@ using BMA.EHR.Domain.Models.Base;
using BMA.EHR.Domain.Models.HR;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text;
namespace BMA.EHR.Application.Repositories
{
@ -43,6 +47,60 @@ namespace BMA.EHR.Application.Repositories
#region " Methods "
#region " For Call External API "
protected async Task<string> GetExternalAPIAsync(string apiPath, string accessToken)
{
try
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
var _res = await client.GetAsync(apiPath);
if (_res.IsSuccessStatusCode)
{
var _result = await _res.Content.ReadAsStringAsync();
return _result;
}
return string.Empty;
}
}
catch
{
throw;
}
}
protected async Task<string> PostExternalAPIAsync(string apiPath, string accessToken, object? body)
{
try
{
var json = JsonConvert.SerializeObject(body);
var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
var _res = await client.PostAsync(apiPath, stringContent);
if (_res.IsSuccessStatusCode)
{
var _result = await _res.Content.ReadAsStringAsync();
return _result;
}
return string.Empty;
}
}
catch
{
throw;
}
}
#endregion
public async Task<Guid> GetProfileOrganizationAsync(string citizenId)
{
try