2025-12-29 15:37:46 +00:00
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using DoliMiddlewareApi.Dtos;
|
2025-12-29 18:20:34 +00:00
|
|
|
using DoliMiddlewareApi.Dtos.Dolibarr;
|
2025-12-29 15:37:46 +00:00
|
|
|
using DoliMiddlewareApi.Exceptions;
|
2025-12-29 18:32:38 +00:00
|
|
|
using DoliMiddlewareApi.Mappers;
|
2025-12-29 15:37:46 +00:00
|
|
|
|
|
|
|
|
namespace DoliMiddlewareApi.Services;
|
|
|
|
|
|
|
|
|
|
public class DolibarrApiClient
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpClient _httpClient;
|
|
|
|
|
|
|
|
|
|
public DolibarrApiClient(HttpClient httpClient)
|
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ===== FACTURAS =====
|
2025-12-29 18:20:34 +00:00
|
|
|
public async Task<InvoiceDetailDto> GetInvoiceAsync(int id)
|
2025-12-29 15:37:46 +00:00
|
|
|
{
|
2025-12-30 18:14:59 +00:00
|
|
|
var data = await GetResourceAsync<InvoiceDetailResponse>($"invoices/{id}");
|
2025-12-29 18:32:38 +00:00
|
|
|
return InvoiceMapper.MapToInvoiceDetailDto(data);
|
2025-12-29 15:37:46 +00:00
|
|
|
}
|
2025-12-30 18:13:26 +00:00
|
|
|
|
2025-12-29 15:58:53 +00:00
|
|
|
public async Task<List<InvoiceDto>> GetInvoicesAsync(
|
|
|
|
|
int limit = 50,
|
|
|
|
|
int page = 1,
|
|
|
|
|
string? status = null)
|
2025-12-29 15:37:46 +00:00
|
|
|
{
|
2025-12-29 17:30:23 +00:00
|
|
|
// empieza por 1 para el frontend
|
|
|
|
|
var endpoint = $"invoices?limit={limit}&page={page - 1}";
|
2025-12-29 15:58:53 +00:00
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(status))
|
2025-12-29 17:30:23 +00:00
|
|
|
{
|
2025-12-29 15:58:53 +00:00
|
|
|
endpoint += $"&status={status}";
|
2025-12-29 17:30:23 +00:00
|
|
|
}
|
2025-12-30 18:13:26 +00:00
|
|
|
|
2025-12-30 18:14:59 +00:00
|
|
|
var dataList = await GetCollectionAsync<InvoiceResponse>(endpoint);
|
2025-12-29 18:32:38 +00:00
|
|
|
return dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList();
|
2025-12-29 18:20:34 +00:00
|
|
|
}
|
2025-12-30 18:13:26 +00:00
|
|
|
|
2025-12-29 15:37:46 +00:00
|
|
|
// ===== MÉTODOS GENÉRICOS =====
|
2025-12-30 18:14:59 +00:00
|
|
|
private async Task<T> GetResourceAsync<T>(string endpoint) where T : class
|
2025-12-29 15:37:46 +00:00
|
|
|
{
|
|
|
|
|
var response = await _httpClient.GetAsync(endpoint);
|
2025-12-30 18:14:59 +00:00
|
|
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
2025-12-29 15:37:46 +00:00
|
|
|
|
|
|
|
|
return await response.Content.ReadFromJsonAsync<T>()
|
2025-12-30 18:13:26 +00:00
|
|
|
?? throw new ApiException($"Failed to deserialize response from Dolibarr for endpoint '{endpoint}'");
|
2025-12-29 15:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-30 18:14:59 +00:00
|
|
|
private async Task<List<T>> GetCollectionAsync<T>(string endpoint) where T : class
|
2025-12-29 15:37:46 +00:00
|
|
|
{
|
|
|
|
|
var response = await _httpClient.GetAsync(endpoint);
|
2025-12-30 18:14:59 +00:00
|
|
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
2025-12-29 15:37:46 +00:00
|
|
|
|
2025-12-30 18:13:26 +00:00
|
|
|
return await response.Content.ReadFromJsonAsync<List<T>>()
|
|
|
|
|
?? throw new ApiException(
|
|
|
|
|
$"Failed to deserialize list response from Dolibarr for endpoint '{endpoint}'");
|
2025-12-29 15:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-30 18:14:59 +00:00
|
|
|
private async Task EnsureSuccessOrThrowAsync(HttpResponseMessage response, string endpoint)
|
2025-12-29 15:37:46 +00:00
|
|
|
{
|
|
|
|
|
if (response.IsSuccessStatusCode) return;
|
|
|
|
|
|
|
|
|
|
var errorContent = await response.Content.ReadAsStringAsync();
|
|
|
|
|
|
|
|
|
|
throw response.StatusCode switch
|
|
|
|
|
{
|
|
|
|
|
HttpStatusCode.NotFound => new NotFoundException($"Resource not found: {endpoint}"),
|
|
|
|
|
HttpStatusCode.Unauthorized => new UnauthorizedException("Invalid API credentials"),
|
|
|
|
|
HttpStatusCode.Forbidden => new ForbiddenException("Access to this resource is forbidden"),
|
|
|
|
|
HttpStatusCode.BadRequest => new BadRequestException($"Bad request: {errorContent}"),
|
|
|
|
|
HttpStatusCode.InternalServerError => new ApiException($"Server error: {errorContent}"),
|
|
|
|
|
_ => new ApiException($"Unexpected error ({response.StatusCode}): {errorContent}")
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|