Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ partial void ProcessCreateGuardResponseContent(
try
{
__response.EnsureSuccessStatusCode();

using var __content = await __response.Content.ReadAsStreamAsync(
#if NET5_0_OR_GREATER
cancellationToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ partial void ProcessDeleteGuardResponseContent(
try
{
__response.EnsureSuccessStatusCode();

using var __content = await __response.Content.ReadAsStreamAsync(
#if NET5_0_OR_GREATER
cancellationToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ partial void ProcessGetGuardResponseContent(
try
{
__response.EnsureSuccessStatusCode();

using var __content = await __response.Content.ReadAsStreamAsync(
#if NET5_0_OR_GREATER
cancellationToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ partial void ProcessGetGuardHistoryResponseContent(
try
{
__response.EnsureSuccessStatusCode();

using var __content = await __response.Content.ReadAsStreamAsync(
#if NET5_0_OR_GREATER
cancellationToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ partial void ProcessGetGuardsResponseContent(
try
{
__response.EnsureSuccessStatusCode();

using var __content = await __response.Content.ReadAsStreamAsync(
#if NET5_0_OR_GREATER
cancellationToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ partial void ProcessUpdateGuardResponseContent(
try
{
__response.EnsureSuccessStatusCode();

using var __content = await __response.Content.ReadAsStreamAsync(
#if NET5_0_OR_GREATER
cancellationToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ partial void ProcessOpenaiChatCompletionResponseContent(
try
{
__response.EnsureSuccessStatusCode();

using var __content = await __response.Content.ReadAsStreamAsync(
#if NET5_0_OR_GREATER
cancellationToken
Expand Down
126 changes: 124 additions & 2 deletions src/libs/Guardrails/Generated/Guardrails.Polyfills.g.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

#if !NET6_0_OR_GREATER
#nullable enable

namespace Guardrails
Expand All @@ -9,6 +8,7 @@ namespace Guardrails
/// </summary>
public static partial class AutoSdkPolyfills
{
#if !NET6_0_OR_GREATER
/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -48,6 +48,128 @@ public static partial class AutoSdkPolyfills
content = content ?? throw new global::System.ArgumentNullException(nameof(content));
return content.ReadAsByteArrayAsync();
}
#endif

/// <summary>
/// Creates a JSON request content instance.
/// </summary>
public static global::System.Net.Http.HttpContent CreateJsonContent<T>(
T inputValue,
string mediaType,
global::System.Text.Json.JsonSerializerOptions? jsonSerializerOptions)
{
if (string.IsNullOrWhiteSpace(mediaType))
{
throw new global::System.ArgumentException("Media type is required.", nameof(mediaType));
}

#if NET5_0_OR_GREATER
return global::System.Net.Http.Json.JsonContent.Create(
inputValue: inputValue,
mediaType: new global::System.Net.Http.Headers.MediaTypeHeaderValue(mediaType),
options: jsonSerializerOptions);
#else
var json = global::System.Text.Json.JsonSerializer.Serialize(inputValue, jsonSerializerOptions);
var stringContent = new global::System.Net.Http.StringContent(
content: json,
encoding: global::System.Text.Encoding.UTF8);
stringContent.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue(mediaType)
{
CharSet = global::System.Text.Encoding.UTF8.WebName,
};
return stringContent;
#endif
}

/// <summary>
/// Creates a JSON request content instance using a source-generated serializer context.
/// </summary>
public static global::System.Net.Http.HttpContent CreateJsonContent(
object? inputValue,
global::System.Type inputType,
string mediaType,
global::System.Text.Json.Serialization.JsonSerializerContext jsonSerializerContext)
{
inputType = inputType ?? throw new global::System.ArgumentNullException(nameof(inputType));
jsonSerializerContext = jsonSerializerContext ?? throw new global::System.ArgumentNullException(nameof(jsonSerializerContext));

if (string.IsNullOrWhiteSpace(mediaType))
{
throw new global::System.ArgumentException("Media type is required.", nameof(mediaType));
}

#if NET5_0_OR_GREATER
var jsonTypeInfo = jsonSerializerContext.GetTypeInfo(inputType) ??
throw new global::System.InvalidOperationException($"No JsonTypeInfo registered for '{inputType}'.");
return global::System.Net.Http.Json.JsonContent.Create(
inputValue: inputValue,
jsonTypeInfo: jsonTypeInfo,
mediaType: new global::System.Net.Http.Headers.MediaTypeHeaderValue(mediaType));
#else
var json = global::System.Text.Json.JsonSerializer.Serialize(
value: inputValue,
inputType: inputType,
jsonSerializerContext);
var stringContent = new global::System.Net.Http.StringContent(
content: json,
encoding: global::System.Text.Encoding.UTF8);
stringContent.Headers.ContentType = new global::System.Net.Http.Headers.MediaTypeHeaderValue(mediaType)
{
CharSet = global::System.Text.Encoding.UTF8.WebName,
};
return stringContent;
#endif
}

/// <summary>
/// Reads JSON content into the specified type using serializer options.
/// </summary>
public static async global::System.Threading.Tasks.Task<T?> ReadFromJsonAsync<T>(
this global::System.Net.Http.HttpContent content,
global::System.Text.Json.JsonSerializerOptions? jsonSerializerOptions,
global::System.Threading.CancellationToken cancellationToken)
{
content = content ?? throw new global::System.ArgumentNullException(nameof(content));

#if NET5_0_OR_GREATER
return await global::System.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsync<T>(
content,
jsonSerializerOptions,
cancellationToken).ConfigureAwait(false);
#else
using var stream = await AutoSdkPolyfills.ReadAsStreamAsync(content, cancellationToken).ConfigureAwait(false);
return await global::System.Text.Json.JsonSerializer.DeserializeAsync<T>(
utf8Json: stream,
options: jsonSerializerOptions,
cancellationToken: cancellationToken).ConfigureAwait(false);
#endif
}

/// <summary>
/// Reads JSON content into the specified type using a source-generated serializer context.
/// </summary>
public static async global::System.Threading.Tasks.Task<T?> ReadFromJsonAsync<T>(
this global::System.Net.Http.HttpContent content,
global::System.Text.Json.Serialization.JsonSerializerContext jsonSerializerContext,
global::System.Threading.CancellationToken cancellationToken)
{
content = content ?? throw new global::System.ArgumentNullException(nameof(content));
jsonSerializerContext = jsonSerializerContext ?? throw new global::System.ArgumentNullException(nameof(jsonSerializerContext));

#if NET5_0_OR_GREATER
return (T?)await global::System.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsync(
content,
typeof(T),
jsonSerializerContext,
cancellationToken).ConfigureAwait(false);
#else
using var stream = await AutoSdkPolyfills.ReadAsStreamAsync(content, cancellationToken).ConfigureAwait(false);
return (T?)await global::System.Text.Json.JsonSerializer.DeserializeAsync(
utf8Json: stream,
returnType: typeof(T),
jsonSerializerContext,
cancellationToken: cancellationToken).ConfigureAwait(false);
#endif
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ partial void ProcessHealthCheckResponseContent(
try
{
__response.EnsureSuccessStatusCode();

using var __content = await __response.Content.ReadAsStreamAsync(
#if NET5_0_OR_GREATER
cancellationToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ partial void ProcessValidateResponseContent(
try
{
__response.EnsureSuccessStatusCode();

using var __content = await __response.Content.ReadAsStreamAsync(
#if NET5_0_OR_GREATER
cancellationToken
Expand Down