Files
FsToolbox/Helper/Extensions.cs

42 lines
1.7 KiB
C#

using System.Text;
using System.Text.Json.Nodes;
namespace FsTool.Helpers
{
public static class Extensions
{
#region Public Method FsPostAsync
/// <summary>
/// Sends a POST request to the specified URI with the provided JSON content.
/// </summary>
/// <param name="httpClient">The instance of <see cref="HttpClient" /> used to send the request.</param>
/// <param name="requestUri">The URI to which the request is sent.</param>
/// <param name="jsonObject">The JSON object to include in the request body.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the HTTP response.</returns>
public static async Task<HttpResponseMessage> FsPostAsync(this HttpClient httpClient, string requestUri, JsonObject jsonObject)
{
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
content.Headers.ContentType = new("application/json");
return await httpClient.PostAsync(requestUri, content);
}
#endregion
#region Public Method ToList
/// <summary>
/// Converts the specified JSON node into a list of non-null <see cref="JsonNode" /> elements.
/// </summary>
/// <param name="node">The <see cref="JsonNode" /> to be converted. If null, an empty list is returned.</param>
/// <returns>A list of <see cref="JsonNode" /> instances containing non-null elements.</returns>
public static List<JsonNode> ToList(this JsonNode? node)
{
var array = node?.AsArray() ?? [];
return array.Where(x => x != null).Select(x => x!).ToList();
}
#endregion
}
}