namespace Swan.Net
{
using 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;
///
/// 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)
{
var 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)
{
var 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)
{
var 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)
{
var response = await Put