persistence/DD.Persistence.Client/RefitClientFactory.cs

47 lines
1.3 KiB
C#
Raw Normal View History

using DD.Persistence.Client.Clients.Interfaces.Refit;
using DD.Persistence.Client.Helpers;
using Microsoft.Extensions.Configuration;
using Refit;
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>
public RefitClientFactory(IConfiguration configuration)
{
this.client = new HttpClient();
var baseUrl = configuration.GetSection("ClientUrl").Get<string>()!;
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);
}
}