-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFluentHeaderExtensions.cs
More file actions
129 lines (110 loc) · 4.31 KB
/
Copy pathFluentHeaderExtensions.cs
File metadata and controls
129 lines (110 loc) · 4.31 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
namespace FluentHttpClient;
/// <summary>
/// Fluent extension methods for adding headers to an <see cref="HttpRequestBuilder"/> instances.
/// </summary>
public static class FluentHeaderExtensions
{
private static readonly HashSet<string> _reservedHeaders =
new(StringComparer.OrdinalIgnoreCase)
{
"Host",
"Content-Length",
"Transfer-Encoding"
};
/// <summary>
/// Adds the specified header and its value to the request.
/// </summary>
/// <param name="builder"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static HttpRequestBuilder WithHeader(this HttpRequestBuilder builder, string key, string value)
{
Guard.AgainstNull(key, nameof(key));
Guard.AgainstNull(value, nameof(value));
if (_reservedHeaders.Contains(key))
{
throw new ArgumentException(
$"Header '{key}' is managed by HttpClient/HttpContent and cannot be set using FluentHttpClient. " +
"Configure the request URI or content instead.",
nameof(key));
}
builder.HeaderConfigurators.Add(target =>
target.TryAddWithoutValidation(key, value));
return builder;
}
/// <summary>
/// Adds the specified header and its values to the request.
/// </summary>
/// <param name="builder"></param>
/// <param name="key"></param>
/// <param name="values"></param>
public static HttpRequestBuilder WithHeader(this HttpRequestBuilder builder, string key, IEnumerable<string> values)
{
Guard.AgainstNull(key, nameof(key));
Guard.AgainstNull(values, nameof(values));
if (_reservedHeaders.Contains(key))
{
throw new ArgumentException(
$"Header '{key}' is managed by HttpClient/HttpContent and cannot be set using FluentHttpClient. " +
"Configure the request URI or content instead.",
nameof(key));
}
builder.HeaderConfigurators.Add(target =>
target.TryAddWithoutValidation(key, values));
return builder;
}
/// <summary>
/// Adds the specified headers and their values to the request.
/// </summary>
/// <param name="builder"></param>
/// <param name="headers"></param>
public static HttpRequestBuilder WithHeaders(this HttpRequestBuilder builder, IEnumerable<KeyValuePair<string, string>> headers)
{
Guard.AgainstNull(headers, nameof(headers));
builder.HeaderConfigurators.Add(target =>
{
foreach (var header in headers)
{
Guard.AgainstNull(header.Key, "key");
Guard.AgainstNull(header.Value, "value");
if (_reservedHeaders.Contains(header.Key))
{
throw new ArgumentException(
$"Header '{header.Key}' is managed by HttpClient/HttpContent and cannot be set using FluentHttpClient. " +
"Configure the request URI or content instead.",
"key");
}
target.TryAddWithoutValidation(header.Key, header.Value);
}
});
return builder;
}
/// <summary>
/// Adds the specified headers and their multiple values to the request.
/// </summary>
/// <param name="builder"></param>
/// <param name="headers"></param>
public static HttpRequestBuilder WithHeaders(
this HttpRequestBuilder builder,
IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers)
{
Guard.AgainstNull(headers, nameof(headers));
builder.HeaderConfigurators.Add(target =>
{
foreach (var header in headers)
{
Guard.AgainstNull(header.Key, "key");
Guard.AgainstNull(header.Value, "values");
if (_reservedHeaders.Contains(header.Key))
{
throw new ArgumentException(
$"Header '{header.Key}' is managed by HttpClient/HttpContent and cannot be set using FluentHttpClient. " +
"Configure the request URI or content instead.",
"key");
}
target.TryAddWithoutValidation(header.Key, header.Value);
}
});
return builder;
}
}