using Microsoft.Extensions.Configuration; using DD.Persistence.Client.Clients.Interfaces; using DD.Persistence.Client.Clients; using DD.Persistence.Client.Helpers; using Refit; using DD.Persistence.Factories; using DD.Persistence.Client.Clients.Interfaces.Refit; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; using System.Text.Json; namespace DD.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); httpClient.Timeout = TimeSpan.FromSeconds(6000); } public PersistenceClientFactory(IHttpClientFactory httpClientFactory, IAuthTokenFactory authTokenFactory, IServiceProvider provider, IConfiguration configuration) { this.provider = provider; httpClient = httpClientFactory.CreateClient(); var token = authTokenFactory.GetToken(); httpClient.Authorize(token); httpClient.Timeout = TimeSpan.FromSeconds(6000); } /// /// Получить клиент для работы с уставками /// /// 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; } /// /// Получить клиент для работы c параметрами Wits /// /// public IWitsDataClient GetWitsDataClient() { var logger = provider.GetRequiredService>(); var restClient = RestService.For(httpClient, RefitSettings); var client = new WitsDataClient(restClient, logger); return client; } /// /// Получить клиент для работы c системами /// /// public IDataSourceSystemClient GetDataSourceSystemClient() { var logger = provider.GetRequiredService>(); var restClient = RestService.For(httpClient, RefitSettings); var client = new DataSourceSystemClient(restClient, logger); return client; } } }