Files
ReC/src/ReC.Client/ReCClientHelpers.cs
TekH 0149a77f21 Add ReCClientHelpers for query and JSON request helpers
Introduced internal static class ReCClientHelpers with methods to build query strings from key/value pairs and to create JsonContent payloads for HTTP requests. Includes conditional compilation for .NET Framework compatibility and XML documentation for both methods. Added necessary using directives.
2026-01-16 11:34:19 +01:00

50 lines
2.0 KiB
C#

using System;
using System.Globalization;
using System.Linq;
using System.Net.Http.Json;
#if NETFRAMEWORK
using System.Net.Http;
#endif
namespace ReC.Client
{
/// <summary>
/// Provides shared helpers for composing requests.
/// </summary>
internal static class ReCClientHelpers
{
#if NETFRAMEWORK
/// <summary>
/// Builds a query string from the provided key/value pairs, skipping null values.
/// </summary>
/// <param name="parameters">The key/value pairs to include in the query string.</param>
/// <returns>A query string beginning with '?', or an empty string if no values are provided.</returns>
public static string BuildQuery(params (string Key, object Value)[] parameters)
#else
/// <summary>
/// Builds a query string from the provided key/value pairs, skipping null values.
/// </summary>
/// <param name="parameters">The key/value pairs to include in the query string.</param>
/// <returns>A query string beginning with '?', or an empty string if no values are provided.</returns>
public static string BuildQuery(params (string Key, object? Value)[] parameters)
#endif
{
var parts = parameters
.Where(p => p.Value != null)
.Select(p => $"{Uri.EscapeDataString(p.Key)}={Uri.EscapeDataString(Convert.ToString(p.Value, CultureInfo.InvariantCulture) ?? string.Empty)}");
var query = string.Join("&", parts);
return string.IsNullOrWhiteSpace(query) ? string.Empty : $"?{query}";
}
/// <summary>
/// Creates a JSON content payload from the provided object.
/// </summary>
/// <typeparam name="T">The type of the payload.</typeparam>
/// <param name="payload">The payload to serialize.</param>
/// <returns>A <see cref="JsonContent"/> instance ready for HTTP requests.</returns>
public static JsonContent ToJsonContent<T>(T payload) => JsonContent.Create(payload);
}
}