enchance: cambio nombres de variables para ser mas descriptivos
This commit is contained in:
parent
198fccd5bc
commit
1a752c007f
|
|
@ -20,7 +20,7 @@ public class DolibarrApiClient
|
||||||
// ===== FACTURAS =====
|
// ===== FACTURAS =====
|
||||||
public async Task<InvoiceDetailDto> GetInvoiceAsync(int id)
|
public async Task<InvoiceDetailDto> GetInvoiceAsync(int id)
|
||||||
{
|
{
|
||||||
var data = await GetAsync<InvoiceDetailResponse>($"invoices/{id}");
|
var data = await GetResourceAsync<InvoiceDetailResponse>($"invoices/{id}");
|
||||||
return InvoiceMapper.MapToInvoiceDetailDto(data);
|
return InvoiceMapper.MapToInvoiceDetailDto(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,31 +37,31 @@ public class DolibarrApiClient
|
||||||
endpoint += $"&status={status}";
|
endpoint += $"&status={status}";
|
||||||
}
|
}
|
||||||
|
|
||||||
var dataList = await GetListAsync<InvoiceResponse>(endpoint);
|
var dataList = await GetCollectionAsync<InvoiceResponse>(endpoint);
|
||||||
return dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList();
|
return dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== MÉTODOS GENÉRICOS =====
|
// ===== MÉTODOS GENÉRICOS =====
|
||||||
private async Task<T> GetAsync<T>(string endpoint) where T : class
|
private async Task<T> GetResourceAsync<T>(string endpoint) where T : class
|
||||||
{
|
{
|
||||||
var response = await _httpClient.GetAsync(endpoint);
|
var response = await _httpClient.GetAsync(endpoint);
|
||||||
await HandleErrorsAsync(response, endpoint);
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
||||||
|
|
||||||
return await response.Content.ReadFromJsonAsync<T>()
|
return await response.Content.ReadFromJsonAsync<T>()
|
||||||
?? throw new ApiException($"Failed to deserialize response from Dolibarr for endpoint '{endpoint}'");
|
?? throw new ApiException($"Failed to deserialize response from Dolibarr for endpoint '{endpoint}'");
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<List<T>> GetListAsync<T>(string endpoint) where T : class
|
private async Task<List<T>> GetCollectionAsync<T>(string endpoint) where T : class
|
||||||
{
|
{
|
||||||
var response = await _httpClient.GetAsync(endpoint);
|
var response = await _httpClient.GetAsync(endpoint);
|
||||||
await HandleErrorsAsync(response, endpoint);
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
||||||
|
|
||||||
return await response.Content.ReadFromJsonAsync<List<T>>()
|
return await response.Content.ReadFromJsonAsync<List<T>>()
|
||||||
?? throw new ApiException(
|
?? throw new ApiException(
|
||||||
$"Failed to deserialize list response from Dolibarr for endpoint '{endpoint}'");
|
$"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;
|
if (response.IsSuccessStatusCode) return;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue