refactor(ClientOptions): move to inf layer
This commit is contained in:
12
src/Leanetec.EConnect.Infrastructure/ClientOptions.cs
Normal file
12
src/Leanetec.EConnect.Infrastructure/ClientOptions.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Leanetec.EConnect.Infrastructure;
|
||||
|
||||
public class ClientOptions
|
||||
{
|
||||
public string? BaseAddress { get; set; }
|
||||
|
||||
public string? ApiKey { get; set; }
|
||||
|
||||
public TimeSpan? Timeout { get; set; }
|
||||
|
||||
public Dictionary<string, string?>? QueryStrings { get; set; }
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using Leanetec.EConnect.Client;
|
||||
using Leanetec.EConnect.Client.Interface;
|
||||
using Leanetec.EConnect.Client.Interface;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
@@ -9,7 +9,7 @@ public static class DependencyInjection
|
||||
{
|
||||
internal static readonly string EConnectClientName = Guid.NewGuid().ToString();
|
||||
|
||||
internal static IServiceCollection ConfigureEConnectClient(this IServiceCollection services, Action<HttpClient> configureClient)
|
||||
internal static IServiceCollection ConfigureEConnectClient(this IServiceCollection services)
|
||||
{
|
||||
services.AddHttpClient(EConnectClientName, (provider, client) => {
|
||||
var opt = provider.GetRequiredService<IOptions<ClientOptions>>().Value;
|
||||
@@ -20,9 +20,6 @@ public static class DependencyInjection
|
||||
|
||||
if (opt.Timeout is TimeSpan timeout)
|
||||
client.Timeout = timeout;
|
||||
|
||||
// add spesific (library based) parameters
|
||||
configureClient(client);
|
||||
});
|
||||
return services;
|
||||
}
|
||||
@@ -31,7 +28,7 @@ public static class DependencyInjection
|
||||
{
|
||||
Config config = new(services);
|
||||
options?.Invoke(config);
|
||||
services.ConfigureEConnectClient(config.EConnectClient);
|
||||
services.ConfigureEConnectClient();
|
||||
services.AddScoped(typeof(IEConnectClient<>), typeof(EConnectClient<>));
|
||||
return services;
|
||||
}
|
||||
@@ -45,6 +42,14 @@ public static class DependencyInjection
|
||||
_services = services;
|
||||
}
|
||||
|
||||
public Action<HttpClient> EConnectClient { get; set; } = _ => { };
|
||||
public void ConfigureClient(IConfiguration config)
|
||||
{
|
||||
_services.Configure<ClientOptions>(config);
|
||||
}
|
||||
|
||||
public void ConfigureClient(Action<ClientOptions> options)
|
||||
{
|
||||
_services.Configure(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user