tmenier / Flurl

Fluent URL builder and testable HTTP client for .NET
https://flurl.dev
MIT License
4.23k stars 387 forks source link

FlurlResponse Get_Async methods don't accept CancellationToken #711

Open develorem opened 2 years ago

develorem commented 2 years ago

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:

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

develorem commented 2 years ago

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

tmenier commented 2 years ago

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

tmenier commented 1 year ago

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.

cremor commented 1 year ago

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.