-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDependencyInjectionSetup.cs
153 lines (134 loc) · 7.47 KB
/
DependencyInjectionSetup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Net.Http;
using commercetools.Base.Client.Middlewares;
using commercetools.Base.Client.Tokens;
using commercetools.Base.Serialization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace commercetools.Base.Client
{
public static class DependencyInjectionSetup
{
public static void UseHttp(this IServiceCollection services)
{
services.AddHttpClient();
}
public static IDictionary<string, IHttpClientBuilder> UseHttpApi(this IServiceCollection services,
IConfiguration configuration, IList<string> clients,
Func<IServiceProvider, ISerializerService> serializerFactory,
Func<HttpResponseMessage, Type> errorResponseTypeMapper,
Func<string, IConfiguration, IServiceProvider, ITokenProvider> tokenProviderSupplier,
ClientOptions options = null,
IEnumerable<DelegatingMiddleware> middlewares = null)
{
options ??= new ClientOptions();
if (clients.Count() == 1)
{
return services.UseSingleClient(configuration, clients.First(), serializerFactory, errorResponseTypeMapper, tokenProviderSupplier, options, middlewares);
}
return services.UseMultipleClients(configuration, clients, serializerFactory, errorResponseTypeMapper, tokenProviderSupplier, options, middlewares);
}
private static IDictionary<string, IHttpClientBuilder> UseMultipleClients(this IServiceCollection services,
IConfiguration configuration, IList<string> clients,
Func<IServiceProvider, ISerializerService> serializerFactory,
Func<HttpResponseMessage, Type> errorResponseTypeMapper,
Func<string, IConfiguration, IServiceProvider, ITokenProvider> tokenProviderSupplier,
ClientOptions options,
IEnumerable<DelegatingMiddleware> middlewares = null)
{
var builders = new ConcurrentDictionary<string, IHttpClientBuilder>();
clients.ToList().ForEach(clientName =>
{
IClientConfiguration clientConfiguration =
configuration.GetSection(clientName).Get<ClientConfiguration>();
Validator.ValidateObject(clientConfiguration, new ValidationContext(clientConfiguration), true);
builders.TryAdd(clientName, services.SetupClient(clientName, errorResponseTypeMapper, serializerFactory, options));
services.AddSingleton(serviceProvider =>
{
var client = new ClientBuilder
{
ClientName = clientName,
ClientConfiguration = clientConfiguration,
HttpClient = serviceProvider.GetService<IHttpClientFactory>().CreateClient(clientName),
SerializerService = serializerFactory(serviceProvider),
TokenProvider = tokenProviderSupplier(clientName, configuration, serviceProvider),
ReadResponseAsStream = options.ReadResponseAsStream,
CorrelationIdProvider = serviceProvider.GetService<ICorrelationIdProvider>(),
Middlewares = middlewares
}.Build();
return client;
});
});
return builders;
}
private static IDictionary<string, IHttpClientBuilder> UseSingleClient(this IServiceCollection services,
IConfiguration configuration, string clientName,
Func<IServiceProvider, ISerializerService> serializerFactory,
Func<HttpResponseMessage, Type> errorResponseTypeMapper,
Func<string, IConfiguration, IServiceProvider, ITokenProvider> tokenProviderSupplier,
ClientOptions options,
IEnumerable<DelegatingMiddleware> middlewares = null)
{
IClientConfiguration clientConfiguration = configuration.GetSection(clientName).Get<ClientConfiguration>();
Validator.ValidateObject(clientConfiguration, new ValidationContext(clientConfiguration), true);
services.AddSingleton(serviceProvider =>
{
var client = new ClientBuilder
{
ClientName = clientName,
ClientConfiguration = clientConfiguration,
HttpClient = serviceProvider.GetService<IHttpClientFactory>().CreateClient(clientName),
SerializerService = serializerFactory(serviceProvider),
TokenProvider = tokenProviderSupplier(clientName, configuration, serviceProvider),
ReadResponseAsStream = options.ReadResponseAsStream,
CorrelationIdProvider = serviceProvider.GetService<ICorrelationIdProvider>(),
Middlewares = middlewares
}.Build();
client.Name = clientName;
return client;
});
var builders = new ConcurrentDictionary<string, IHttpClientBuilder>();
builders.TryAdd(clientName, services.SetupClient(clientName, errorResponseTypeMapper, serializerFactory, options));
return builders;
}
public static IHttpClientBuilder SetupClient(this IServiceCollection services, string clientName, Func<HttpResponseMessage, Type> errorResponseTypeMapper, Func<IServiceProvider, ISerializerService> serializerFactory, ClientOptions options = null)
{
options ??= new ClientOptions();
services.AddSingleton<IUserAgentProvider, UserAgentProvider>();
services.AddSingleton<ILoggerHandlerFactory, LoggerHandlerFactory>();
services.AddSingleton<IHttpLogger, DefaultHttpLogger>();
services.AddSingleton<ILoggerHandlerOptions, LoggerHandlerOptions>();
var httpClientBuilder = services.AddHttpClient(clientName)
.ConfigureHttpClient((provider, client) =>
{
if (options.DecompressionMethods.HasFlag(DecompressionMethods.GZip))
{
client.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip");
}
if (options.DecompressionMethods.HasFlag(DecompressionMethods.Deflate))
{
client.DefaultRequestHeaders.AcceptEncoding.ParseAdd("deflate");
}
client.DefaultRequestVersion = options.UseHttpVersion;
var userAgentProvider = provider.GetService<IUserAgentProvider>() ?? new UserAgentProvider();
client.DefaultRequestHeaders.UserAgent.ParseAdd(userAgentProvider.UserAgent);
})
.ConfigureHttpMessageHandlerBuilder(builder =>
{
builder.PrimaryHandler = new SocketsHttpHandler()
{
AutomaticDecompression = options.DecompressionMethods
};
})
.AddHttpMessageHandler(c => c.GetService<ILoggerHandlerFactory>().Create())
.AddHttpMessageHandler(c => new ErrorHandler(message =>
serializerFactory(c).Deserialize(errorResponseTypeMapper(message), message.ExtractResponseBody())));
return httpClientBuilder;
}
}
}