#nullable enable
using Swan.Formatters;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Swan.Net {
///
/// Represents a HttpClient with extended methods to use with JSON payloads
/// and bearer tokens authentication.
///
public static class JsonClient {
private const String JsonMimeType = "application/json";
private const String FormType = "application/x-www-form-urlencoded";
private static readonly HttpClient HttpClient = new HttpClient();
///
/// Post a object as JSON with optional authorization token.
///
/// The type of response object.
/// The request URI.
/// The payload.
/// The authorization.
/// The cancellation token.
///
/// A task with a result of the requested type.
///
public static async Task Post(Uri requestUri, Object payload, String? authorization = null, CancellationToken cancellationToken = default) where T : notnull {
String jsonString = await PostString(requestUri, payload, authorization, cancellationToken).ConfigureAwait(false);
return !String.IsNullOrEmpty(jsonString) ? Json.Deserialize(jsonString) : default;
}
///
/// Posts the specified URL.
///
/// The request URI.
/// The payload.
/// The authorization.
/// The cancellation token.
///
/// A task with a result as a collection of key/value pairs.
///
public static async Task?> Post(Uri requestUri, Object payload, String? authorization = null, CancellationToken cancellationToken = default) {
String jsonString = await PostString(requestUri, payload, authorization, cancellationToken).ConfigureAwait(false);
return String.IsNullOrWhiteSpace(jsonString) ? default : Json.Deserialize(jsonString) as IDictionary;
}
///
/// Posts the specified URL.
///
/// The request URI.
/// The payload.
/// The authorization.
/// The cancellation token.
///
/// A task with a result of the requested string.
///
/// url.
/// Error POST JSON.
public static Task PostString(Uri requestUri, Object payload, String? authorization = null, CancellationToken cancellationToken = default) => SendAsync(HttpMethod.Post, requestUri, payload, authorization, cancellationToken);
///
/// Puts the specified URL.
///
/// The type of response object.
/// The request URI.
/// The payload.
/// The authorization.
/// The cancellation token.
///
/// A task with a result of the requested type.
///
public static async Task Put(Uri requestUri, Object payload, String? authorization = null, CancellationToken ct = default) where T : notnull {
String jsonString = await PutString(requestUri, payload, authorization, ct).ConfigureAwait(false);
return !String.IsNullOrEmpty(jsonString) ? Json.Deserialize(jsonString) : default;
}
///
/// Puts the specified URL.
///
/// The request URI.
/// The payload.
/// The authorization.
/// The cancellation token.
///
/// A task with a result of the requested collection of key/value pairs.
///
public static async Task?> Put(Uri requestUri, Object payload, String? authorization = null, CancellationToken cancellationToken = default) {
Object response = await Put