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
3 changes: 2 additions & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<!-- Please add your release notes in the following format:
- My change description (#PR/#issue)
-->
- Adding support for opting-in to get empty entries in function trigger payload (#1091)
- Adding support for opting-in to get empty entries in function trigger payload (#1091)
- Exposed new overloads for `WriteStringAsync` and `WriteBytesAsync` methods (#1111)
50 changes: 37 additions & 13 deletions src/DotNetWorker.Core/Http/HttpResponseDataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Serialization;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -43,13 +42,26 @@ public static void WriteString(this HttpResponseData response, string value, Enc
}

/// <summary>
/// Writes the provided string to the response body using the specified encoding.
/// Asynchronously writes the provided string to the response body using the specified encoding.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

/// </summary>
/// <param name="response">The response to write the string to.</param>
/// <param name="value">The string content to write to the request body.</param>
/// <param name="encoding">The encoding to use when writing the string. Defaults to UTF-8</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task WriteStringAsync(this HttpResponseData response, string value, Encoding? encoding = null)
{
return WriteStringAsync(response, value, default, encoding);
}

/// <summary>
/// Asynchronously writes the provided string to the response body using the specified encoding, and monitors cancellation requests.
/// </summary>
/// <param name="response">The response to write the string to.</param>
/// <param name="value">The string content to write to the request body.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <param name="encoding">The encoding to use when writing the string. Defaults to UTF-8</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task WriteStringAsync(this HttpResponseData response, string value, CancellationToken cancellationToken, Encoding? encoding = null)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new overload we are exposing.

{
if (response is null)
{
Expand All @@ -64,11 +76,11 @@ public static Task WriteStringAsync(this HttpResponseData response, string value
encoding ??= Encoding.UTF8;

byte[] bytes = encoding.GetBytes(value);
return response.Body.WriteAsync(bytes, 0, bytes.Length);
return response.Body.WriteAsync(bytes, 0, bytes.Length, cancellationToken);
}

/// <summary>
/// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to 200.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
Expand All @@ -82,7 +94,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
}

/// <summary>
/// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to the provided <paramref name="statusCode"/>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
Expand All @@ -98,7 +110,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
}

/// <summary>
/// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to 200.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
Expand All @@ -120,7 +132,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
}

/// <summary>
/// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to the provided <paramref name="statusCode"/>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
Expand All @@ -145,7 +157,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in


/// <summary>
/// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to 200.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
Expand All @@ -160,7 +172,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
}

/// <summary>
/// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to the provided <paramref name="statusCode"/>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
Expand All @@ -177,7 +189,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
}

/// <summary>
/// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to 200.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
Expand All @@ -195,7 +207,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
}

/// <summary>
/// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to the provided <paramref name="statusCode"/>.
/// </summary>
/// <typeparam name="T">The type of object to write.</typeparam>
Expand Down Expand Up @@ -250,12 +262,24 @@ public static void WriteBytes(this HttpResponseData response, byte[] value)
}

/// <summary>
/// Writes the provided bytes to the response body.
/// Asynchronously writes the provided bytes to the response body.
/// </summary>
/// <param name="response">The response to write the string to.</param>
/// <param name="value">The byte content to write to the request body.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task WriteBytesAsync(this HttpResponseData response, byte[] value)
{
return WriteBytesAsync(response, value, default);
}

/// <summary>
/// Asynchronously writes the provided bytes to the response body, and monitors cancellation requests.
/// </summary>
/// <param name="response">The response to write the string to.</param>
/// <param name="value">The byte content to write to the request body.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static Task WriteBytesAsync(this HttpResponseData response, byte[] value, CancellationToken cancellationToken)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the other new overload we are exposing.

{
if (response is null)
{
Expand All @@ -267,7 +291,7 @@ public static Task WriteBytesAsync(this HttpResponseData response, byte[] value)
throw new ArgumentNullException(nameof(value));
}

return response.Body.WriteAsync(value, 0, value.Length);
return response.Body.WriteAsync(value, 0, value.Length, cancellationToken);
}

private static ObjectSerializer GetObjectSerializer(HttpResponseData response)
Expand Down