Skip to content

Allow HttpCompletionOption to be specified when dispatching a request #123

Closed

Description

@Jericho

HttpClient.Send accepts a HttpCompletionOption parameter that allows developers to stream a response rather than buffer it in memory. Buffering the response is perfectly fine for the majority of scenarios where the expected response is a reasonable size but streaming is a much better option when the response is large.

Currently, the FluentHttpClient does not allow developers to specify their desired completion option and by default Microsoft's HttpClient buffers all responses in memory. This means that we cannot enable the streaming of responses when we use FluentHttpClient.

Here's a sample scenario to illustrate when streaming would be desirable: let's say the developer issues a GET request to an API to download a file and let's say this file is quite large (I don't know exactly at what point a file become too large, but for the sake of this example let's say 100mb). With the current behavior, the entire content of the file will be buffered in memory and there's a real risk the developer will get a "Out of Memory" exception. If the developer intends to save the content of the file to disk, there really is no need to buffer the content in memory and streaming the content would be much better.

I propose the following changes:

  1. Add a boolean property to the FluentClientOptions class to allow developers to indicate if they want responses to be streamed by default
  2. Add a boolean property to the RequestOptions class to track whether the response to a particular request should be streamed or not
  3. Add a boolean parameter to the WithOptions extension method which would make it easy for developers to turn on the streaming feature for a specific request
  4. Modify the request dispatch logic to specify the appropriate completion option when invoking the HttpClient.Send method

Please note that my proposed changes 1, 2 and 3 would be consistent with how other options (i.e. IgnoreNullArguments and IgnoreHttpErrors) are currently handled by FluentHttpClient.

I will submit a PR with my proposed changes for review.

Activity

  1. added this to the 4.4.0 milestone on May 12, 2024
  2. Pathoschild commented on May 12, 2024

    @Pathoschild
    Owner

    Done in develop for the upcoming FluentHttpClient 4.4.0. Thanks for the help!

  3. Jericho commented on May 24, 2024

    @Jericho
    ContributorAuthor

    Thanks for merging my PR. Do you have an idea when you plan to release 4.4.0? No pressure, I'm just trying to figure out when I will be able to update my project which is waiting for this streaming feature.

  4. Pathoschild commented on May 24, 2024

    @Pathoschild
    Owner

    I don't have a particular timeline, but I'll release the update today if you're waiting on the changes.

  5. Pathoschild commented on May 24, 2024

    @Pathoschild
    Owner
  6. Jericho commented on May 24, 2024

    @Jericho
    ContributorAuthor

    Excellent. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions