Новая фабрика рефит-клиентов (RefitClientFactory).
Some checks failed
Unit tests / test (push) Failing after 1m1s

Работает пока только для ChangeLog
This commit is contained in:
Оля Бизюкова 2024-12-26 13:42:14 +05:00
parent 01898e84f6
commit 6593acbc20
13 changed files with 138 additions and 26 deletions

View File

@ -16,12 +16,6 @@ namespace DD.Persistence.API;
public static class DependencyInjection
{
//public static void MapsterSetup()
//{
// TypeAdapterConfig.GlobalSettings.Default.Config
// .ForType<TechMessageDto, TechMessage>()
// .Ignore(dest => dest.System, dest => dest.SystemId);
//}
public static void AddSwagger(this IServiceCollection services, IConfiguration configuration)
{
services.AddSwaggerGen(c =>

View File

@ -1,6 +1,7 @@
using DD.Persistence.Database.Model;
using DD.Persistence.Database.Postgres;
using DD.Persistence.Repository;
using DD.Persistence.Client;
namespace DD.Persistence.API;
@ -26,8 +27,7 @@ public class Startup
services.AddJWTAuthentication(Configuration);
services.AddMemoryCache();
services.AddServices();
//DependencyInjection.MapsterSetup();
services.AddPersistenceClients();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

View File

@ -1,6 +1,6 @@
{
"DbConnection": {
"Host": "postgres",
"Host": "localhost",
"Port": 5432,
"Database": "persistence",
"Username": "postgres",

View File

@ -19,6 +19,7 @@
"password": 12345,
"clientId": "webapi",
"grantType": "password",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier": "7d9f3574-6574-4ca3-845a-0276eb4aa8f6"
}
"http://schemas.xmlsoap.org/ws/2005/05/identity /claims/nameidentifier": "7d9f3574-6574-4ca3-845a-0276eb4aa8f6"
},
"ClientUrl": "http://localhost:5000/"
}

View File

@ -3,15 +3,16 @@ using DD.Persistence.Client.Clients.Base;
using DD.Persistence.Client.Clients.Interfaces;
using DD.Persistence.Models;
using DD.Persistence.Models.Requests;
using DD.Persistence.Client.Clients.Interfaces.Refit;
namespace DD.Persistence.Client.Clients;
public class ChangeLogClient : BaseClient, IChangeLogClient
{
private readonly Interfaces.Refit.IRefitChangeLogClient refitChangeLogClient;
private readonly IRefitChangeLogClient refitChangeLogClient;
public ChangeLogClient(Interfaces.Refit.IRefitChangeLogClient refitChangeLogClient, ILogger<ChangeLogClient> logger) : base(logger)
public ChangeLogClient(IRefitClientFactory<IRefitChangeLogClient> refitClientFactory, ILogger<ChangeLogClient> logger) : base(logger)
{
this.refitChangeLogClient = refitChangeLogClient;
this.refitChangeLogClient = refitClientFactory.Create();
}
public async Task<int> ClearAndAddRange(Guid idDiscriminator, IEnumerable<DataWithWellDepthAndSectionDto> dtos, CancellationToken token)

View File

@ -4,7 +4,7 @@ using Refit;
namespace DD.Persistence.Client.Clients.Interfaces.Refit;
public interface IRefitChangeLogClient : IDisposable
public interface IRefitChangeLogClient : IRefitClient, IDisposable
{
private const string BaseRoute = "/api/ChangeLog";

View File

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DD.Persistence.Client.Clients.Interfaces.Refit;
public interface IRefitClient
{
}

View File

@ -0,0 +1,28 @@
using DD.Persistence.Client.Clients;
using DD.Persistence.Client.Clients.Interfaces;
using Microsoft.Extensions.DependencyInjection;
namespace DD.Persistence.Client;
/// <summary>
///
/// </summary>
public static class DependencyInjection
{
/// <summary>
///
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddPersistenceClients(this IServiceCollection services)
{
services.AddSingleton(typeof(IRefitClientFactory<>), typeof(RefitClientFactory<>));
services.AddTransient<IChangeLogClient, ChangeLogClient>();
//services.AddTransient<ISetpointClient, SetpointClient>();
//services.AddTransient<ISetpointClient, SetpointClient>();
//services.AddTransient<ISetpointClient, SetpointClient>();
//services.AddTransient<ISetpointClient, SetpointClient>();
//services.AddTransient<ISetpointClient, SetpointClient>();
return services;
}
}

View File

@ -0,0 +1,21 @@
using DD.Persistence.Client.Clients.Interfaces.Refit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DD.Persistence.Client;
/// <summary>
/// Интерфейс для фабрики, которая создает refit-клиентов
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IRefitClientFactory<T> where T : IRefitClient
{
/// <summary>
/// Создание refit-клиента
/// </summary>
/// <returns></returns>
public T Create();
}

View File

@ -105,15 +105,15 @@ namespace DD.Persistence.Client
/// Получить клиент для работы с записями ChangeLog
/// </summary>
/// <returns></returns>
public IChangeLogClient GetChangeLogClient()
{
var logger = provider.GetRequiredService<ILogger<ChangeLogClient>>();
//public IChangeLogClient GetChangeLogClient()
//{
// var logger = provider.GetRequiredService<ILogger<ChangeLogClient>>();
var restClient = RestService.For<IRefitChangeLogClient>(httpClient, RefitSettings);
var client = new ChangeLogClient(restClient, logger);
// var restClient = RestService.For<IRefitChangeLogClient>(httpClient, RefitSettings);
// var client = new ChangeLogClient(restClient, logger);
return client;
}
// return client;
//}
/// <summary>
/// Получить клиент для работы c параметрами Wits

View File

@ -0,0 +1,46 @@
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);
}
}

View File

@ -7,6 +7,11 @@ using DD.Persistence.Models.Requests;
using Xunit;
using DD.Persistence.Client.Clients.Interfaces;
using DD.Persistence.Client;
using DD.Persistence.Client.Clients.Interfaces.Refit;
using DD.Persistence.Client.Clients;
using Microsoft.Extensions.Logging;
using Refit;
using System.Net.Http;
namespace DD.Persistence.IntegrationTests.Controllers;
public class ChangeLogControllerTest : BaseIntegrationTest
@ -16,10 +21,12 @@ public class ChangeLogControllerTest : BaseIntegrationTest
public ChangeLogControllerTest(WebAppFactoryFixture factory) : base(factory)
{
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
var refitClientFactory = scope.ServiceProvider
.GetRequiredService<IRefitClientFactory<IRefitChangeLogClient>>();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<ChangeLogClient>>();
client = persistenceClientFactory.GetChangeLogClient();
client = scope.ServiceProvider
.GetRequiredService<IChangeLogClient>();
}
[Fact]

View File

@ -46,6 +46,7 @@ public class WebAppFactoryFixture : WebApplicationFactory<Program>
});
services.AddSingleton<PersistenceClientFactory>();
services.AddPersistenceClients();
var serviceProvider = services.BuildServiceProvider();
@ -56,6 +57,9 @@ public class WebAppFactoryFixture : WebApplicationFactory<Program>
dbContext.Database.EnsureCreatedAndMigrated();
dbContext.SaveChanges();
});
builder.UseUrls("http://localhost:5000");
}
public override async ValueTask DisposeAsync()