57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
namespace TaxBaik.Web.Services.AdminClients;
|
|
|
|
using System.Collections.Generic;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using TaxBaik.Domain.Entities;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
public interface ICommonCodeBrowserClient
|
|
{
|
|
Task<List<CommonCode>> GetAllActiveAsync(CancellationToken ct = default);
|
|
Task<List<CommonCode>> GetByGroupAsync(string group, CancellationToken ct = default);
|
|
}
|
|
|
|
public class CommonCodeBrowserClient(HttpClient httpClient, ITokenStore tokenStore, ILogger<CommonCodeBrowserClient> logger) : ICommonCodeBrowserClient
|
|
{
|
|
private const string BaseUrl = "/api/commoncode";
|
|
|
|
private void EnsureAuthHeader()
|
|
{
|
|
if (!string.IsNullOrEmpty(tokenStore.AccessToken))
|
|
httpClient.DefaultRequestHeaders.Authorization = new("Bearer", tokenStore.AccessToken);
|
|
else
|
|
httpClient.DefaultRequestHeaders.Authorization = null;
|
|
}
|
|
|
|
public async Task<List<CommonCode>> GetAllActiveAsync(CancellationToken ct = default)
|
|
{
|
|
try
|
|
{
|
|
EnsureAuthHeader();
|
|
return await httpClient.GetFromJsonAsync<List<CommonCode>>($"{BaseUrl}", ct) ?? [];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Failed to get all active common codes");
|
|
return [];
|
|
}
|
|
}
|
|
|
|
public async Task<List<CommonCode>> GetByGroupAsync(string group, CancellationToken ct = default)
|
|
{
|
|
try
|
|
{
|
|
EnsureAuthHeader();
|
|
return await httpClient.GetFromJsonAsync<List<CommonCode>>($"{BaseUrl}/group/{group}", ct) ?? [];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Failed to get common codes for group {Group}", group);
|
|
return [];
|
|
}
|
|
}
|
|
}
|