Новая фабрика рефит-клиентов (RefitClientFactory).
Some checks failed
Unit tests / test (push) Failing after 1m1s
Some checks failed
Unit tests / test (push) Failing after 1m1s
Работает пока только для ChangeLog
This commit is contained in:
parent
01898e84f6
commit
6593acbc20
@ -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 =>
|
||||
|
@ -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)
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"DbConnection": {
|
||||
"Host": "postgres",
|
||||
"Host": "localhost",
|
||||
"Port": 5432,
|
||||
"Database": "persistence",
|
||||
"Username": "postgres",
|
||||
|
@ -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/"
|
||||
}
|
||||
|
@ -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<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)
|
||||
{
|
||||
|
@ -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";
|
||||
|
||||
|
@ -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
|
||||
{
|
||||
}
|
28
DD.Persistence.Client/DependencyInjection.cs
Normal file
28
DD.Persistence.Client/DependencyInjection.cs
Normal 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;
|
||||
}
|
||||
}
|
21
DD.Persistence.Client/IRefitClientFactory.cs
Normal file
21
DD.Persistence.Client/IRefitClientFactory.cs
Normal 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();
|
||||
}
|
@ -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
|
||||
|
46
DD.Persistence.Client/RefitClientFactory.cs
Normal file
46
DD.Persistence.Client/RefitClientFactory.cs
Normal 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);
|
||||
}
|
||||
}
|
@ -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]
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user