63 lines
1.3 KiB
C#
63 lines
1.3 KiB
C#
using DD.Persistence.Client.Helpers;
|
|
using Microsoft.Extensions.Logging;
|
|
using Refit;
|
|
|
|
namespace DD.Persistence.Client.Clients.Base;
|
|
public abstract class BaseClient
|
|
{
|
|
private readonly ILogger logger;
|
|
|
|
public BaseClient(ILogger<BaseClient> logger)
|
|
{
|
|
this.logger = logger;
|
|
}
|
|
|
|
public async Task<T?> ExecuteGetResponse<T>(Func<Task<IApiResponse<T>>> getMethod, CancellationToken token)
|
|
{
|
|
var response = await getMethod.Invoke().WaitAsync(token);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
return response.Content;
|
|
}
|
|
|
|
var exception = response.GetPersistenceException();
|
|
|
|
logger.LogError(exception.Message);
|
|
|
|
throw exception;
|
|
}
|
|
|
|
public async Task ExecutePostResponse(Func<Task<IApiResponse>> postMethod, CancellationToken token)
|
|
{
|
|
var response = await postMethod.Invoke().WaitAsync(token);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var exception = response.GetPersistenceException();
|
|
|
|
logger.LogError(exception.Message);
|
|
|
|
throw exception;
|
|
}
|
|
|
|
public async Task<int> ExecutePostResponse(Func<Task<IApiResponse<int>>> postMethod, CancellationToken token)
|
|
{
|
|
var response = await postMethod.Invoke().WaitAsync(token);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
return response.Content;
|
|
}
|
|
|
|
var exception = response.GetPersistenceException();
|
|
|
|
logger.LogError(exception.Message);
|
|
|
|
throw exception;
|
|
}
|
|
}
|