namespace Unosquare.Swan.Networking
{
using System;
using Exceptions;
using Models;
using Formatters;
using System.Collections.Generic;
using System.Net.Http;
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";
///
/// Post a object as JSON with optional authorization token.
///
/// The type of response object.
/// The URL.
/// The payload.
/// The authorization.
/// The cancellation token.
/// A task with a result of the requested type.
public static async Task Post(
string url,
object payload,
string authorization = null,
CancellationToken ct = default)
{
var jsonString = await PostString(url, payload, authorization, ct).ConfigureAwait(false);
return !string.IsNullOrEmpty(jsonString) ? Json.Deserialize(jsonString) : default;
}
///
/// Posts a object as JSON with optional authorization token and retrieve an object
/// or an error.
///
/// The type of response object.
/// The type of the error.
/// The URL.
/// The payload.
/// The HTTP status error.
/// The authorization.
/// The cancellation token.
/// A task with a result of the requested type or an error object.
public static async Task> PostOrError(
string url,
object payload,
int httpStatusError = 500,
string authorization = null,
CancellationToken ct = default)
{
using (var httpClient = GetHttpClientWithAuthorizationHeader(authorization))
{
var payloadJson = new StringContent(Json.Serialize(payload), Encoding.UTF8, JsonMimeType);
var response = await httpClient.PostAsync(url, payloadJson, ct).ConfigureAwait(false);
var jsonString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return OkOrError.FromOk(!string.IsNullOrEmpty(jsonString)
? Json.Deserialize(jsonString)
: default);
}
if ((int) response.StatusCode == httpStatusError)
{
return OkOrError.FromError(!string.IsNullOrEmpty(jsonString)
? Json.Deserialize(jsonString)
: default);
}
return new OkOrError();
}
}
///
/// Posts the specified URL.
///
/// The URL.
/// The payload.
/// The authorization.
/// The cancellation token.
/// A task with a result as a collection of key/value pairs.
public static async Task> Post(
string url,
object payload,
string authorization = null,
CancellationToken ct = default)
{
var jsonString = await PostString(url, payload, authorization, ct).ConfigureAwait(false);
return string.IsNullOrWhiteSpace(jsonString)
? default
: Json.Deserialize(jsonString) as IDictionary;
}
///
/// Posts the specified URL.
///
/// The URL.
/// The payload.
/// The authorization.
/// The cancellation token.
///
/// A task with a result of the requested string.
///
/// url.
/// Error POST JSON.
public static Task PostString(
string url,
object payload,
string authorization = null,
CancellationToken ct = default) => SendAsync(HttpMethod.Post, url, payload, authorization, ct);
///
/// Puts the specified URL.
///
/// The type of response object.
/// The URL.
/// The payload.
/// The authorization.
/// The cancellation token.
/// A task with a result of the requested type.
public static async Task Put(
string url,
object payload,
string authorization = null,
CancellationToken ct = default)
{
var jsonString = await PutString(url, payload, authorization, ct).ConfigureAwait(false);
return !string.IsNullOrEmpty(jsonString) ? Json.Deserialize(jsonString) : default;
}
///
/// Puts the specified URL.
///
/// The URL.
/// 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(
string url,
object payload,
string authorization = null,
CancellationToken ct = default)
{
var response = await Put