diff --git a/DD.Persistence.API/DependencyInjection.cs b/DD.Persistence.API/DependencyInjection.cs index b0a71d5..6b0754c 100644 --- a/DD.Persistence.API/DependencyInjection.cs +++ b/DD.Persistence.API/DependencyInjection.cs @@ -16,12 +16,6 @@ namespace DD.Persistence.API; public static class DependencyInjection { - //public static void MapsterSetup() - //{ - // TypeAdapterConfig.GlobalSettings.Default.Config - // .ForType() - // .Ignore(dest => dest.System, dest => dest.SystemId); - //} public static void AddSwagger(this IServiceCollection services, IConfiguration configuration) { services.AddSwaggerGen(c => diff --git a/DD.Persistence.API/Startup.cs b/DD.Persistence.API/Startup.cs index c6c44a4..8f07c31 100644 --- a/DD.Persistence.API/Startup.cs +++ b/DD.Persistence.API/Startup.cs @@ -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) diff --git a/DD.Persistence.App/appsettings.Tests.json b/DD.Persistence.App/appsettings.Tests.json index 72c43d3..9934757 100644 --- a/DD.Persistence.App/appsettings.Tests.json +++ b/DD.Persistence.App/appsettings.Tests.json @@ -1,6 +1,6 @@ { "DbConnection": { - "Host": "postgres", + "Host": "localhost", "Port": 5432, "Database": "persistence", "Username": "postgres", diff --git a/DD.Persistence.App/appsettings.json b/DD.Persistence.App/appsettings.json index 8002fc7..7ad8c67 100644 --- a/DD.Persistence.App/appsettings.json +++ b/DD.Persistence.App/appsettings.json @@ -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/" } diff --git a/DD.Persistence.Client/Clients/ChangeLogClient.cs b/DD.Persistence.Client/Clients/ChangeLogClient.cs index e10008e..38c2ec4 100644 --- a/DD.Persistence.Client/Clients/ChangeLogClient.cs +++ b/DD.Persistence.Client/Clients/ChangeLogClient.cs @@ -3,16 +3,17 @@ 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 logger) : base(logger) + public ChangeLogClient(IRefitClientFactory refitClientFactory, ILogger logger) : base(logger) { - this.refitChangeLogClient = refitChangeLogClient; - } + this.refitChangeLogClient = refitClientFactory.Create(); + } public async Task ClearAndAddRange(Guid idDiscriminator, IEnumerable dtos, CancellationToken token) { diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitChangeLogClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitChangeLogClient.cs index 958e24e..83c240f 100644 --- a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitChangeLogClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitChangeLogClient.cs @@ -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"; diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitClient.cs new file mode 100644 index 0000000..9568a51 --- /dev/null +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitClient.cs @@ -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 +{ +} diff --git a/DD.Persistence.Client/DependencyInjection.cs b/DD.Persistence.Client/DependencyInjection.cs new file mode 100644 index 0000000..ab4d80f --- /dev/null +++ b/DD.Persistence.Client/DependencyInjection.cs @@ -0,0 +1,28 @@ +using DD.Persistence.Client.Clients; +using DD.Persistence.Client.Clients.Interfaces; +using Microsoft.Extensions.DependencyInjection; + +namespace DD.Persistence.Client; + +/// +/// +/// +public static class DependencyInjection +{ + /// + /// + /// + /// + /// + public static IServiceCollection AddPersistenceClients(this IServiceCollection services) + { + services.AddSingleton(typeof(IRefitClientFactory<>), typeof(RefitClientFactory<>)); + services.AddTransient(); + //services.AddTransient(); + //services.AddTransient(); + //services.AddTransient(); + //services.AddTransient(); + //services.AddTransient(); + return services; + } +} diff --git a/DD.Persistence.Client/IRefitClientFactory.cs b/DD.Persistence.Client/IRefitClientFactory.cs new file mode 100644 index 0000000..0b41a9a --- /dev/null +++ b/DD.Persistence.Client/IRefitClientFactory.cs @@ -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; + +/// +/// Интерфейс для фабрики, которая создает refit-клиентов +/// +/// +public interface IRefitClientFactory where T : IRefitClient +{ + /// + /// Создание refit-клиента + /// + /// + public T Create(); +} diff --git a/DD.Persistence.Client/PersistenceClientFactory.cs b/DD.Persistence.Client/PersistenceClientFactory.cs index ad7c817..1ebfbd5 100644 --- a/DD.Persistence.Client/PersistenceClientFactory.cs +++ b/DD.Persistence.Client/PersistenceClientFactory.cs @@ -105,15 +105,15 @@ namespace DD.Persistence.Client /// Получить клиент для работы с записями ChangeLog /// /// - public IChangeLogClient GetChangeLogClient() - { - var logger = provider.GetRequiredService>(); + //public IChangeLogClient GetChangeLogClient() + //{ + // var logger = provider.GetRequiredService>(); - var restClient = RestService.For(httpClient, RefitSettings); - var client = new ChangeLogClient(restClient, logger); + // var restClient = RestService.For(httpClient, RefitSettings); + // var client = new ChangeLogClient(restClient, logger); - return client; - } + // return client; + //} /// /// Получить клиент для работы c параметрами Wits diff --git a/DD.Persistence.Client/RefitClientFactory.cs b/DD.Persistence.Client/RefitClientFactory.cs new file mode 100644 index 0000000..a83aae5 --- /dev/null +++ b/DD.Persistence.Client/RefitClientFactory.cs @@ -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; + +/// +/// Фабрика, которая создает refit-клиентов +/// +/// +public class RefitClientFactory : IRefitClientFactory where T : IRefitClient +{ + private HttpClient client; + private RefitSettings refitSettings; + + /// + /// + /// + public RefitClientFactory(IConfiguration configuration) + { + this.client = new HttpClient(); + + var baseUrl = configuration.GetSection("ClientUrl").Get()!; + client.BaseAddress = new Uri(baseUrl); + client.Authorize(configuration); + + JsonSerializerOptions JsonSerializerOptions = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + PropertyNameCaseInsensitive = true + }; + refitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions)); + } + /// + /// создание клиента + /// + /// + public T Create() + { + + + return RestService.For(client, refitSettings); + } +} diff --git a/DD.Persistence.IntegrationTests/Controllers/ChangeLogControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/ChangeLogControllerTest.cs index 3f1bb6c..64419e7 100644 --- a/DD.Persistence.IntegrationTests/Controllers/ChangeLogControllerTest.cs +++ b/DD.Persistence.IntegrationTests/Controllers/ChangeLogControllerTest.cs @@ -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(); + var refitClientFactory = scope.ServiceProvider + .GetRequiredService>(); + var logger = scope.ServiceProvider.GetRequiredService>(); - client = persistenceClientFactory.GetChangeLogClient(); + client = scope.ServiceProvider + .GetRequiredService(); } [Fact] diff --git a/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs b/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs index 76cf71b..d953f50 100644 --- a/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs +++ b/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs @@ -46,6 +46,7 @@ public class WebAppFactoryFixture : WebApplicationFactory }); services.AddSingleton(); + services.AddPersistenceClients(); var serviceProvider = services.BuildServiceProvider(); @@ -56,6 +57,9 @@ public class WebAppFactoryFixture : WebApplicationFactory dbContext.Database.EnsureCreatedAndMigrated(); dbContext.SaveChanges(); }); + + builder.UseUrls("http://localhost:5000"); + } public override async ValueTask DisposeAsync()