Flurl icon indicating copy to clipboard operation
Flurl copied to clipboard

FlurlResponse Get_Async methods don't accept CancellationToken

Open develorem opened this issue 2 years ago • 4 comments

Please describe the feature/enhancement in as much detail as possible. CancellationToken is accepted on some async methods. While I haven't audited the code base, the class I care about currently is FlurlResponse and methods such as:

  • GetJsonAsync<T>()
  • GetStreamAsync<T>()

The underlying calls to HttpResponseMessage.Content.ReadAsStreamAsync() supports CancellationTokens, FlurlResponse should too

develorem avatar Aug 09 '22 04:08 develorem

@tmenier - Happy to implement this as it looks pretty straight forward? Just tell me which branch to start from.

develorem avatar Aug 11 '22 06:08 develorem

Yeah, this looks like an oversight. Pretty trivial enhancement, I'll get it out with the next 4.0 prerelease.

tmenier avatar Sep 15 '22 20:09 tmenier

Sorry for the long delay. It turns out this wasn't an oversight. Flurl relies on async methods off HttpContent to implement these methods, and those didn't support CancellationTokens until .NET 5. Flurl still supports older platforms, making this tricky to implement without a bunch of compiler switches all over the place. Not impossible, but not trivial like I originally thought, so this won't make the cut for now.

tmenier avatar Dec 20 '22 18:12 tmenier

You could use conditionally compiled extension methods to get CancellationToken support for this methods in older runtimes.

Examples: https://github.com/SimonCropp/Polyfill/blob/main/src/Polyfill/PolyfillExtensions_HttpClient.cs https://github.com/SimonCropp/Polyfill/blob/main/src/Polyfill/PolyfillExtensions_HttpContent.cs

edit: Ok, I just noticed that those HttpContent extension methods do not actually use the CancellationToken except for a check before requesting the content. So those are not really a good solution.

cremor avatar May 03 '23 06:05 cremor