diff --git a/DoliMiddlewareApi/Services/DolibarrApiClient.cs b/DoliMiddlewareApi/Services/DolibarrApiClient.cs index 0525875..f2c549f 100644 --- a/DoliMiddlewareApi/Services/DolibarrApiClient.cs +++ b/DoliMiddlewareApi/Services/DolibarrApiClient.cs @@ -20,7 +20,7 @@ public class DolibarrApiClient // ===== FACTURAS ===== public async Task GetInvoiceAsync(int id) { - var data = await GetAsync($"invoices/{id}"); + var data = await GetResourceAsync($"invoices/{id}"); return InvoiceMapper.MapToInvoiceDetailDto(data); } @@ -37,31 +37,31 @@ public class DolibarrApiClient endpoint += $"&status={status}"; } - var dataList = await GetListAsync(endpoint); + var dataList = await GetCollectionAsync(endpoint); return dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList(); } // ===== MÉTODOS GENÉRICOS ===== - private async Task GetAsync(string endpoint) where T : class + private async Task GetResourceAsync(string endpoint) where T : class { var response = await _httpClient.GetAsync(endpoint); - await HandleErrorsAsync(response, endpoint); + await EnsureSuccessOrThrowAsync(response, endpoint); return await response.Content.ReadFromJsonAsync() ?? throw new ApiException($"Failed to deserialize response from Dolibarr for endpoint '{endpoint}'"); } - private async Task> GetListAsync(string endpoint) where T : class + private async Task> GetCollectionAsync(string endpoint) where T : class { var response = await _httpClient.GetAsync(endpoint); - await HandleErrorsAsync(response, endpoint); + await EnsureSuccessOrThrowAsync(response, endpoint); return await response.Content.ReadFromJsonAsync>() ?? throw new ApiException( $"Failed to deserialize list response from Dolibarr for endpoint '{endpoint}'"); } - private async Task HandleErrorsAsync(HttpResponseMessage response, string endpoint) + private async Task EnsureSuccessOrThrowAsync(HttpResponseMessage response, string endpoint) { if (response.IsSuccessStatusCode) return;