From 0c04bd1e0d47d546d1922433ba959167a139610a Mon Sep 17 00:00:00 2001 From: jericho Date: Sun, 21 Apr 2024 08:39:30 -0400 Subject: [PATCH 1/4] Add option to indicate that we want the response to be streamed --- Client/FluentClient.cs | 9 ++++++++- Client/FluentClientExtensions.cs | 6 ++++-- Client/FluentClientOptions.cs | 11 ++++++++++- Client/RequestOptions.cs | 7 +++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Client/FluentClient.cs b/Client/FluentClient.cs index 1e6f819..d451bf0 100644 --- a/Client/FluentClient.cs +++ b/Client/FluentClient.cs @@ -174,9 +174,16 @@ protected virtual async Task SendImplAsync(IRequest request // clone request (to avoid issues when resending messages) HttpRequestMessage requestMessage = await request.Message.CloneAsync(request.CancellationToken).ConfigureAwait(false); + // determine if the response should be streamed or not + HttpCompletionOption httpCompletionOption = request.Options.StreamResponse switch + { + true => HttpCompletionOption.ResponseHeadersRead, + false or null => HttpCompletionOption.ResponseContentRead + }; + // dispatch request return await this.BaseClient - .SendAsync(requestMessage, request.CancellationToken) + .SendAsync(requestMessage, httpCompletionOption, request.CancellationToken) .ConfigureAwait(false); } diff --git a/Client/FluentClientExtensions.cs b/Client/FluentClientExtensions.cs index 2dc61a6..2ae73c6 100644 --- a/Client/FluentClientExtensions.cs +++ b/Client/FluentClientExtensions.cs @@ -274,12 +274,14 @@ public static IRequest WithRequestCoordinator(this IRequest request, IEnumerable /// 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 the response should be streamed or . + public static IRequest WithOptions(this IRequest request, bool? ignoreHttpErrors = null, bool? ignoreNullArguments = null, bool? streamResponse = null) { return request.WithOptions(new RequestOptions { IgnoreHttpErrors = ignoreHttpErrors, - IgnoreNullArguments = ignoreNullArguments + IgnoreNullArguments = ignoreNullArguments, + StreamResponse = streamResponse }); } diff --git a/Client/FluentClientOptions.cs b/Client/FluentClientOptions.cs index 73b35d6..83feab0 100644 --- a/Client/FluentClientOptions.cs +++ b/Client/FluentClientOptions.cs @@ -12,6 +12,13 @@ public class FluentClientOptions /// Whether HTTP error responses (e.g. HTTP 404) should be ignored (else raised as exceptions). Default false if not specified. public bool? IgnoreHttpErrors { get; set; } + /// + /// When false, indicates that the request should complete after reading the entire response including the content. + /// When true, indicates that the request should complete as soon as a response is available and headers are read (the content is not read yet). + /// Default false if not specified. + /// + public bool? StreamResponse { get; set; } + /********* ** Public methods @@ -22,7 +29,8 @@ internal RequestOptions ToRequestOptions() return new RequestOptions { IgnoreHttpErrors = this.IgnoreHttpErrors, - IgnoreNullArguments = this.IgnoreNullArguments + IgnoreNullArguments = this.IgnoreNullArguments, + StreamResponse = this.StreamResponse, }; } @@ -32,6 +40,7 @@ internal void MergeFrom(FluentClientOptions? options) { this.IgnoreNullArguments = options?.IgnoreNullArguments ?? this.IgnoreNullArguments; this.IgnoreHttpErrors = options?.IgnoreHttpErrors ?? this.IgnoreHttpErrors; + this.StreamResponse = options?.StreamResponse ?? this.StreamResponse; } } } diff --git a/Client/RequestOptions.cs b/Client/RequestOptions.cs index 6a7d8af..4bef84a 100644 --- a/Client/RequestOptions.cs +++ b/Client/RequestOptions.cs @@ -12,6 +12,12 @@ public class RequestOptions /// Whether HTTP error responses (e.g. HTTP 404) should be ignored (else raised as exceptions). Default false if not specified. public bool? IgnoreHttpErrors { get; set; } + /// + /// When false, indicates that the request should complete after reading the entire response including the content. + /// When true, indicates that the request should complete as soon as a response is available and headers are read (the content is not read yet). + /// Default false if not specified. + /// + public bool? StreamResponse { get; set; } /********* ** Public methods @@ -22,6 +28,7 @@ internal void MergeFrom(RequestOptions? options) { this.IgnoreNullArguments = options?.IgnoreNullArguments ?? this.IgnoreNullArguments; this.IgnoreHttpErrors = options?.IgnoreHttpErrors ?? this.IgnoreHttpErrors; + this.StreamResponse = options?.StreamResponse ?? this.StreamResponse; } } } From 442d14fc7243be7c5fec934dd5243b52e5923930 Mon Sep 17 00:00:00 2001 From: jericho Date: Sat, 11 May 2024 21:46:22 -0400 Subject: [PATCH 2/4] The completion option should be enum rather than bool --- Client/FluentClient.cs | 8 ++------ Client/FluentClientExtensions.cs | 6 +++--- Client/FluentClientOptions.cs | 11 ++++++----- Client/RequestOptions.cs | 8 +++++--- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Client/FluentClient.cs b/Client/FluentClient.cs index d451bf0..4593427 100644 --- a/Client/FluentClient.cs +++ b/Client/FluentClient.cs @@ -175,15 +175,11 @@ protected virtual async Task SendImplAsync(IRequest request HttpRequestMessage requestMessage = await request.Message.CloneAsync(request.CancellationToken).ConfigureAwait(false); // determine if the response should be streamed or not - HttpCompletionOption httpCompletionOption = request.Options.StreamResponse switch - { - true => HttpCompletionOption.ResponseHeadersRead, - false or null => HttpCompletionOption.ResponseContentRead - }; + var completionOption = request.Options.CompletionOption ?? HttpCompletionOption.ResponseContentRead; // dispatch request return await this.BaseClient - .SendAsync(requestMessage, httpCompletionOption, request.CancellationToken) + .SendAsync(requestMessage, completionOption, request.CancellationToken) .ConfigureAwait(false); } diff --git a/Client/FluentClientExtensions.cs b/Client/FluentClientExtensions.cs index 2ae73c6..47da10d 100644 --- a/Client/FluentClientExtensions.cs +++ b/Client/FluentClientExtensions.cs @@ -274,14 +274,14 @@ public static IRequest WithRequestCoordinator(this IRequest request, IEnumerable /// 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). - /// Whether the response should be streamed or . - public static IRequest WithOptions(this IRequest request, bool? ignoreHttpErrors = null, bool? ignoreNullArguments = null, bool? streamResponse = null) + /// When the operation should complete (as soon as a response is available or after reasing the whole response content). + public static IRequest WithOptions(this IRequest request, bool? ignoreHttpErrors = null, bool? ignoreNullArguments = null, HttpCompletionOption? completionOption = null) { return request.WithOptions(new RequestOptions { IgnoreHttpErrors = ignoreHttpErrors, IgnoreNullArguments = ignoreNullArguments, - StreamResponse = streamResponse + CompletionOption = completionOption }); } diff --git a/Client/FluentClientOptions.cs b/Client/FluentClientOptions.cs index 83feab0..b471782 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. @@ -13,11 +15,10 @@ public class FluentClientOptions public bool? IgnoreHttpErrors { get; set; } /// - /// When false, indicates that the request should complete after reading the entire response including the content. - /// When true, indicates that the request should complete as soon as a response is available and headers are read (the content is not read yet). + /// When the operation should complete (as soon as a response is available or after reasing the whole response content). /// Default false if not specified. /// - public bool? StreamResponse { get; set; } + public HttpCompletionOption? CompletionOption { get; set; } /********* @@ -30,7 +31,7 @@ internal RequestOptions ToRequestOptions() { IgnoreHttpErrors = this.IgnoreHttpErrors, IgnoreNullArguments = this.IgnoreNullArguments, - StreamResponse = this.StreamResponse, + CompletionOption = this.CompletionOption, }; } @@ -40,7 +41,7 @@ internal void MergeFrom(FluentClientOptions? options) { this.IgnoreNullArguments = options?.IgnoreNullArguments ?? this.IgnoreNullArguments; this.IgnoreHttpErrors = options?.IgnoreHttpErrors ?? this.IgnoreHttpErrors; - this.StreamResponse = options?.StreamResponse ?? this.StreamResponse; + this.CompletionOption = options?.CompletionOption ?? this.CompletionOption; } } } diff --git a/Client/RequestOptions.cs b/Client/RequestOptions.cs index 4bef84a..78366d5 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. @@ -15,9 +17,9 @@ public class RequestOptions /// /// When false, indicates that the request should complete after reading the entire response including the content. /// When true, indicates that the request should complete as soon as a response is available and headers are read (the content is not read yet). - /// Default false if not specified. + /// Default HttpCompletionOption.ResponseContentRead if not specified. /// - public bool? StreamResponse { get; set; } + public HttpCompletionOption? CompletionOption { get; set; } /********* ** Public methods @@ -28,7 +30,7 @@ internal void MergeFrom(RequestOptions? options) { this.IgnoreNullArguments = options?.IgnoreNullArguments ?? this.IgnoreNullArguments; this.IgnoreHttpErrors = options?.IgnoreHttpErrors ?? this.IgnoreHttpErrors; - this.StreamResponse = options?.StreamResponse ?? this.StreamResponse; + this.CompletionOption = options?.CompletionOption ?? this.CompletionOption; } } } From 8f600973daac71efcfb17dec74a1f622d61c0e97 Mon Sep 17 00:00:00 2001 From: jericho Date: Sat, 11 May 2024 21:48:21 -0400 Subject: [PATCH 3/4] Fix XML comments --- Client/FluentClientOptions.cs | 2 +- Client/RequestOptions.cs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Client/FluentClientOptions.cs b/Client/FluentClientOptions.cs index b471782..251ed5a 100644 --- a/Client/FluentClientOptions.cs +++ b/Client/FluentClientOptions.cs @@ -16,7 +16,7 @@ public class FluentClientOptions /// /// When the operation should complete (as soon as a response is available or after reasing the whole response content). - /// Default false if not specified. + /// Default HttpCompletionOption.ResponseContentRead if not specified. /// public HttpCompletionOption? CompletionOption { get; set; } diff --git a/Client/RequestOptions.cs b/Client/RequestOptions.cs index 78366d5..7670906 100644 --- a/Client/RequestOptions.cs +++ b/Client/RequestOptions.cs @@ -15,8 +15,7 @@ public class RequestOptions public bool? IgnoreHttpErrors { get; set; } /// - /// When false, indicates that the request should complete after reading the entire response including the content. - /// When true, indicates that the request should complete as soon as a response is available and headers are read (the content is not read yet). + /// When the operation should complete (as soon as a response is available or after reasing the whole response content). /// Default HttpCompletionOption.ResponseContentRead if not specified. /// public HttpCompletionOption? CompletionOption { get; set; } From 66627dd83b6a260e36cf48d35c700a1b62978465 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 11 May 2024 23:16:56 -0400 Subject: [PATCH 4/4] add option to client.SetOptions, tweak naming, & expand docs --- Client/FluentClient.cs | 9 +++++---- Client/FluentClientExtensions.cs | 20 +++++++++++--------- Client/FluentClientOptions.cs | 14 +++++++------- Client/RequestOptions.cs | 12 ++++++------ README.md | 23 +++++++++++++++++++++++ 5 files changed, 52 insertions(+), 26 deletions(-) diff --git a/Client/FluentClient.cs b/Client/FluentClient.cs index 4593427..75fae57 100644 --- a/Client/FluentClient.cs +++ b/Client/FluentClient.cs @@ -174,12 +174,13 @@ protected virtual async Task SendImplAsync(IRequest request // clone request (to avoid issues when resending messages) HttpRequestMessage requestMessage = await request.Message.CloneAsync(request.CancellationToken).ConfigureAwait(false); - // determine if the response should be streamed or not - var completionOption = request.Options.CompletionOption ?? HttpCompletionOption.ResponseContentRead; - // dispatch request return await this.BaseClient - .SendAsync(requestMessage, completionOption, 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 47da10d..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,16 +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). - /// When the operation should complete (as soon as a response is available or after reasing the whole response content). - public static IRequest WithOptions(this IRequest request, bool? ignoreHttpErrors = null, bool? ignoreNullArguments = null, HttpCompletionOption? completionOption = 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, - CompletionOption = completionOption + CompleteWhen = completeWhen }); } diff --git a/Client/FluentClientOptions.cs b/Client/FluentClientOptions.cs index 251ed5a..2106586 100644 --- a/Client/FluentClientOptions.cs +++ b/Client/FluentClientOptions.cs @@ -8,17 +8,17 @@ 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 the operation should complete (as soon as a response is available or after reasing the whole response content). - /// Default HttpCompletionOption.ResponseContentRead if not specified. + /// 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? CompletionOption { get; set; } + public HttpCompletionOption? CompleteWhen { get; set; } /********* @@ -31,7 +31,7 @@ internal RequestOptions ToRequestOptions() { IgnoreHttpErrors = this.IgnoreHttpErrors, IgnoreNullArguments = this.IgnoreNullArguments, - CompletionOption = this.CompletionOption, + CompleteWhen = this.CompleteWhen, }; } @@ -41,7 +41,7 @@ internal void MergeFrom(FluentClientOptions? options) { this.IgnoreNullArguments = options?.IgnoreNullArguments ?? this.IgnoreNullArguments; this.IgnoreHttpErrors = options?.IgnoreHttpErrors ?? this.IgnoreHttpErrors; - this.CompletionOption = options?.CompletionOption ?? this.CompletionOption; + this.CompleteWhen = options?.CompleteWhen ?? this.CompleteWhen; } } } diff --git a/Client/RequestOptions.cs b/Client/RequestOptions.cs index 7670906..d6e29f8 100644 --- a/Client/RequestOptions.cs +++ b/Client/RequestOptions.cs @@ -8,17 +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 the operation should complete (as soon as a response is available or after reasing the whole response content). - /// Default HttpCompletionOption.ResponseContentRead if not specified. + /// 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? CompletionOption { get; set; } + public HttpCompletionOption? CompleteWhen { get; set; } /********* ** Public methods @@ -29,7 +29,7 @@ internal void MergeFrom(RequestOptions? options) { this.IgnoreNullArguments = options?.IgnoreNullArguments ?? this.IgnoreNullArguments; this.IgnoreHttpErrors = options?.IgnoreHttpErrors ?? this.IgnoreHttpErrors; - this.CompletionOption = options?.CompletionOption ?? this.CompletionOption; + 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: