Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new streaming APIs for the System.Net.Http.Json extensions #89258

Merged
merged 14 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 55 additions & 34 deletions src/libraries/System.Net.Http.Json/ref/System.Net.Http.Json.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ System.Net.Http.Json.JsonContent</PackageDescription>
</PropertyGroup>

<ItemGroup>
<Compile Include="System\Net\Http\Json\HttpClientJsonExtensions.Get.AsyncEnumerable.cs" />
<Compile Include="System\Net\Http\Json\HttpClientJsonExtensions.cs" />
<Compile Include="System\Net\Http\Json\HttpContentJsonExtensions.AsyncEnumerable.cs" />
<Compile Include="System\Net\Http\Json\JsonHelpers.cs" />
<Compile Include="System\Net\Http\Json\HttpClientJsonExtensions.Delete.cs" />
<Compile Include="System\Net\Http\Json\HttpClientJsonExtensions.Get.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.Http.Json
{
public static partial class HttpClientJsonExtensions
{
/// <summary>
/// Sends an <c>HTTP GET</c> request to the specified <paramref name="requestUri"/> and returns the value that results
/// from deserializing the response body as JSON in an async enumerable operation.
/// </summary>
/// <typeparam name="TValue">The target type to deserialize to.</typeparam>
/// <param name="client">The client used to send the request.</param>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <param name="options"></param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>An <see cref="IAsyncEnumerable{TValue}"/> that represents the deserialized response body.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="client"/> is <see langword="null"/>.</exception>
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(HttpContentJsonExtensions.SerializationDynamicCodeMessage)]
public static IAsyncEnumerable<TValue?> GetFromJsonAsAsyncEnumerable<TValue>(
this HttpClient client,
[StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri,
JsonSerializerOptions? options,
CancellationToken cancellationToken = default) =>
GetFromJsonAsAsyncEnumerable<TValue>(client, CreateUri(requestUri), options, cancellationToken);

/// <summary>
/// Sends an <c>HTTP GET</c>request to the specified <paramref name="requestUri"/> and returns the value that results
/// from deserializing the response body as JSON in an async enumerable operation.
/// </summary>
/// <typeparam name="TValue">The target type to deserialize to.</typeparam>
/// <param name="client">The client used to send the request.</param>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <param name="options"></param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>An <see cref="IAsyncEnumerable{TValue}"/> that represents the deserialized response body.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="client"/> is <see langword="null"/>.</exception>
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(HttpContentJsonExtensions.SerializationDynamicCodeMessage)]
public static IAsyncEnumerable<TValue?> GetFromJsonAsAsyncEnumerable<TValue>(
this HttpClient client,
Uri? requestUri,
JsonSerializerOptions? options,
CancellationToken cancellationToken = default) =>
FromJsonStreamAsyncCore<TValue>(client, requestUri, options, cancellationToken);

/// <summary>
/// Sends an <c>HTTP GET</c>request to the specified <paramref name="requestUri"/> and returns the value that results
/// from deserializing the response body as JSON in an async enumerable operation.
/// </summary>
/// <typeparam name="TValue">The target type to deserialize to.</typeparam>
/// <param name="client">The client used to send the request.</param>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <param name="jsonTypeInfo">Source generated JsonTypeInfo to control the behavior during deserialization.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>An <see cref="IAsyncEnumerable{TValue}"/> that represents the deserialized response body.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="client"/> is <see langword="null"/>.</exception>
public static IAsyncEnumerable<TValue?> GetFromJsonAsAsyncEnumerable<TValue>(
this HttpClient client,
[StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri,
JsonTypeInfo<TValue> jsonTypeInfo,
CancellationToken cancellationToken = default) =>
GetFromJsonAsAsyncEnumerable(client, CreateUri(requestUri), jsonTypeInfo, cancellationToken);

/// <summary>
/// Sends an <c>HTTP GET</c>request to the specified <paramref name="requestUri"/> and returns the value that results
/// from deserializing the response body as JSON in an async enumerable operation.
/// </summary>
/// <typeparam name="TValue">The target type to deserialize to.</typeparam>
/// <param name="client">The client used to send the request.</param>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <param name="jsonTypeInfo">Source generated JsonTypeInfo to control the behavior during deserialization.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>An <see cref="IAsyncEnumerable{TValue}"/> that represents the deserialized response body.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="client"/> is <see langword="null"/>.</exception>
public static IAsyncEnumerable<TValue?> GetFromJsonAsAsyncEnumerable<TValue>(
this HttpClient client,
Uri? requestUri,
JsonTypeInfo<TValue> jsonTypeInfo,
CancellationToken cancellationToken = default) =>
FromJsonStreamAsyncCore(client, requestUri, jsonTypeInfo, cancellationToken);

/// <summary>
/// Sends an <c>HTTP GET</c>request to the specified <paramref name="requestUri"/> and returns the value that results
/// from deserializing the response body as JSON in an async enumerable operation.
/// </summary>
/// <typeparam name="TValue">The target type to deserialize to.</typeparam>
/// <param name="client">The client used to send the request.</param>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>An <see cref="IAsyncEnumerable{TValue}"/> that represents the deserialized response body.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="client"/> is <see langword="null"/>.</exception>
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(HttpContentJsonExtensions.SerializationDynamicCodeMessage)]
public static IAsyncEnumerable<TValue?> GetFromJsonAsAsyncEnumerable<TValue>(
this HttpClient client,
[StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri,
CancellationToken cancellationToken = default) =>
GetFromJsonAsAsyncEnumerable<TValue>(client, requestUri, options: null, cancellationToken);

/// <summary>
/// Sends an <c>HTTP GET</c>request to the specified <paramref name="requestUri"/> and returns the value that results
/// from deserializing the response body as JSON in an async enumerable operation.
/// </summary>
/// <typeparam name="TValue">The target type to deserialize to.</typeparam>
/// <param name="client">The client used to send the request.</param>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>An <see cref="IAsyncEnumerable{TValue}"/> that represents the deserialized response body.</returns>
/// <exception cref="ArgumentNullException">The <paramref name="client"/> is <see langword="null"/>.</exception>
[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(HttpContentJsonExtensions.SerializationDynamicCodeMessage)]
public static IAsyncEnumerable<TValue?> GetFromJsonAsAsyncEnumerable<TValue>(
this HttpClient client,
Uri? requestUri,
CancellationToken cancellationToken = default) =>
GetFromJsonAsAsyncEnumerable<TValue>(client, requestUri, options: null, cancellationToken);

[RequiresUnreferencedCode(HttpContentJsonExtensions.SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(HttpContentJsonExtensions.SerializationDynamicCodeMessage)]
private static IAsyncEnumerable<TValue?> FromJsonStreamAsyncCore<TValue>(
IEvangelist marked this conversation as resolved.
Show resolved Hide resolved
HttpClient client,
Uri? requestUri,
JsonSerializerOptions? options,
CancellationToken cancellationToken)
{
options ??= JsonSerializerOptions.Default;
IEvangelist marked this conversation as resolved.
Show resolved Hide resolved
options.MakeReadOnly();

var jsonTypeInfo = (JsonTypeInfo<TValue>)options.GetTypeInfo(typeof(TValue));

return FromJsonStreamAsyncCore(client, requestUri, jsonTypeInfo, cancellationToken);
}

private static IAsyncEnumerable<TValue?> FromJsonStreamAsyncCore<TValue>(
HttpClient client,
Uri? requestUri,
JsonTypeInfo<TValue> jsonTypeInfo,
CancellationToken cancellationToken)
{
if (client is null)
{
throw new ArgumentNullException(nameof(client));
}

return Core(client, requestUri, jsonTypeInfo, cancellationToken);

static async IAsyncEnumerable<TValue?> Core(
HttpClient client,
Uri? requestUri,
JsonTypeInfo<TValue> jsonTypeInfo,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
using HttpResponseMessage response = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);
response.EnsureSuccessStatusCode();

using Stream readStream = await GetHttpResponseStreamAsync(client, response, false, cancellationToken)
.ConfigureAwait(false);

await foreach (TValue? value in JsonSerializer.DeserializeAsyncEnumerable<TValue>(
readStream, jsonTypeInfo, cancellationToken).ConfigureAwait(false))
{
yield return value;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,10 @@ public static partial class HttpClientJsonExtensions
using HttpResponseMessage response = await responseTask.ConfigureAwait(false);
response.EnsureSuccessStatusCode();

Debug.Assert(client.MaxResponseContentBufferSize is > 0 and <= int.MaxValue);
int contentLengthLimit = (int)client.MaxResponseContentBufferSize;

if (response.Content.Headers.ContentLength is long contentLength && contentLength > contentLengthLimit)
{
LengthLimitReadStream.ThrowExceededBufferLimit(contentLengthLimit);
}

try
{
using Stream contentStream = await HttpContentJsonExtensions.GetContentStreamAsync(response.Content, linkedCTS?.Token ?? cancellationToken).ConfigureAwait(false);

// If ResponseHeadersRead wasn't used, HttpClient will have already buffered the whole response upfront. No need to check the limit again.
Stream readStream = usingResponseHeadersRead
? new LengthLimitReadStream(contentStream, (int)client.MaxResponseContentBufferSize)
: contentStream;
using Stream readStream = await GetHttpResponseStreamAsync(client, response, usingResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);

return await deserializeMethod(readStream, jsonOptions, linkedCTS?.Token ?? cancellationToken).ConfigureAwait(false);
}
Expand All @@ -123,5 +111,31 @@ public static partial class HttpClientJsonExtensions

private static Uri? CreateUri(string? uri) =>
string.IsNullOrEmpty(uri) ? null : new Uri(uri, UriKind.RelativeOrAbsolute);

private static async Task<Stream> GetHttpResponseStreamAsync(
HttpClient client,
HttpResponseMessage response,
bool usingResponseHeadersRead,
CancellationToken cancellationToken)
{
Debug.Assert(client.MaxResponseContentBufferSize is > 0 and <= int.MaxValue);
int contentLengthLimit = (int)client.MaxResponseContentBufferSize;

if (response.Content.Headers.ContentLength is long contentLength && contentLength > contentLengthLimit)
{
LengthLimitReadStream.ThrowExceededBufferLimit(contentLengthLimit);
}

Stream contentStream = await HttpContentJsonExtensions.GetContentStreamAsync(response.Content, cancellationToken)
.ConfigureAwait(false);

// If ResponseHeadersRead wasn't used, HttpClient will have already buffered the whole response upfront.
// No need to check the limit again.
Stream readStream = usingResponseHeadersRead
? new LengthLimitReadStream(contentStream, (int)client.MaxResponseContentBufferSize)
: contentStream;

return readStream;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.Http.Json
{
public static partial class HttpContentJsonExtensions
{
/// <summary>
/// Reads the HTTP content and returns the value that results from deserializing the content as
/// JSON in an async enumerable operation.
/// </summary>
/// <typeparam name="TValue">The target type to deserialize to.</typeparam>
/// <param name="content"></param>
/// <param name="cancellationToken"></param>
/// <returns>An <see cref="IAsyncEnumerable{TValue}"/> that represents the deserialized response body.</returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="content"/> is <see langword="null"/>.
/// </exception>
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(SerializationDynamicCodeMessage)]
public static IAsyncEnumerable<TValue?> ReadFromJsonAsAsyncEnumerable<TValue>(
this HttpContent content,
CancellationToken cancellationToken = default) =>
ReadFromJsonAsAsyncEnumerable<TValue>(content, options: null, cancellationToken: cancellationToken);

/// <summary>
/// Reads the HTTP content and returns the value that results from deserializing the content as
/// JSON in an async enumerable operation.
/// </summary>
/// <typeparam name="TValue">The target type to deserialize to.</typeparam>
/// <param name="content">The content to read from.</param>
/// <param name="options">Options to control the behavior during deserialization.
/// The default options are those specified by <see cref="JsonSerializerDefaults.Web"/>.</param>
/// <param name="cancellationToken"></param>
/// <returns>An <see cref="IAsyncEnumerable{TValue}"/> that represents the deserialized response body.</returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="content"/> is <see langword="null"/>.
/// </exception>
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(SerializationDynamicCodeMessage)]
public static IAsyncEnumerable<TValue?> ReadFromJsonAsAsyncEnumerable<TValue>(
this HttpContent content,
JsonSerializerOptions? options,
CancellationToken cancellationToken = default)
{
if (content is null)
{
throw new ArgumentNullException(nameof(content));
}

return ReadFromJsonAsAsyncEnumerableCore<TValue>(content, options, cancellationToken);
}

[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
[RequiresDynamicCode(SerializationDynamicCodeMessage)]
private static IAsyncEnumerable<TValue?> ReadFromJsonAsAsyncEnumerableCore<TValue>(
HttpContent content,
JsonSerializerOptions? options,
CancellationToken cancellationToken)
{
options ??= JsonSerializerOptions.Default;
IEvangelist marked this conversation as resolved.
Show resolved Hide resolved
options.MakeReadOnly();

var jsonTypeInfo = (JsonTypeInfo<TValue>)options.GetTypeInfo(typeof(TValue));

return ReadFromJsonAsAsyncEnumerableCore(content, jsonTypeInfo, cancellationToken);
}

public static IAsyncEnumerable<TValue?> ReadFromJsonAsAsyncEnumerable<TValue>(
this HttpContent content,
JsonTypeInfo<TValue> jsonTypeInfo,
CancellationToken cancellationToken = default)
{
if (content is null)
{
throw new ArgumentNullException(nameof(content));
}

return ReadFromJsonAsAsyncEnumerableCore(content, jsonTypeInfo, cancellationToken);
}

private static async IAsyncEnumerable<TValue?> ReadFromJsonAsAsyncEnumerableCore<TValue>(
HttpContent content,
JsonTypeInfo<TValue> jsonTypeInfo,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
using Stream contentStream = await GetContentStreamAsync(content, cancellationToken)
.ConfigureAwait(false);

await foreach (TValue? value in JsonSerializer.DeserializeAsyncEnumerable<TValue>(
contentStream, jsonTypeInfo, cancellationToken)
.ConfigureAwait(false))
{
yield return value;
}
}
}
}
Loading
Loading