Skip to content

Commit

Permalink
HttpContent buffering
Browse files Browse the repository at this point in the history
  • Loading branch information
7702244 committed Jun 27, 2024
1 parent 7e32148 commit 270008a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
10 changes: 3 additions & 7 deletions src/MAOToolkit/Extensions/HttpContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ public static class HttpContentExtensions
/// <returns>String representation of HTTP content.</returns>
public static async Task<string> ReadAsStringAsync(this HttpContent httpContent, int charsLimit)
{
if (httpContent is null)
throw new ArgumentNullException(nameof(httpContent));
ArgumentNullException.ThrowIfNull(httpContent);

await httpContent.LoadIntoBufferAsync();

var stream = await httpContent.ReadAsStreamAsync();

if (!stream.CanRead)
{
return (await httpContent.ReadAsStringAsync()).Left(charsLimit);
}

// The `leaveOpen` should be `true` if there's another function going to be invoked AFTER this.
using var reader = new StreamReader(
stream,
Expand Down
3 changes: 1 addition & 2 deletions src/MAOToolkit/Extensions/StreamReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public static class StreamReaderExtensions
/// <returns>String representation of readed stream.</returns>
public static async ValueTask<string> ReadWithLimitAsync(this StreamReader reader, int charsLimit)
{
if (reader is null)
throw new ArgumentNullException(nameof(reader));
ArgumentNullException.ThrowIfNull(reader);

if (charsLimit > 0)
{
Expand Down

0 comments on commit 270008a

Please sign in to comment.