diff --git a/Client/FluentClient.cs b/Client/FluentClient.cs index 1e6f819..75fae57 100644 --- a/Client/FluentClient.cs +++ b/Client/FluentClient.cs @@ -176,7 +176,11 @@ protected virtual async Task SendImplAsync(IRequest request // dispatch request return await this.BaseClient - .SendAsync(requestMessage, request.CancellationToken) + .SendAsync( + request: requestMessage, + completionOption: request.Options.CompleteWhen ?? HttpCompletionOption.ResponseContentRead, + cancellationToken: request.CancellationToken + ) .ConfigureAwait(false); } diff --git a/Client/FluentClientExtensions.cs b/Client/FluentClientExtensions.cs index 2dc61a6..2911f7c 100644 --- a/Client/FluentClientExtensions.cs +++ b/Client/FluentClientExtensions.cs @@ -189,14 +189,16 @@ public static IClient SetRequestCoordinator(this IClient client, IEnumerableSet default options for all requests. /// The client. - /// Whether HTTP error responses like HTTP 404 should be ignored; else raised as exceptions (or null to leave the option unchanged). - /// Whether to ignore null arguments when the request is dispatched (or null to leave the option unchanged). - public static IClient SetOptions(this IClient client, bool? ignoreHttpErrors = null, bool? ignoreNullArguments = null) + /// Whether HTTP error responses like HTTP 404 should be ignored (true) or raised as exceptions (false). Default false. + /// Whether null arguments in the request body and URL query string should be ignored (true) or sent as-is (false). Default true. + /// When we should stop waiting for the response. For example, setting this to will let you handle the response as soon as the headers are received, before the full response body has been fetched. This only affects getting the ; reading the response body (e.g. using a method like ) will still wait for the request body to be fetched as usual. + public static IClient SetOptions(this IClient client, bool? ignoreHttpErrors = null, bool? ignoreNullArguments = null, HttpCompletionOption? completeWhen = null) { return client.SetOptions(new FluentClientOptions { IgnoreHttpErrors = ignoreHttpErrors, - IgnoreNullArguments = ignoreNullArguments + IgnoreNullArguments = ignoreNullArguments, + CompleteWhen = completeWhen }); } @@ -272,14 +274,16 @@ public static IRequest WithRequestCoordinator(this IRequest request, IEnumerable /// Set options for this request. /// The request. - /// Whether HTTP error responses like HTTP 404 should be ignored; else raised as exceptions (or null to leave the option unchanged). - /// Whether to ignore null arguments when the request is dispatched (or null to leave the option unchanged). - public static IRequest WithOptions(this IRequest request, bool? ignoreHttpErrors = null, bool? ignoreNullArguments = null) + /// Whether HTTP error responses like HTTP 404 should be ignored (true) or raised as exceptions (false). Default false. + /// Whether null arguments in the request body and URL query string should be ignored (true) or sent as-is (false). Default true. + /// When we should stop waiting for the response. For example, setting this to will let you handle the response as soon as the headers are received, before the full response body has been fetched. This only affects getting the ; reading the response body (e.g. using a method like ) will still wait for the request body to be fetched as usual. + public static IRequest WithOptions(this IRequest request, bool? ignoreHttpErrors = null, bool? ignoreNullArguments = null, HttpCompletionOption? completeWhen = null) { return request.WithOptions(new RequestOptions { IgnoreHttpErrors = ignoreHttpErrors, - IgnoreNullArguments = ignoreNullArguments + IgnoreNullArguments = ignoreNullArguments, + CompleteWhen = completeWhen }); } diff --git a/Client/FluentClientOptions.cs b/Client/FluentClientOptions.cs index 73b35d6..2106586 100644 --- a/Client/FluentClientOptions.cs +++ b/Client/FluentClientOptions.cs @@ -1,3 +1,5 @@ +using System.Net.Http; + namespace Pathoschild.Http.Client { /// Options for the fluent client. @@ -6,12 +8,18 @@ public class FluentClientOptions /********* ** Accessors *********/ - /// Whether to ignore null arguments when the request is dispatched. Default true if not specified. + /// Whether null arguments in the request body and URL query string should be ignored (true) or sent as-is (false). Default true. public bool? IgnoreNullArguments { get; set; } - /// Whether HTTP error responses (e.g. HTTP 404) should be ignored (else raised as exceptions). Default false if not specified. + /// Whether HTTP error responses like HTTP 404 should be ignored (true) or raised as exceptions (false). Default false. public bool? IgnoreHttpErrors { get; set; } + /// + /// When we should stop waiting for the response. For example, setting this to will let you handle the response as soon as the headers are received, before the full response body has been fetched. This only affects getting the ; reading the response body (e.g. using a method like ) will still wait for the request body to be fetched as usual. + /// Default if not specified. + /// + public HttpCompletionOption? CompleteWhen { get; set; } + /********* ** Public methods @@ -22,7 +30,8 @@ internal RequestOptions ToRequestOptions() return new RequestOptions { IgnoreHttpErrors = this.IgnoreHttpErrors, - IgnoreNullArguments = this.IgnoreNullArguments + IgnoreNullArguments = this.IgnoreNullArguments, + CompleteWhen = this.CompleteWhen, }; } @@ -32,6 +41,7 @@ internal void MergeFrom(FluentClientOptions? options) { this.IgnoreNullArguments = options?.IgnoreNullArguments ?? this.IgnoreNullArguments; this.IgnoreHttpErrors = options?.IgnoreHttpErrors ?? this.IgnoreHttpErrors; + this.CompleteWhen = options?.CompleteWhen ?? this.CompleteWhen; } } } diff --git a/Client/RequestOptions.cs b/Client/RequestOptions.cs index 6a7d8af..d6e29f8 100644 --- a/Client/RequestOptions.cs +++ b/Client/RequestOptions.cs @@ -1,3 +1,5 @@ +using System.Net.Http; + namespace Pathoschild.Http.Client { /// Options for a request. @@ -6,12 +8,17 @@ public class RequestOptions /********* ** Accessors *********/ - /// Whether to ignore null arguments when the request is dispatched. Default true if not specified. + /// Whether null arguments in the request body and URL query string should be ignored (true) or sent as-is (false). Default true. public bool? IgnoreNullArguments { get; set; } - /// Whether HTTP error responses (e.g. HTTP 404) should be ignored (else raised as exceptions). Default false if not specified. + /// Whether HTTP error responses like HTTP 404 should be ignored (true) or raised as exceptions (false). Default false. public bool? IgnoreHttpErrors { get; set; } + /// + /// When we should stop waiting for the response. For example, setting this to will let you handle the response as soon as the headers are received, before the full response body has been fetched. This only affects getting the ; reading the response body (e.g. using a method like ) will still wait for the request body to be fetched as usual. + /// Default if not specified. + /// + public HttpCompletionOption? CompleteWhen { get; set; } /********* ** Public methods @@ -22,6 +29,7 @@ internal void MergeFrom(RequestOptions? options) { this.IgnoreNullArguments = options?.IgnoreNullArguments ?? this.IgnoreNullArguments; this.IgnoreHttpErrors = options?.IgnoreHttpErrors ?? this.IgnoreHttpErrors; + this.CompleteWhen = options?.CompleteWhen ?? this.CompleteWhen; } } } diff --git a/README.md b/README.md index 64a7a68..731f63b 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,29 @@ If you don't want that, you can... * [use your own error filter](#custom-filters). ## Advanced features +### Request options +You can customize the request/response flow using a few built-in options. + +You can set an option for one request: +```c# +IResponse response = await client + .GetAsync("items") + .WithOptions(ignoreHttpErrors: true); +``` + +Or for all requests: +```c# +client.SetOptions(ignoreHttpErrors: true); +``` + +The available options are: + +option | default | effect +--------------------- | ------- | ------ +`ignoreHttpErrors` | `false` | Whether HTTP error responses like HTTP 404 should be ignored (`true`) or raised as exceptions (`false`). +`ignoreNullArguments` | `true` | Whether null arguments in the request body and URL query string should be ignored (`true`) or sent as-is (`false`). +`completeWhen` | `ResponseContentRead` | When we should stop waiting for the response. For example, setting this to `ResponseHeadersRead` will let you handle the response as soon as the headers are received, before the full response body has been fetched. This only affects getting the `IResponse`; reading the response body (e.g. using a method like `IResponse.As()`) will still wait for the request body to be fetched as usual. + ### Response metadata The previous examples parse the response directly, but sometimes you want to peek at the HTTP metadata: