forked from Pathoschild/FluentHttpClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.cs
More file actions
319 lines (276 loc) · 14.4 KB
/
Request.cs
File metadata and controls
319 lines (276 loc) · 14.4 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Pathoschild.Http.Client.Extensibility;
using Pathoschild.Http.Client.Retry;
namespace Pathoschild.Http.Client.Internal
{
/// <summary>Builds and dispatches an asynchronous HTTP request, and asynchronously parses the response.</summary>
public sealed class Request : IRequest
{
/*********
** Fields
*********/
/// <summary>Dispatcher that executes the request.</summary>
private readonly Func<IRequest, Task<HttpResponseMessage>> Dispatcher;
/*********
** Accessors
*********/
/// <summary>The underlying HTTP request message.</summary>
public HttpRequestMessage Message { get; }
/// <summary>The formatters used for serializing and deserializing message bodies.</summary>
public MediaTypeFormatterCollection Formatters { get; }
/// <summary>Middleware classes which can intercept and modify HTTP requests and responses.</summary>
public ICollection<IHttpFilter> Filters { get; }
/// <summary>The optional token used to cancel async operations.</summary>
public CancellationToken CancellationToken { get; private set; }
/// <summary>The request coordinator.</summary>
public IRequestCoordinator RequestCoordinator { get; private set; }
/// <summary>The request options.</summary>
public RequestOptions Options { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="message">The underlying HTTP request message.</param>
/// <param name="formatters">The formatters used for serializing and deserializing message bodies.</param>
/// <param name="dispatcher">Executes an HTTP request.</param>
/// <param name="filters">Middleware classes which can intercept and modify HTTP requests and responses.</param>
public Request(HttpRequestMessage message, MediaTypeFormatterCollection formatters, Func<IRequest, Task<HttpResponseMessage>> dispatcher, ICollection<IHttpFilter> filters)
{
this.Message = message;
this.Formatters = formatters;
this.Dispatcher = dispatcher;
this.Filters = filters;
this.CancellationToken = CancellationToken.None;
this.RequestCoordinator = null;
this.Options = new RequestOptions();
}
/***
** Build request
***/
/// <summary>Set the body content of the HTTP request.</summary>
/// <param name="body">The formatted HTTP body content.</param>
/// <returns>Returns the request builder for chaining.</returns>
[Obsolete("Will be removed in 4.0. Use `" + nameof(WithBody) + "` instead.")]
public IRequest WithBodyContent(HttpContent body)
{
return this.WithBody(body);
}
/// <summary>Set the body content of the HTTP request.</summary>
/// <param name="bodyBuilder">The HTTP body builder.</param>
/// <returns>Returns the request builder for chaining.</returns>
public IRequest WithBody(Func<IBodyBuilder, HttpContent> bodyBuilder)
{
this.Message.Content = bodyBuilder(new BodyBuilder(this));
return this;
}
/// <summary>Set an HTTP header.</summary>
/// <param name="key">The key of the HTTP header.</param>
/// <param name="value">The value of the HTTP header.</param>
/// <returns>Returns the request builder for chaining.</returns>
public IRequest WithHeader(string key, string value)
{
this.Message.Headers.Add(key, value);
return this;
}
/// <summary>Add an authentication header.</summary>
/// <param name="scheme">The scheme to use for authorization. e.g.: "Basic", "Bearer".</param>
/// <param name="parameter">The credentials containing the authentication information.</param>
public IRequest WithAuthentication(string scheme, string parameter)
{
this.Message.Headers.Authorization = new AuthenticationHeaderValue(scheme, parameter);
return this;
}
/// <summary>Add an HTTP query string argument.</summary>
/// <param name="key">The key of the query argument.</param>
/// <param name="value">The value of the query argument.</param>
/// <returns>Returns the request builder for chaining.</returns>
public IRequest WithArgument(string key, object value)
{
this.Message.RequestUri = this.Message.RequestUri.WithArguments(this.Options.IgnoreNullArguments ?? false, new KeyValuePair<string, object>(key, value));
return this;
}
/// <summary>Add HTTP query string arguments.</summary>
/// <param name="arguments">The arguments to add.</param>
/// <returns>Returns the request builder for chaining.</returns>
/// <example><code>client.WithArguments(new[] { new KeyValuePair<string, string>("genre", "drama"), new KeyValuePair<string, int>("genre", "comedy") })</code></example>
public IRequest WithArguments<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> arguments)
{
if (arguments == null)
return this;
KeyValuePair<string, object>[] args = (
from arg in arguments
let key = arg.Key?.ToString()
where !string.IsNullOrWhiteSpace(key)
select new KeyValuePair<string, object>(key, arg.Value)
).ToArray();
this.Message.RequestUri = this.Message.RequestUri.WithArguments(this.Options.IgnoreNullArguments ?? false, args);
return this;
}
/// <summary>Add HTTP query string arguments.</summary>
/// <param name="arguments">An anonymous object where the property names and values are used.</param>
/// <returns>Returns the request builder for chaining.</returns>
/// <example><code>client.WithArguments(new { id = 14, name = "Joe" })</code></example>
public IRequest WithArguments(object arguments)
{
if (arguments == null)
return this;
KeyValuePair<string, object>[] args = arguments.GetKeyValueArguments().ToArray();
this.Message.RequestUri = this.Message.RequestUri.WithArguments(this.Options.IgnoreNullArguments ?? false, args);
return this;
}
/// <summary>Customize the underlying HTTP request message.</summary>
/// <param name="request">The HTTP request message.</param>
/// <returns>Returns the request builder for chaining.</returns>
public IRequest WithCustom(Action<HttpRequestMessage> request)
{
request(this.Message);
return this;
}
/// <summary>Specify the token that can be used to cancel the async operation.</summary>
/// <param name="cancellationToken">The cancellationtoken.</param>
/// <returns>Returns the request builder for chaining.</returns>
public IRequest WithCancellationToken(CancellationToken cancellationToken)
{
this.CancellationToken = cancellationToken;
return this;
}
/// <summary>Set whether HTTP errors (e.g. HTTP 500) should be raised an exceptions for this request.</summary>
/// <param name="enabled">Whether to raise HTTP errors as exceptions.</param>
[Obsolete("Will be removed in 4.0. Use `" + nameof(WithOptions) + "` instead.")]
public IRequest WithHttpErrorAsException(bool enabled)
{
return this.WithOptions(ignoreHttpErrors: !enabled);
}
/// <summary>Set options for this request.</summary>
/// <param name="options">The options to set. (Fields set to <c>null</c> won't change the current value.)</param>
public IRequest WithOptions(RequestOptions options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));
if (options.IgnoreHttpErrors.HasValue)
this.Options.IgnoreHttpErrors = options.IgnoreHttpErrors.Value;
if (options.IgnoreNullArguments.HasValue)
this.Options.IgnoreNullArguments = options.IgnoreNullArguments.Value;
return this;
}
/// <summary>Specify the request coordinator for this request.</summary>
/// <param name="requestCoordinator">The request coordinator</param>
public IRequest WithRequestCoordinator(IRequestCoordinator requestCoordinator)
{
this.RequestCoordinator = requestCoordinator;
return this;
}
/***
** Retrieve response
***/
/// <summary>Get an object that waits for the completion of the request. This enables support for the <c>await</c> keyword.</summary>
/// <example>
/// <code>await client.PostAsync("api/ideas", idea);</code>
/// <code>await client.GetAsync("api/ideas").AsString();</code>
/// </example>
public TaskAwaiter<IResponse> GetAwaiter()
{
async Task<IResponse> Waiter() => await this.Execute().ConfigureAwait(false);
return Waiter().GetAwaiter();
}
/// <summary>Asynchronously retrieve the HTTP response.</summary>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public Task<IResponse> AsResponse()
{
return this.Execute();
}
/// <summary>Asynchronously retrieve the HTTP response.</summary>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public async Task<HttpResponseMessage> AsMessage()
{
IResponse response = await this.AsResponse().ConfigureAwait(false);
return response.Message;
}
/// <summary>Asynchronously retrieve the response body as a deserialized model.</summary>
/// <typeparam name="T">The response model to deserialize into.</typeparam>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public async Task<T> As<T>()
{
IResponse response = await this.AsResponse().ConfigureAwait(false);
return await response.As<T>().ConfigureAwait(false);
}
/// <summary>Asynchronously retrieve the response body as a list of deserialized models.</summary>
/// <typeparam name="T">The response model to deserialize into.</typeparam>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public Task<T[]> AsArray<T>()
{
return this.As<T[]>();
}
/// <summary>Asynchronously retrieve the response body as an array of <see cref="byte"/>.</summary>
/// <returns>Returns the response body, or <c>null</c> if the response has no body.</returns>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public async Task<byte[]> AsByteArray()
{
IResponse response = await this.AsResponse().ConfigureAwait(false);
return await response.AsByteArray().ConfigureAwait(false);
}
/// <summary>Asynchronously retrieve the response body as a <see cref="string"/>.</summary>
/// <returns>Returns the response body, or <c>null</c> if the response has no body.</returns>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public async Task<string> AsString()
{
IResponse response = await this.AsResponse().ConfigureAwait(false);
return await response.AsString().ConfigureAwait(false);
}
/// <summary>Asynchronously retrieve the response body as a <see cref="Stream"/>.</summary>
/// <returns>Returns the response body, or <c>null</c> if the response has no body.</returns>
/// <exception cref="ApiException">An error occurred processing the response.</exception>
public async Task<Stream> AsStream()
{
IResponse response = await this.AsResponse().ConfigureAwait(false);
return await response.AsStream().ConfigureAwait(false);
}
/// <summary>Get a raw JSON representation of the response, which can also be accessed as a <c>dynamic</c> value.</summary>
public async Task<JToken> AsRawJson()
{
IResponse response = await this.AsResponse().ConfigureAwait(false);
return await response.AsRawJson().ConfigureAwait(false);
}
/// <summary>Get a raw JSON object representation of the response, which can also be accessed as a <c>dynamic</c> value.</summary>
public async Task<JObject> AsRawJsonObject()
{
IResponse response = await this.AsResponse().ConfigureAwait(false);
return await response.AsRawJsonObject().ConfigureAwait(false);
}
/// <summary>Get a raw JSON array representation of the response, which can also be accessed as a <c>dynamic</c> value.</summary>
public async Task<JArray> AsRawJsonArray()
{
IResponse response = await this.AsResponse().ConfigureAwait(false);
return await response.AsRawJsonArray().ConfigureAwait(false);
}
/*********
** Private methods
*********/
/// <summary>Execute the HTTP request and fetch the response.</summary>
private async Task<IResponse> Execute()
{
// apply request filters
foreach (IHttpFilter filter in this.Filters)
filter.OnRequest(this);
// execute the request
HttpResponseMessage responseMessage = this.RequestCoordinator != null
? await this.RequestCoordinator.ExecuteAsync(this, this.Dispatcher).ConfigureAwait(false)
: await this.Dispatcher(this).ConfigureAwait(false);
IResponse response = new Response(responseMessage, this.Formatters);
// apply response filters
foreach (IHttpFilter filter in this.Filters)
filter.OnResponse(response, !(this.Options.IgnoreHttpErrors ?? false));
return response;
}
}
}