96 lines
3.0 KiB
C#
96 lines
3.0 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// Фабрика клиентов для доступа к Persistence - сервису
|
||
/// </summary>
|
||
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<PersistenceClientFactory> 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);
|
||
}
|
||
|
||
/// <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()
|
||
{
|
||
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;
|
||
}
|
||
}
|
||
}
|