2024-12-26 13:42:14 +05:00
|
|
|
|
using DD.Persistence.Client.Clients.Interfaces.Refit;
|
|
|
|
|
using DD.Persistence.Client.Helpers;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2024-12-26 15:47:24 +05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-12-26 13:42:14 +05:00
|
|
|
|
using Refit;
|
2024-12-26 15:47:24 +05:00
|
|
|
|
using System.Configuration;
|
2024-12-26 13:42:14 +05:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
namespace DD.Persistence.Client;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Фабрика, которая создает refit-клиентов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
public class RefitClientFactory<T> : IRefitClientFactory<T> where T : IRefitClient
|
|
|
|
|
{
|
|
|
|
|
private HttpClient client;
|
|
|
|
|
private RefitSettings refitSettings;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2024-12-27 00:37:52 +05:00
|
|
|
|
public RefitClientFactory(IConfiguration configuration, ILogger<IRefitClientFactory<T>> logger, HttpClient client)
|
2024-12-26 13:42:14 +05:00
|
|
|
|
{
|
2024-12-27 00:37:52 +05:00
|
|
|
|
//this.client = factory.CreateClient();
|
|
|
|
|
this.client = client;
|
2024-12-26 13:42:14 +05:00
|
|
|
|
|
2024-12-26 15:47:24 +05:00
|
|
|
|
var baseUrl = configuration.GetSection("ClientUrl").Get<string>();
|
|
|
|
|
if (String.IsNullOrEmpty(baseUrl))
|
|
|
|
|
{
|
|
|
|
|
var exception = new SettingsPropertyNotFoundException("В настройках конфигурации не указан адрес Persistence сервиса.");
|
|
|
|
|
|
|
|
|
|
logger.LogError(exception.Message);
|
|
|
|
|
|
|
|
|
|
throw exception;
|
|
|
|
|
}
|
2024-12-26 13:42:14 +05:00
|
|
|
|
client.BaseAddress = new Uri(baseUrl);
|
|
|
|
|
client.Authorize(configuration);
|
|
|
|
|
|
|
|
|
|
JsonSerializerOptions JsonSerializerOptions = new()
|
|
|
|
|
{
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
PropertyNameCaseInsensitive = true
|
|
|
|
|
};
|
|
|
|
|
refitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions));
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// создание клиента
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public T Create()
|
|
|
|
|
{
|
|
|
|
|
return RestService.For<T>(client, refitSettings);
|
|
|
|
|
}
|
|
|
|
|
}
|