Add builder method for user-supplied CancellationToken
Closed this issue ยท 7 comments
I found this note:
https://github.com/fsprojects/FsHttp/blob/1682f420c2676b6126b3e13a2be6cdf08b3ce27e/src/FsHttp/Response.fs#L25
Assuming this would be applied to requests as well.
Yes, definitely. I have to check how cancellation is done in the underlying HttpClient and others and then decide how the API should allow passing tokens, etc.
For clarification + documentation:
There are 2 solutions that seems to be reaonable for an implementation:
- Setting the cancellation token on "define-time" (as part of the request)
- Passing it along at "execution-time" (something like
http { GET "url" } |> Request.withCancellationToken tok |> Request.sendAsync
).
The 2nd approach seems to me like the way to go, because: A FsHttp request is just data, out of which a dotnet HttpRequest is constructed and executed. This can happen multiple times. But: This is not strictly followed through since it's (for example) possible to capture an instance of HttpClient via an HttpClientTransformer, which is then part of the FsHttp request. I can also think of scenarios where it's wanted to bind a CTok to a request definition. This automatically also provides the possibility for passing a CTok ad-hoc on execution time, like shown above.
There are now these 2 possibilities implemented:
// It is possible to bind a cancellation token to a request definition,
// that will be used for the underlying HTTP request:
use cts = new CancellationTokenSource()
// ...
http {
GET "https://mysite"
config_cancellationToken cts.Token
}
|> Request.send
// See also: https://github.com/fsprojects/FsHttp/issues/105
// Instead of binding a cancellation token directly to a request definition (like in the example above),
// it is also possible to pass it on execution-timer, like so:
http {
GET "https://mysite"
}
|> Config.cancellationToken cts.Token
|> Request.send
I'd be happy with either one, but having a choice is great. Thanks for getting this in!
Do you have an estimate for when the next Nuget package will be released?
Hi Josh. I just released v12 (https://www.nuget.org/packages/FsHttp). Could you check please if it all works for you? Thank you.