Modularize project structure by splitting responsibilities into distinct files and namespaces. Add helper methods for authentication and JSON operations.
This commit is contained in:
41
Helper/Extensions.cs
Normal file
41
Helper/Extensions.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user