persistence/DD.Persistence.Client/Clients/Base/BaseClient.cs
Olga Nemt d90b72b14e 1. Исправлены namespaces.
2. Добавлен проект DD.Persistence.App со всеми необходимыми настройками
2024-12-16 15:38:46 +05:00

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;
}
}