-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathHttp.cs
More file actions
491 lines (430 loc) · 18.3 KB
/
Http.cs
File metadata and controls
491 lines (430 loc) · 18.3 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
using Azure;
using Azure.Core;
using Azure.Core.Pipeline;
using LanguageExt;
using LanguageExt.UnsafeValueAccess;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
namespace common;
public static class HttpPipelineExtensions
{
public static async ValueTask<BinaryData> GetContent(this HttpPipeline pipeline, Uri uri, CancellationToken cancellationToken)
{
var either = await pipeline.TryGetContent(uri, cancellationToken);
return either.IfLeftThrow(uri);
}
/// <summary>
/// Gets the response content. If the status code is <see cref="HttpStatusCode.NotFound"/>, returns <see cref="Option.None"/>.
/// </summary>
public static async ValueTask<Option<BinaryData>> GetContentOption(this HttpPipeline pipeline, Uri uri, CancellationToken cancellationToken)
{
var either = await pipeline.TryGetContent(uri, cancellationToken);
return either.Match(response =>
{
using (response)
{
return response.Status == (int)HttpStatusCode.NotFound
? Option<BinaryData>.None
: throw response.ToHttpRequestException(uri);
}
}, Option<BinaryData>.Some);
}
public static async ValueTask<Either<Response, BinaryData>> TryGetContent(this HttpPipeline pipeline, Uri uri, CancellationToken cancellationToken)
{
using var request = pipeline.CreateRequest(uri, RequestMethod.Get);
var response = await pipeline.SendRequestAsync(request, cancellationToken);
if (response.IsError)
{
return response;
}
else
{
using (response)
{
return response.Content;
}
}
}
public static HttpRequestException ToHttpRequestException(this Response response, Uri requestUri) =>
new(message: $"HTTP request to URI {requestUri} failed with status code {response.Status}. Content is '{response.Content}'.", inner: null, statusCode: (HttpStatusCode)response.Status);
private static T IfLeftThrow<T>(this Either<Response, T> either, Uri requestUri) =>
either.IfLeft(response =>
{
using (response)
{
throw response.ToHttpRequestException(requestUri);
}
});
public static async IAsyncEnumerable<JsonObject> ListJsonObjects(this HttpPipeline pipeline, Uri uri, [EnumeratorCancellation] CancellationToken cancellationToken)
{
Uri? nextLink = uri;
while (nextLink is not null)
{
var responseJson = await pipeline.GetJsonObject(nextLink, cancellationToken);
var values = responseJson.TryGetJsonArrayProperty("value")
.IfLeft(() => [])
.PickJsonObjects();
foreach (var value in values)
{
yield return value;
}
nextLink = responseJson.TryGetAbsoluteUriProperty("nextLink")
.ValueUnsafe();
}
}
public static async ValueTask<JsonObject> GetJsonObject(this HttpPipeline pipeline, Uri uri, CancellationToken cancellationToken)
{
var either = await pipeline.TryGetJsonObject(uri, cancellationToken);
return either.IfLeftThrow(uri);
}
/// <summary>
/// Gets the response content as a JSON object. If the status code is <see cref="HttpStatusCode.NotFound"/>, returns <see cref="Option.None"/>.
/// </summary>
public static async ValueTask<Option<JsonObject>> GetJsonObjectOption(this HttpPipeline pipeline, Uri uri, CancellationToken cancellationToken)
{
var option = await pipeline.GetContentOption(uri, cancellationToken);
return option.Map(content => content.ToObjectFromJson<JsonObject>());
}
public static async ValueTask<Either<Response, JsonObject>> TryGetJsonObject(this HttpPipeline pipeline, Uri uri, CancellationToken cancellationToken)
{
var either = await pipeline.TryGetContent(uri, cancellationToken);
return either.Map(content => content.ToObjectFromJson<JsonObject>());
}
public static async ValueTask DeleteResource(this HttpPipeline pipeline, Uri uri, bool waitForCompletion, CancellationToken cancellationToken)
{
var either = await pipeline.TryDeleteResource(uri, waitForCompletion, cancellationToken);
either.IfLeft(response =>
{
using (response)
{
if (response.Status != 404)
{
throw response.ToHttpRequestException(uri);
}
}
});
}
public static async ValueTask<Either<Response, Unit>> TryDeleteResource(this HttpPipeline pipeline, Uri uri, bool waitForCompletion, CancellationToken cancellationToken)
{
using var request = pipeline.CreateRequest(uri, RequestMethod.Delete);
var response = await pipeline.SendRequestAsync(request, cancellationToken);
if (response.IsError)
{
return response;
};
using (response)
{
if (waitForCompletion)
{
var operationResponse = await pipeline.WaitForLongRunningOperation(response, cancellationToken);
if (operationResponse.IsError)
{
return operationResponse;
}
else
{
using (operationResponse)
{
return Unit.Default;
}
}
}
else
{
return Unit.Default;
}
}
}
public static async ValueTask PutContent(this HttpPipeline pipeline, Uri uri, BinaryData content, CancellationToken cancellationToken)
{
var either = await pipeline.TryPutContent(uri, content, cancellationToken);
#pragma warning disable CA1806 // Do not ignore method results
either.IfLeft(response => throw response.ToHttpRequestException(uri));
#pragma warning restore CA1806 // Do not ignore method results
}
public static async ValueTask<Either<Response, Unit>> TryPutContent(this HttpPipeline pipeline, Uri uri, BinaryData content, CancellationToken cancellationToken)
{
using var request = pipeline.CreateRequest(uri, RequestMethod.Put);
request.Content = RequestContent.Create(content);
request.Headers.Add("Content-type", "application/json");
var response = await pipeline.SendRequestAsync(request, cancellationToken);
if (response.IsError)
{
return response;
};
using (response)
{
var operationResponse = await pipeline.WaitForLongRunningOperation(response, cancellationToken);
if (operationResponse.IsError)
{
return operationResponse;
}
else
{
using (operationResponse)
{
return Unit.Default;
}
}
}
}
public static async ValueTask PatchContent(this HttpPipeline pipeline, Uri uri, BinaryData content, CancellationToken cancellationToken)
{
var either = await pipeline.TryPatchContent(uri, content, cancellationToken);
#pragma warning disable CA1806 // Do not ignore method results
either.IfLeft(response => throw response.ToHttpRequestException(uri));
#pragma warning restore CA1806 // Do not ignore method results
}
public static async ValueTask<Either<Response, Unit>> TryPatchContent(this HttpPipeline pipeline, Uri uri, BinaryData content, CancellationToken cancellationToken)
{
using var request = pipeline.CreateRequest(uri, RequestMethod.Patch);
request.Content = RequestContent.Create(content);
request.Headers.Add("Content-type", "application/json");
var response = await pipeline.SendRequestAsync(request, cancellationToken);
if (response.IsError)
{
return response;
};
using (response)
{
var operationResponse = await pipeline.WaitForLongRunningOperation(response, cancellationToken);
if (operationResponse.IsError)
{
return operationResponse;
}
else
{
using (operationResponse)
{
return Unit.Default;
}
}
}
}
public static Request CreateRequest(this HttpPipeline pipeline, Uri uri, RequestMethod requestMethod)
{
var request = pipeline.CreateRequest();
request.Uri.Reset(uri);
request.Method = requestMethod;
return request;
}
private static async ValueTask<Response> WaitForLongRunningOperation(this HttpPipeline pipeline, Response response, CancellationToken cancellationToken)
{
var updatedResponse = response;
while (((updatedResponse.Status is (int)HttpStatusCode.OK or (int)HttpStatusCode.Created && IsProvisioningInProgress(updatedResponse)) ||
updatedResponse.Status == (int)HttpStatusCode.Accepted)
&& updatedResponse.Headers.TryGetValue("Location", out var locationHeaderValue)
&& Uri.TryCreate(locationHeaderValue, UriKind.Absolute, out var locationUri)
&& locationUri is not null)
{
if (updatedResponse.Headers.TryGetValue("Retry-After", out var retryAfterString) && int.TryParse(retryAfterString, out var retryAfterSeconds))
{
var retryAfterDuration = TimeSpan.FromSeconds(retryAfterSeconds);
await Task.Delay(retryAfterDuration, cancellationToken);
}
else
{
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
}
using var request = pipeline.CreateRequest(locationUri, RequestMethod.Get);
updatedResponse = await pipeline.SendRequestAsync(request, cancellationToken);
if (updatedResponse.IsError)
{
throw updatedResponse.ToHttpRequestException(locationUri);
}
}
return updatedResponse;
}
private static bool IsProvisioningInProgress(Response response)
{
try
{
return response.Content.ToObjectFromJson<JsonObject>()
.TryGetJsonObjectProperty("properties")
.Bind(json => json.TryGetStringProperty("ProvisioningState"))
.ToOption()
.Where(state => state.Equals("InProgress", StringComparison.OrdinalIgnoreCase))
.IsSome;
}
catch (JsonException)
{
return false;
}
}
}
public sealed class ILoggerHttpPipelinePolicy(ILogger logger) : HttpPipelinePolicy
{
public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
{
ProcessAsync(message, pipeline).AsTask().GetAwaiter().GetResult();
}
public override async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
{
if (logger.IsEnabled(LogLevel.Trace))
{
logger.LogTrace("""
Starting request
Method: {HttpMethod}
Uri: {Uri}
Content: {RequestContent}
""", message.Request.Method, message.Request.Uri, await GetRequestContent(message, message.CancellationToken));
}
else if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug("""
Starting request
Method: {HttpMethod}
Uri: {Uri}
""", message.Request.Method, message.Request.Uri);
}
var startTime = Stopwatch.GetTimestamp();
await ProcessNextAsync(message, pipeline);
var endTime = Stopwatch.GetTimestamp();
var duration = TimeSpan.FromSeconds((endTime - startTime) / (double)Stopwatch.Frequency);
if (logger.IsEnabled(LogLevel.Trace))
{
logger.LogTrace("""
Received response
Method: {HttpMethod}
Uri: {Uri}
Status code: {StatusCode}
Duration (hh:mm:ss): {Duration}
Content: {ResponseContent}
""", message.Request.Method, message.Request.Uri, message.Response.Status, duration.ToString("c"), GetResponseContent(message));
}
else if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug("""
Received response
Method: {HttpMethod}
Uri: {Uri}
Status code: {StatusCode}
Duration (hh:mm:ss): {Duration}
""", message.Request.Method, message.Request.Uri, message.Response.Status, duration.ToString("c"));
}
}
private static async ValueTask<string> GetRequestContent(HttpMessage message, CancellationToken cancellationToken)
{
if (message.Request.Content is null)
{
return "<null>";
}
else if (HeaderIsJson(message.Request.Headers))
{
using var stream = new MemoryStream();
await message.Request.Content.WriteToAsync(stream, cancellationToken);
stream.Position = 0;
var data = await BinaryData.FromStreamAsync(stream, cancellationToken);
return data.ToString();
}
else
{
return "<non-json>";
}
}
private static bool HeaderIsJson(IEnumerable<HttpHeader> headers) =>
headers.Any(header => header.Name.Equals("Content-Type", StringComparison.OrdinalIgnoreCase)
&& header.Value.Contains("application/json", StringComparison.OrdinalIgnoreCase));
private static string GetResponseContent(HttpMessage message) =>
message.Response.Content is null
? "<null>"
: HeaderIsJson(message.Response.Headers)
? message.Response.Content.ToString()
: "<non-json>";
}
public class CommonRetryPolicy : RetryPolicy
{
protected override bool ShouldRetry(HttpMessage message, Exception? exception) =>
base.ShouldRetry(message, exception) || ShouldRetryInner(message, exception);
protected override async ValueTask<bool> ShouldRetryAsync(HttpMessage message, Exception? exception) =>
await base.ShouldRetryAsync(message, exception) || ShouldRetryInner(message, exception);
private static bool ShouldRetryInner(HttpMessage message, Exception? exception)
{
try
{
return
(message, exception) switch
{
({ Response.Status: 422 or 409 }, _) when HasManagementApiRequestFailedError(message.Response) => true,
({ Response.Status: 409 }, _) when HasConflictOrPessimisticConcurrencyConflictError(message.Response) && HasOperationOnTheApiIsInProgressMessage(message.Response) => true,
({ Response.Status: 412 }, _) => true,
({ Response.Status: 429 }, _) => true,
_ => false
};
}
catch (InvalidOperationException)
{
return false;
}
}
private static bool HasManagementApiRequestFailedError(Response response) =>
TryGetErrorCode(response)
.Where(code => code.Equals("ManagementApiRequestFailed", StringComparison.OrdinalIgnoreCase))
.IsSome;
private static bool HasConflictOrPessimisticConcurrencyConflictError(Response response) =>
TryGetErrorCode(response)
.Where(code =>
code.Equals("Conflict", StringComparison.OrdinalIgnoreCase) ||
code.Equals("PessimisticConcurrencyConflict", StringComparison.OrdinalIgnoreCase)
)
.IsSome;
private static bool HasOperationOnTheApiIsInProgressMessage(Response response) =>
TryGetMessage(response)
.Where(code => code.Equals("Operation on the API is in progress", StringComparison.OrdinalIgnoreCase))
.IsSome;
private static Option<string> TryGetErrorCode(Response response)
{
try
{
return response.Content
.ToObjectFromJson<JsonObject>()
.TryGetJsonObjectProperty("error")
.Bind(error => error.TryGetStringProperty("code"))
.ToOption();
}
catch (Exception exception) when (exception is ArgumentNullException or NotSupportedException or JsonException)
{
return Option<string>.None;
}
}
private static Option<string> TryGetMessage(Response response)
{
try
{
return response.Content
.ToObjectFromJson<JsonObject>()
.TryGetJsonObjectProperty("error")
.Bind(error => error.TryGetStringProperty("message"))
.ToOption();
}
catch (Exception exception) when (exception is ArgumentNullException or NotSupportedException or JsonException)
{
return Option<string>.None;
}
}
}
public class TelemetryPolicy(Version version) : HttpPipelinePolicy
{
public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
{
ProcessAsync(message, pipeline).AsTask().GetAwaiter().GetResult();
}
public override async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
{
var header = new ProductHeaderValue("apimanagement-apiops", version.ToString());
message.Request.Headers.Add(HttpHeader.Names.UserAgent, header.ToString());
await ProcessNextAsync(message, pipeline);
}
}