-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFluentHttpRequest.cs
More file actions
130 lines (112 loc) · 4.14 KB
/
FluentHttpRequest.cs
File metadata and controls
130 lines (112 loc) · 4.14 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
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and Fluent Framework Contributors.
// All Rights Reserved.
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Fluent.Client;
/// <summary>
/// Represents a fluent HTTP request.
/// </summary>
/// <param name="client"><see cref="System.Net.Http.HttpClient"/> to use for sending the request.</param>
/// <param name="contents">Contents of the HTTP request.</param>
public sealed class FluentHttpRequest(HttpClient client, FluentHttpRequestContents? contents = null)
{
/// <summary>
/// Default JSON options for serialization.
/// </summary>
public static JsonSerializerOptions DefaultJsonOptions = new()
{
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true,
WriteIndented = true,
IncludeFields = false,
Converters = { new JsonStringEnumConverter() },
};
/// <summary>
/// Gets the encoded parameters for the request URI.
/// </summary>
public Uri Uri
{
get
{
string query = Contents.Path ?? "";
if (Contents.QueryParameters is not null && Contents.QueryParameters.Count > 0)
{
query +=
$"?{string.Join(
"&",
Contents.QueryParameters.Select(p =>
$"{Uri.EscapeDataString(p.Key)}={(p.Value is null ? "" : Uri.EscapeDataString(p.Value))}"
)
)}";
}
return new Uri(query, UriKind.RelativeOrAbsolute);
}
}
/// <summary>
/// Gets the contents of the Fluent HTTP request.
/// </summary>
public FluentHttpRequestContents Contents
{
get
{
if (field is not null)
{
return field;
}
field = contents ?? new FluentHttpRequestContents();
return field;
}
}
/// <summary>
/// Sends the HTTP request asynchronously.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The HTTP response message.</returns>
public Task<HttpResponseMessage> SendAsync(CancellationToken cancellationToken = default)
{
HttpRequestMessage request = new()
{
Method = Contents.HttpMethod ?? HttpMethod.Get,
RequestUri = Uri,
};
request.Headers.TryAddWithoutValidation("User-Agent", Contents.UserAgent);
request.Headers.TryAddWithoutValidation("Accept", Contents.AcceptedContentType);
request.Headers.TryAddWithoutValidation("Accept-Language", Contents.Culture);
request.Headers.TryAddWithoutValidation("Lang", Contents.Culture);
foreach (KeyValuePair<string, string> header in Contents.Headers ?? [])
{
request.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
if (Contents.Body is not null)
{
string jsonContent = JsonSerializer.Serialize(
Contents.Body,
Contents.JsonOptions ?? DefaultJsonOptions
);
#if NETFRAMEWORK
HttpContent httpContent = new StringContent(jsonContent, Encoding.UTF8, Contents.ContentType);
#else
HttpContent httpContent = new StringContent(
jsonContent,
Encoding.UTF8,
new System.Net.Http.Headers.MediaTypeHeaderValue(Contents.ContentType)
);
#endif
request.Content = httpContent;
}
if (Contents.Timeout.HasValue)
{
using CancellationTokenSource cts = new(Contents.Timeout.Value);
using CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(
cancellationToken,
cts.Token
);
return client.SendAsync(request, linkedCts.Token);
}
return client.SendAsync(request, cancellationToken);
}
}