refactor: extraer interfaz IDolibarrApiClient para facilitar testing
- Crear IDolibarrApiClient con métodos de la API de Dolibarr - Implementar interfaz en DolibarrApiClient - Modificar InvoiceService para depender de IDolibarrApiClient en lugar de la clase concreta - Actualizar Program.cs para registrar IDolibarrApiClient en el contenedor DI Esto sigue el Principio de Inversión de Dependencias (DIP) de SOLID, permitiendo mockear el cliente API en tests unitarios sin conectividad externa.
This commit is contained in:
parent
2e1062cc71
commit
f28461ae0c
|
|
@ -22,7 +22,8 @@ builder.Services.AddSwaggerGen();
|
|||
|
||||
|
||||
// Typed Client - Inyecta HttpClient directamente en DolibarrApiClient
|
||||
builder.Services.AddHttpClient<DolibarrApiClient>(client =>
|
||||
// Registra la interfaz para poder hacer mock en tests
|
||||
builder.Services.AddHttpClient<IDolibarrApiClient, DolibarrApiClient>(client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(builder.Configuration["Dolibarr:ApiUrl"]!);
|
||||
client.DefaultRequestHeaders.Add("DOLAPIKEY", builder.Configuration["Dolibarr:ApiKey"]!);
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ using DoliMiddlewareApi.Exceptions;
|
|||
|
||||
namespace DoliMiddlewareApi.Services.Clients;
|
||||
|
||||
public class DolibarrApiClient
|
||||
public class DolibarrApiClient : IDolibarrApiClient
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
namespace DoliMiddlewareApi.Services.Clients;
|
||||
|
||||
public interface IDolibarrApiClient
|
||||
{
|
||||
Task<T> GetResourceAsync<T>(string endpoint) where T : class;
|
||||
Task<List<T>> GetCollectionAsync<T>(string endpoint) where T : class;
|
||||
Task<string> PostAsync(string endpoint, object requestBody);
|
||||
Task<string> PutAsync(string endpoint, object requestBody);
|
||||
}
|
||||
|
|
@ -10,9 +10,9 @@ namespace DoliMiddlewareApi.Services;
|
|||
|
||||
public class InvoiceService
|
||||
{
|
||||
private readonly DolibarrApiClient _apiClient;
|
||||
private readonly IDolibarrApiClient _apiClient;
|
||||
|
||||
public InvoiceService(DolibarrApiClient apiClient)
|
||||
public InvoiceService(IDolibarrApiClient apiClient)
|
||||
{
|
||||
_apiClient = apiClient;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue