using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Persistence.Client.Clients.Interfaces;
using Persistence.Client.Clients;
using Persistence.Client.Helpers;
using Refit;
using Persistence.Factories;
using Persistence.Client.Clients.Interfaces.Refit;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
namespace Persistence.Client
{
///
/// Фабрика клиентов для доступа к Persistence - сервису
///
public class PersistenceClientFactory
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true
};
private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions));
private readonly IServiceProvider provider;
private HttpClient httpClient;
public PersistenceClientFactory(IHttpClientFactory httpClientFactory, IServiceProvider provider, IConfiguration configuration)
{
this.provider = provider;
httpClient = httpClientFactory.CreateClient();
httpClient.Authorize(configuration);
}
public PersistenceClientFactory(IHttpClientFactory httpClientFactory, IAuthTokenFactory authTokenFactory, IServiceProvider provider, IConfiguration configuration)
{
this.provider = provider;
httpClient = httpClientFactory.CreateClient();
var token = authTokenFactory.GetToken();
httpClient.Authorize(token);
}
///
/// Получить клиент для работы с уставками
///
///
public ISetpointClient GetSetpointClient()
{
var logger = provider.GetRequiredService>();
var restClient = RestService.For(httpClient, RefitSettings);
var client = new SetpointClient(restClient, logger);
return client;
}
///
/// Получить клиент для работы с технологическими сообщениями
///
///
public ITechMessagesClient GetTechMessagesClient()
{
var logger = provider.GetRequiredService>();
var restClient = RestService.For(httpClient, RefitSettings);
var client = new TechMessagesClient(restClient, logger);
return client;
}
///
/// Получить клиент для работы с временными данными
///
///
///
public ITimeSeriesClient GetTimeSeriesClient()
where TDto : class, new()
{
var logger = provider.GetRequiredService>>();
var restClient = RestService.For>(httpClient, RefitSettings);
var client = new TimeSeriesClient(restClient, logger);
return client;
}
///
/// Получить клиент для работы с данными с отметкой времени
///
///
public ITimestampedSetClient GetTimestampedSetClient()
{
var logger = provider.GetRequiredService>();
var restClient = RestService.For(httpClient, RefitSettings);
var client = new TimestampedSetClient(restClient, logger);
return client;
}
///
/// Получить клиент для работы с записями ChangeLog
///
///
public IChangeLogClient GetChangeLogClient()
{
var logger = provider.GetRequiredService>();
var restClient = RestService.For(httpClient, RefitSettings);
var client = new ChangeLogClient(restClient, logger);
return client;
}
}
}