2025-12-29 15:37:46 +00:00
|
|
|
using System.Net;
|
|
|
|
|
using DoliMiddlewareApi.Exceptions;
|
2026-01-03 18:52:07 +00:00
|
|
|
using DoliMiddlewareApi.Services.Auth;
|
2025-12-29 15:37:46 +00:00
|
|
|
|
2025-12-31 14:15:00 +00:00
|
|
|
namespace DoliMiddlewareApi.Services.Clients;
|
2025-12-29 15:37:46 +00:00
|
|
|
|
2026-01-03 19:31:53 +00:00
|
|
|
public class DolibarrApiClient(HttpClient httpClient, DolibarrTokenCacheService tokenCacheService)
|
|
|
|
|
: IDolibarrApiClient
|
2025-12-29 15:37:46 +00:00
|
|
|
{
|
2025-12-31 14:15:00 +00:00
|
|
|
public async Task<T> GetResourceAsync<T>(string endpoint) where T : class
|
2025-12-29 15:37:46 +00:00
|
|
|
{
|
2026-01-03 18:52:07 +00:00
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
|
|
|
|
|
AddDolibarrTokenHeader(request);
|
2026-01-03 19:31:53 +00:00
|
|
|
var response = await httpClient.SendAsync(request);
|
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-31 14:15:00 +00:00
|
|
|
public async Task<List<T>> GetCollectionAsync<T>(string endpoint) where T : class
|
2025-12-29 15:37:46 +00:00
|
|
|
{
|
2026-01-03 18:52:07 +00:00
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
|
|
|
|
|
AddDolibarrTokenHeader(request);
|
2026-01-03 19:31:53 +00:00
|
|
|
var response = await httpClient.SendAsync(request);
|
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
|
|
|
}
|
2026-01-03 18:52:07 +00:00
|
|
|
|
2026-01-02 18:01:04 +00:00
|
|
|
public async Task<string> PostAsync(string endpoint, object requestBody)
|
|
|
|
|
{
|
2026-01-03 18:52:07 +00:00
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, endpoint)
|
|
|
|
|
{
|
|
|
|
|
Content = JsonContent.Create(requestBody)
|
|
|
|
|
};
|
|
|
|
|
AddDolibarrTokenHeader(request);
|
2026-01-03 19:31:53 +00:00
|
|
|
var response = await httpClient.SendAsync(request);
|
2026-01-02 18:01:04 +00:00
|
|
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
|
|
|
|
|
|
|
|
|
// porque devuelve el id como response
|
|
|
|
|
return await response.Content.ReadAsStringAsync();
|
|
|
|
|
}
|
2026-01-03 18:52:07 +00:00
|
|
|
|
2026-01-02 19:20:58 +00:00
|
|
|
public async Task<string> PutAsync(string endpoint, object requestBody)
|
|
|
|
|
{
|
2026-01-03 18:52:07 +00:00
|
|
|
var request = new HttpRequestMessage(HttpMethod.Put, endpoint)
|
|
|
|
|
{
|
|
|
|
|
Content = JsonContent.Create(requestBody)
|
|
|
|
|
};
|
|
|
|
|
AddDolibarrTokenHeader(request);
|
2026-01-03 19:31:53 +00:00
|
|
|
var response = await httpClient.SendAsync(request);
|
2026-01-02 19:20:58 +00:00
|
|
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
2026-01-03 18:52:07 +00:00
|
|
|
|
2026-01-02 19:20:58 +00:00
|
|
|
return await response.Content.ReadAsStringAsync();
|
|
|
|
|
}
|
2026-01-03 18:52:07 +00:00
|
|
|
|
2026-02-05 13:33:03 +00:00
|
|
|
public async Task DeleteAsync(string endpoint)
|
|
|
|
|
{
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Delete, endpoint);
|
|
|
|
|
AddDolibarrTokenHeader(request);
|
|
|
|
|
var response = await httpClient.SendAsync(request);
|
|
|
|
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
|
|
|
|
}
|
|
|
|
|
|
feat: add Contacts & Setup controllers, extend Clients/Invoices/Document controllers
- ContactsController: full CRUD (list, get, get by email, create, update, delete)
- SetupController: dictionary endpoints (payment-types, countries, civilities,
contact-types, payment-terms, company)
- ClientsController: added GET/{id}, POST, PUT/{id}, DELETE/{id}
- InvoicesController: added PUT/{id}/lines/{lineId}, GET /templates
- DocumentController: added GET /list and GET /download endpoints
- ClientService: added GetClientAsync, CreateClientAsync, UpdateClientAsync,
DeleteClientAsync
- InvoiceService: added UpdateInvoiceLineAsync, GetInvoiceTemplatesAsync
- DocumentService: added GetDocumentsAsync, DownloadDocumentAsync (using
new GetAsyncRaw on IDolibarrApiClient)
- ContactService: new service with CRUD operations
- SetupService: new service for Dolibarr dictionary data
- IDolibarrApiClient: added GetAsyncRaw method for raw string responses
- DolibarrApiClient: implemented GetAsyncRaw
- JwtTokenProvider: extended token expiry from 30 min to 8 hours
- AuthApplicationService: extended Dolibarr token cache from 30 min to 8 hours
2026-05-15 16:58:35 +00:00
|
|
|
public async Task<string> GetAsyncRaw(string endpoint)
|
|
|
|
|
{
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
|
|
|
|
|
AddDolibarrTokenHeader(request);
|
|
|
|
|
var response = await httpClient.SendAsync(request);
|
|
|
|
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
|
|
|
|
return await response.Content.ReadAsStringAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-03 18:52:07 +00:00
|
|
|
private void AddDolibarrTokenHeader(HttpRequestMessage request)
|
|
|
|
|
{
|
2026-01-03 19:31:53 +00:00
|
|
|
var dolibarrToken = tokenCacheService.GetDolibarrToken();
|
2026-01-03 18:52:07 +00:00
|
|
|
if (!string.IsNullOrEmpty(dolibarrToken))
|
|
|
|
|
{
|
|
|
|
|
request.Headers.Add("DOLAPIKEY", dolibarrToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-03 16:19:53 +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}")
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|