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; 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 ILogger logger; private HttpClient httpClient; public PersistenceClientFactory(IHttpClientFactory httpClientFactory, ILogger logger, IConfiguration configuration) { this.logger = logger; httpClient = httpClientFactory.CreateClient(); httpClient.Authorize(configuration); } public PersistenceClientFactory(IHttpClientFactory httpClientFactory, IAuthTokenFactory authTokenFactory, ILogger logger, IConfiguration configuration) { this.logger = logger; httpClient = httpClientFactory.CreateClient(); var token = authTokenFactory.GetToken(); httpClient.Authorize(token); } /// /// Получить клиент для работы с уставками /// /// public ISetpointClient GetSetpointClient() { var restClient = RestService.For(httpClient, RefitSettings); var client = new SetpointClient(restClient, logger); return client; } /// /// Получить клиент для работы с технологическими сообщениями /// /// public ITechMessagesClient GetTechMessagesClient() { var restClient = RestService.For(httpClient, RefitSettings); var client = new TechMessagesClient(restClient, logger); return client; } /// /// Получить клиент для работы с временными данными /// /// /// public ITimeSeriesClient GetTimeSeriesClient() where TDto : class, new() { var restClient = RestService.For>(httpClient, RefitSettings); var client = new TimeSeriesClient(restClient, logger); return client; } /// /// Получить клиент для работы с данными с отметкой времени /// /// public ITimestampedSetClient GetTimestampedSetClient() { var restClient = RestService.For(httpClient, RefitSettings); var client = new TimestampedSetClient(restClient, logger); return client; } } }