FsHttp icon indicating copy to clipboard operation
FsHttp copied to clipboard

Add builder method for user-supplied CancellationToken

Open jcmrva opened this issue 3 years ago • 1 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.

jcmrva avatar Aug 02 '22 19:08 jcmrva

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.

SchlenkR avatar Aug 03 '22 07:08 SchlenkR

Hint:

grafik

SchlenkR avatar Aug 05 '23 10:08 SchlenkR

527e9b6a2a8f9e1421d3ca2d23c439d413159729 99e5f269e7eb348cd6b51ae4ecca5103edbf9639

SchlenkR avatar Aug 16 '23 07:08 SchlenkR

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

SchlenkR avatar Sep 03 '23 08:09 SchlenkR

I'd be happy with either one, but having a choice is great. Thanks for getting this in!

jcmrva avatar Sep 04 '23 01:09 jcmrva

Do you have an estimate for when the next Nuget package will be released?

jcmrva avatar Oct 31 '23 17:10 jcmrva

Hi Josh. I just released v12 (https://www.nuget.org/packages/FsHttp). Could you check please if it all works for you? Thank you.

SchlenkR avatar Oct 31 '23 21:10 SchlenkR