-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathHttp.cs
More file actions
178 lines (141 loc) · 6.2 KB
/
Copy pathHttp.cs
File metadata and controls
178 lines (141 loc) · 6.2 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using ModularPipelines.Logging;
using ModularPipelines.Options;
namespace ModularPipelines.Http;
internal class Http : IHttp, IDisposable
{
public HttpClient HttpClient { get; }
private readonly IModuleLoggerProvider _moduleLoggerProvider;
private readonly IHttpLogger _httpLogger;
private readonly IOptions<PipelineOptions> _pipelineOptions;
private readonly ConcurrentDictionary<HttpLoggingType, (HttpClient Client, ServiceProvider Provider)> _loggingHttpClients = new();
public Http(HttpClient defaultHttpClient,
IModuleLoggerProvider moduleLoggerProvider,
IHttpLogger httpLogger,
IOptions<PipelineOptions> pipelineOptions)
{
HttpClient = defaultHttpClient;
_moduleLoggerProvider = moduleLoggerProvider;
_httpLogger = httpLogger;
_pipelineOptions = pipelineOptions;
}
public async Task<HttpResponseMessage> SendAsync(HttpOptions httpOptions, CancellationToken cancellationToken = default)
{
if (httpOptions.HttpClient != null)
{
return await SendAndWrapLogging(httpOptions, cancellationToken).ConfigureAwait(false);
}
var httpClient = GetLoggingHttpClient(httpOptions.LoggingType);
var response = await httpClient.SendAsync(httpOptions.HttpRequestMessage, cancellationToken).ConfigureAwait(false);
if (!httpOptions.ThrowOnNonSuccessStatusCode)
{
return response;
}
return response.EnsureSuccessStatusCode();
}
public HttpClient GetLoggingHttpClient(HttpLoggingType loggingType)
{
return _loggingHttpClients.GetOrAdd(loggingType, _ =>
{
var moduleLogger = _moduleLoggerProvider.GetLogger();
var serviceCollection = new ServiceCollection()
.AddSingleton(moduleLogger)
.AddSingleton(_httpLogger);
var httpClientBuilder = serviceCollection
.AddHttpClient<ModularPipelinesHttpClientProvider>();
if (loggingType.HasFlag(HttpLoggingType.Request))
{
serviceCollection
.AddTransient<RequestLoggingHttpHandler>();
httpClientBuilder
.AddHttpMessageHandler<RequestLoggingHttpHandler>();
}
if (loggingType.HasFlag(HttpLoggingType.Response))
{
serviceCollection
.AddTransient<ResponseLoggingHttpHandler>();
httpClientBuilder.AddHttpMessageHandler<ResponseLoggingHttpHandler>();
}
if (loggingType.HasFlag(HttpLoggingType.Duration))
{
serviceCollection
.AddTransient<DurationLoggingHttpHandler>();
httpClientBuilder.AddHttpMessageHandler<DurationLoggingHttpHandler>();
}
if (loggingType.HasFlag(HttpLoggingType.StatusCode))
{
serviceCollection
.AddTransient<StatusCodeLoggingHttpHandler>();
httpClientBuilder.AddHttpMessageHandler<StatusCodeLoggingHttpHandler>();
}
var serviceProvider = serviceCollection.BuildServiceProvider();
var httpClient = serviceProvider
.GetRequiredService<ModularPipelinesHttpClientProvider>()
.HttpClient;
return (httpClient, serviceProvider);
}).Client;
}
public void Dispose()
{
// Note: HttpClient is obtained from IHttpClientFactory via DI and should NOT be disposed.
// The factory manages the lifetime of HttpClient instances.
// Dispose the ServiceProviders which will clean up their internal resources
foreach (var (_, provider) in _loggingHttpClients.Values)
{
provider.Dispose();
}
}
private async Task<HttpResponseMessage> SendAndWrapLogging(HttpOptions httpOptions, CancellationToken cancellationToken)
{
var logger = _moduleLoggerProvider.GetLogger();
var loggingOptions = GetEffectiveLoggingOptions(httpOptions);
if (httpOptions.LoggingType.HasFlag(HttpLoggingType.Request))
{
await _httpLogger.PrintRequest(httpOptions.HttpRequestMessage, logger, loggingOptions).ConfigureAwait(false);
}
var stopWatch = Stopwatch.StartNew();
var response = await httpOptions.HttpClient!.SendAsync(httpOptions.HttpRequestMessage, cancellationToken).ConfigureAwait(false);
LogStatusCode(response.StatusCode, httpOptions, loggingOptions);
LogDuration(stopWatch.Elapsed, httpOptions, loggingOptions);
if (httpOptions.LoggingType.HasFlag(HttpLoggingType.Response))
{
await _httpLogger.PrintResponse(response, logger, loggingOptions).ConfigureAwait(false);
}
if (!httpOptions.ThrowOnNonSuccessStatusCode)
{
return response;
}
return response.EnsureSuccessStatusCode();
}
private HttpLoggingOptions GetEffectiveLoggingOptions(HttpOptions httpOptions)
{
// Priority: options property > pipeline default > default
if (httpOptions.LogSettings is not null)
{
return httpOptions.LogSettings;
}
if (_pipelineOptions.Value.DefaultHttpLoggingOptions is not null)
{
return _pipelineOptions.Value.DefaultHttpLoggingOptions;
}
return HttpLoggingOptions.Default;
}
private void LogDuration(TimeSpan duration, HttpOptions httpOptions, HttpLoggingOptions loggingOptions)
{
if (httpOptions.LoggingType.HasFlag(HttpLoggingType.Duration) && loggingOptions.LogDuration)
{
_httpLogger.PrintDuration(duration, _moduleLoggerProvider.GetLogger());
}
}
private void LogStatusCode(HttpStatusCode? httpStatusCode, HttpOptions httpOptions, HttpLoggingOptions loggingOptions)
{
if (httpOptions.LoggingType.HasFlag(HttpLoggingType.StatusCode) && loggingOptions.LogStatusCode)
{
_httpLogger.PrintStatusCode(httpStatusCode, _moduleLoggerProvider.GetLogger());
}
}
}