-
Notifications
You must be signed in to change notification settings - Fork 5k
Implement new streaming APIs for the System.Net.Http.Json
extensions
#89258
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
Merged
eiriktsarpalis
merged 14 commits into
dotnet:main
from
IEvangelist:streaming-json-http-apis
Jul 24, 2023
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ed44a47
Contributes to #87577
IEvangelist 9901e29
More updates
IEvangelist 352141c
Added unit tests, generated ref, and minor clean up
IEvangelist 73c44b9
Added missing triple slash comments
IEvangelist 04a2537
Update src/libraries/System.Net.Http.Json/tests/FunctionalTests/HttpC…
IEvangelist ad465c8
Correct the preprocessor directives, and delegate to JsonTypeInfo ove…
IEvangelist 4c4ff60
Refactor for deferred execution, remove helper methods since they're …
IEvangelist 7c76121
Add test to ensure deferred execution semantics, updates from Miha.
IEvangelist f790d55
Apply suggestions from code review
IEvangelist b73f3ce
Update src/libraries/System.Net.Http.Json/src/System/Net/Http/Json/Ht…
IEvangelist deb2409
Update test per Miha's feedback
IEvangelist ad03a32
A few more updates from peer review, Eirik's nits.
IEvangelist c99e708
No need for the length limit read stream.
IEvangelist a1528ff
Add limit per invocation, not element. Share length limit read stream…
IEvangelist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
89 changes: 55 additions & 34 deletions
89
src/libraries/System.Net.Http.Json/ref/System.Net.Http.Json.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
...em.Net.Http.Json/src/System/Net/Http/Json/HttpClientJsonExtensions.Get.AsyncEnumerable.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>( | ||
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...ystem.Net.Http.Json/src/System/Net/Http/Json/HttpContentJsonExtensions.AsyncEnumerable.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.