-
Notifications
You must be signed in to change notification settings - Fork 171
/
HttpRequestDataExtensions.cs
142 lines (123 loc) · 5.72 KB
/
HttpRequestDataExtensions.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.DependencyInjection;
using Azure.Core.Serialization;
using System.Threading;
using System.Text.Json;
namespace Microsoft.Azure.Functions.Worker.Http
{
/// <summary>
/// Provides extension methods to work with an <see cref="HttpRequestData"/> instance.
/// </summary>
public static class HttpRequestDataExtensions
{
/// <summary>
/// Reads the body payload as a string.
/// </summary>
/// <param name="request">The request from which to read.</param>
/// <param name="encoding">The encoding to use when reading the string. Defaults to UTF-8</param>
/// <returns>A <see cref="Task{String}"/> that represents the asynchronous read operation.</returns>
public static async Task<string?> ReadAsStringAsync(this HttpRequestData request, Encoding? encoding = null)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
if (request.Body is null)
{
return null;
}
using (var reader = new StreamReader(request.Body, bufferSize: -1, detectEncodingFromByteOrderMarks: true, encoding: encoding, leaveOpen: true))
{
return await reader.ReadToEndAsync();
}
}
/// <summary>
/// Reads the body payload as a string.
/// </summary>
/// <param name="request">The request from which to read.</param>
/// <param name="encoding">The encoding to use when reading the string. Defaults to UTF-8</param>
/// <returns>A <see cref="string"/> that represents request body.</returns>
public static string? ReadAsString(this HttpRequestData request, Encoding? encoding = null)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
if (request.Body is null)
{
return null;
}
using (var reader = new StreamReader(request.Body, bufferSize: -1, detectEncodingFromByteOrderMarks: true, encoding: encoding, leaveOpen: true))
{
return reader.ReadToEnd();
}
}
/// <summary>
/// Reads the request using the default <see cref="ObjectSerializer"/> configured for this worker.
/// </summary>
/// <typeparam name="T">The target type of the JSON value.</typeparam>
/// <param name="request">The request to be read.</param>
/// <param name="cancellationToken">A token that may be used to cancel the read operation.</param>
/// <returns>A <see cref="ValueTask{T}"/> representing the asynchronous operation.</returns>
public static ValueTask<T?> ReadFromJsonAsync<T>(this HttpRequestData request, CancellationToken cancellationToken = default)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
ObjectSerializer serializer = request.FunctionContext.InstanceServices.GetService<IOptions<WorkerOptions>>()?.Value?.Serializer
?? throw new InvalidOperationException("A serializer is not configured for the worker.");
return ReadFromJsonAsync<T>(request, serializer, cancellationToken);
}
/// <summary>
/// Reads the request using the provided <see cref="ObjectSerializer"/>.
/// </summary>
/// <typeparam name="T">The target type of the JSON value.</typeparam>
/// <param name="request">The request to be read.</param>
/// <param name="serializer">The <see cref="ObjectSerializer"/> to use for the deserialization.</param>
/// <param name="cancellationToken">A token that may be used to cancel the read operation.</param>
/// <returns>A <see cref="ValueTask{T}"/> representing the asynchronous operation.</returns>
public static ValueTask<T?> ReadFromJsonAsync<T>(this HttpRequestData request, ObjectSerializer serializer, CancellationToken cancellationToken = default)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
if (serializer is null)
{
throw new ArgumentNullException(nameof(serializer));
}
ValueTask<object?> result = serializer.DeserializeAsync(request.Body, typeof(T), cancellationToken);
static T? TryCast(object? value)
{
return value != null
? (T)value
: default;
}
if (result.IsCompletedSuccessfully)
{
return new ValueTask<T?>(TryCast(result.Result));
}
return new ValueTask<T?>(result.AsTask().ContinueWith(t => TryCast(t.Result)));
}
/// <summary>
/// Creates a response for the the provided <see cref="HttpRequestData"/>.
/// </summary>
/// <param name="request">The <see cref="HttpRequestData"/> for this response.</param>
/// <param name="statusCode">The response status code.</param>
/// <returns>The response data.</returns>
public static HttpResponseData CreateResponse(this HttpRequestData request, HttpStatusCode statusCode)
{
var response = request.CreateResponse();
response.StatusCode = statusCode;
return response;
}
}
}