2024-11-25 10:09:38 +05:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2024-12-09 14:45:35 +05:00
|
|
|
|
using Persistence.Client.Clients.Interfaces;
|
|
|
|
|
using Persistence.Client.Clients;
|
2024-11-21 14:50:36 +05:00
|
|
|
|
using Persistence.Client.Helpers;
|
2024-11-25 10:09:38 +05:00
|
|
|
|
using Refit;
|
2024-12-09 14:45:35 +05:00
|
|
|
|
using Persistence.Factories;
|
|
|
|
|
using Persistence.Client.Clients.Interfaces.Refit;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-11-21 14:50:36 +05:00
|
|
|
|
|
|
|
|
|
namespace Persistence.Client
|
|
|
|
|
{
|
2024-11-25 14:29:42 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Фабрика клиентов для доступа к Persistence - сервису
|
|
|
|
|
/// </summary>
|
2024-11-25 10:09:38 +05:00
|
|
|
|
public class PersistenceClientFactory
|
2024-11-21 14:50:36 +05:00
|
|
|
|
{
|
|
|
|
|
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
|
|
|
|
|
{
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
PropertyNameCaseInsensitive = true
|
|
|
|
|
};
|
|
|
|
|
private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions));
|
2024-12-09 14:45:35 +05:00
|
|
|
|
private readonly ILogger logger;
|
2024-11-25 10:09:38 +05:00
|
|
|
|
private HttpClient httpClient;
|
2024-12-09 14:45:35 +05:00
|
|
|
|
public PersistenceClientFactory(IHttpClientFactory httpClientFactory, ILogger<PersistenceClientFactory> logger, IConfiguration configuration)
|
2024-11-21 14:50:36 +05:00
|
|
|
|
{
|
2024-12-09 14:45:35 +05:00
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
|
|
|
|
httpClient = httpClientFactory.CreateClient();
|
2024-11-21 14:50:36 +05:00
|
|
|
|
|
2024-11-25 10:09:38 +05:00
|
|
|
|
httpClient.Authorize(configuration);
|
2024-11-21 14:50:36 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-09 14:45:35 +05:00
|
|
|
|
public PersistenceClientFactory(IHttpClientFactory httpClientFactory, IAuthTokenFactory authTokenFactory, ILogger logger, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
|
|
|
|
httpClient = httpClientFactory.CreateClient();
|
|
|
|
|
|
|
|
|
|
var token = authTokenFactory.GetToken();
|
|
|
|
|
httpClient.Authorize(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить клиент для работы с уставками
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public ISetpointClient GetSetpointClient()
|
|
|
|
|
{
|
|
|
|
|
var restClient = RestService.For<IRefitSetpointClient>(httpClient, RefitSettings);
|
|
|
|
|
var client = new SetpointClient(restClient, logger);
|
|
|
|
|
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить клиент для работы с технологическими сообщениями
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public ITechMessagesClient GetTechMessagesClient()
|
2024-11-21 14:50:36 +05:00
|
|
|
|
{
|
2024-12-09 14:45:35 +05:00
|
|
|
|
var restClient = RestService.For<IRefitTechMessagesClient>(httpClient, RefitSettings);
|
|
|
|
|
var client = new TechMessagesClient(restClient, logger);
|
|
|
|
|
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить клиент для работы с временными данными
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TDto"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public ITimeSeriesClient<TDto> GetTimeSeriesClient<TDto>()
|
|
|
|
|
where TDto : class, new()
|
|
|
|
|
{
|
|
|
|
|
var restClient = RestService.For<IRefitTimeSeriesClient<TDto>>(httpClient, RefitSettings);
|
|
|
|
|
var client = new TimeSeriesClient<TDto>(restClient, logger);
|
|
|
|
|
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить клиент для работы с данными с отметкой времени
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public ITimestampedSetClient GetTimestampedSetClient()
|
|
|
|
|
{
|
|
|
|
|
var restClient = RestService.For<IRefitTimestampedSetClient>(httpClient, RefitSettings);
|
|
|
|
|
var client = new TimestampedSetClient(restClient, logger);
|
|
|
|
|
|
|
|
|
|
return client;
|
2024-11-21 14:50:36 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|