Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Client/FluentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ protected virtual async Task<HttpResponseMessage> 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);
}

Expand Down
20 changes: 12 additions & 8 deletions Client/FluentClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,16 @@ public static IClient SetRequestCoordinator(this IClient client, IEnumerable<IRe

/// <summary>Set default options for all requests.</summary>
/// <param name="client">The client.</param>
/// <param name="ignoreHttpErrors">Whether HTTP error responses like HTTP 404 should be ignored; else raised as exceptions (or <c>null</c> to leave the option unchanged).</param>
/// <param name="ignoreNullArguments">Whether to ignore null arguments when the request is dispatched (or <c>null</c> to leave the option unchanged).</param>
public static IClient SetOptions(this IClient client, bool? ignoreHttpErrors = null, bool? ignoreNullArguments = null)
/// <param name="ignoreHttpErrors">Whether HTTP error responses like HTTP 404 should be ignored (<c>true</c>) or raised as exceptions (<c>false</c>). Default <c>false</c>.</param>
/// <param name="ignoreNullArguments">Whether null arguments in the request body and URL query string should be ignored (<c>true</c>) or sent as-is (<c>false</c>). Default <c>true</c>.</param>
/// <param name="completeWhen">When we should stop waiting for the response. For example, setting this to <see cref="HttpCompletionOption.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 <see cref="IResponse"/>; reading the response body (e.g. using a method like <see cref="IResponse.As{T}"/>) will still wait for the request body to be fetched as usual.</param>
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
});
}

Expand Down Expand Up @@ -272,14 +274,16 @@ public static IRequest WithRequestCoordinator(this IRequest request, IEnumerable

/// <summary>Set options for this request.</summary>
/// <param name="request">The request.</param>
/// <param name="ignoreHttpErrors">Whether HTTP error responses like HTTP 404 should be ignored; else raised as exceptions (or <c>null</c> to leave the option unchanged).</param>
/// <param name="ignoreNullArguments">Whether to ignore null arguments when the request is dispatched (or <c>null</c> to leave the option unchanged).</param>
public static IRequest WithOptions(this IRequest request, bool? ignoreHttpErrors = null, bool? ignoreNullArguments = null)
/// <param name="ignoreHttpErrors">Whether HTTP error responses like HTTP 404 should be ignored (<c>true</c>) or raised as exceptions (<c>false</c>). Default <c>false</c>.</param>
/// <param name="ignoreNullArguments">Whether null arguments in the request body and URL query string should be ignored (<c>true</c>) or sent as-is (<c>false</c>). Default <c>true</c>.</param>
/// <param name="completeWhen">When we should stop waiting for the response. For example, setting this to <see cref="HttpCompletionOption.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 <see cref="IResponse"/>; reading the response body (e.g. using a method like <see cref="IResponse.As{T}"/>) will still wait for the request body to be fetched as usual.</param>
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
});
}

Expand Down
16 changes: 13 additions & 3 deletions Client/FluentClientOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Net.Http;

namespace Pathoschild.Http.Client
{
/// <summary>Options for the fluent client.</summary>
Expand All @@ -6,12 +8,18 @@ public class FluentClientOptions
/*********
** Accessors
*********/
/// <summary>Whether to ignore null arguments when the request is dispatched. Default true if not specified.</summary>
/// <summary>Whether null arguments in the request body and URL query string should be ignored (<c>true</c>) or sent as-is (<c>false</c>). Default <c>true</c>.</summary>
public bool? IgnoreNullArguments { get; set; }

/// <summary>Whether HTTP error responses (e.g. HTTP 404) should be ignored (else raised as exceptions). Default false if not specified.</summary>
/// <summary>Whether HTTP error responses like HTTP 404 should be ignored (<c>true</c>) or raised as exceptions (<c>false</c>). Default <c>false</c>.</summary>
public bool? IgnoreHttpErrors { get; set; }

/// <summary>
/// <para>When we should stop waiting for the response. For example, setting this to <see cref="HttpCompletionOption.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 <see cref="IResponse"/>; reading the response body (e.g. using a method like <see cref="IResponse.As{T}"/>) will still wait for the request body to be fetched as usual.</para>
/// <para>Default <see cref="HttpCompletionOption.ResponseContentRead"/> if not specified.</para>
/// </summary>
public HttpCompletionOption? CompleteWhen { get; set; }


/*********
** Public methods
Expand All @@ -22,7 +30,8 @@ internal RequestOptions ToRequestOptions()
return new RequestOptions
{
IgnoreHttpErrors = this.IgnoreHttpErrors,
IgnoreNullArguments = this.IgnoreNullArguments
IgnoreNullArguments = this.IgnoreNullArguments,
CompleteWhen = this.CompleteWhen,
};
}

Expand All @@ -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;
}
}
}
12 changes: 10 additions & 2 deletions Client/RequestOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Net.Http;

namespace Pathoschild.Http.Client
{
/// <summary>Options for a request.</summary>
Expand All @@ -6,12 +8,17 @@ public class RequestOptions
/*********
** Accessors
*********/
/// <summary>Whether to ignore null arguments when the request is dispatched. Default true if not specified.</summary>
/// <summary>Whether null arguments in the request body and URL query string should be ignored (<c>true</c>) or sent as-is (<c>false</c>). Default <c>true</c>.</summary>
public bool? IgnoreNullArguments { get; set; }

/// <summary>Whether HTTP error responses (e.g. HTTP 404) should be ignored (else raised as exceptions). Default false if not specified.</summary>
/// <summary>Whether HTTP error responses like HTTP 404 should be ignored (<c>true</c>) or raised as exceptions (<c>false</c>). Default <c>false</c>.</summary>
public bool? IgnoreHttpErrors { get; set; }

/// <summary>
/// <para>When we should stop waiting for the response. For example, setting this to <see cref="HttpCompletionOption.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 <see cref="IResponse"/>; reading the response body (e.g. using a method like <see cref="IResponse.As{T}"/>) will still wait for the request body to be fetched as usual.</para>
/// <para>Default <see cref="HttpCompletionOption.ResponseContentRead"/> if not specified.</para>
/// </summary>
public HttpCompletionOption? CompleteWhen { get; set; }

/*********
** Public methods
Expand All @@ -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;
}
}
}
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>()`) 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:
Expand Down