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 7 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,215 @@
// 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>(s_getAsync, 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(s_getAsync, 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
Func<HttpClient, Uri?, CancellationToken, Task<HttpResponseMessage>> getMethod,
HttpClient client,
Uri? requestUri,
JsonSerializerOptions? options,
CancellationToken cancellationToken)
{
options ??= JsonSerializerOptions.Default;
IEvangelist marked this conversation as resolved.
Show resolved Hide resolved
var jsonTypeInfo = (JsonTypeInfo<TValue>)options.GetTypeInfo(typeof(TValue));

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

private static IAsyncEnumerable<TValue?> FromJsonStreamAsyncCore<TValue>(
Func<HttpClient, Uri?, CancellationToken, Task<HttpResponseMessage>> getMethod,
IEvangelist marked this conversation as resolved.
Show resolved Hide resolved
HttpClient client,
Uri? requestUri,
JsonTypeInfo<TValue> jsonTypeInfo,
CancellationToken cancellationToken)
{
if (client is null)
{
throw new ArgumentNullException(nameof(client));
}

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

static async IAsyncEnumerable<TValue?> Core(
Func<HttpClient, Uri?, CancellationToken, Task<HttpResponseMessage>> getMethod,
HttpClient client,
Uri? requestUri,
JsonTypeInfo<TValue> jsonTypeInfo,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
TimeSpan timeout = client.Timeout;

// Create the CTS before the initial SendAsync so that the SendAsync counts against the timeout.
CancellationTokenSource? linkedCTS = null;
if (timeout != Timeout.InfiniteTimeSpan)
{
linkedCTS = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
linkedCTS.CancelAfter(timeout);
}

// We call SendAsync outside of the async Core method to propagate exception even without awaiting the returned task.
Task<HttpResponseMessage> responseTask;
try
{
// Intentionally using cancellationToken instead of the linked one here as HttpClient will enforce the Timeout on its own for this part
responseTask = getMethod(client, requestUri, cancellationToken);
}
catch
{
linkedCTS?.Dispose();
throw;
}

try
{
using HttpResponseMessage response = await responseTask.ConfigureAwait(false);
IEvangelist marked this conversation as resolved.
Show resolved Hide resolved
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);
}

await foreach (TValue? value in response.Content.ReadFromJsonAsAsyncEnumerable<TValue>(
jsonTypeInfo, cancellationToken))
{
yield return value;
}
}
finally
{
linkedCTS?.Dispose();
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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 async IAsyncEnumerable<TValue?> ReadFromJsonAsAsyncEnumerableCore<TValue>(
HttpContent content,
JsonSerializerOptions? options,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
using (Stream contentStream = await GetContentStreamAsync(content, cancellationToken)
.ConfigureAwait(false))
{
await foreach (TValue? value in JsonSerializer.DeserializeAsyncEnumerable<TValue>(
contentStream, options ?? JsonHelpers.s_defaultSerializerOptions, cancellationToken)
.ConfigureAwait(false))
{
yield return value;
}
}
}

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