// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
namespace EpicGames.Core
{
///
/// Extension methods for consuming REST APIs via JSON objects
///
public static class HttpClientExtensions
{
///
/// Gets a resource from an HTTP endpoint and parses it as a JSON object
///
/// The object type to return
/// The http client instance
/// The url to retrieve
/// Cancels the request
/// New instance of the object
public static async Task GetAsync(this HttpClient Client, string Url, CancellationToken CancellationToken)
{
using (HttpResponseMessage Response = await Client.GetAsync(Url, CancellationToken))
{
Response.EnsureSuccessStatusCode();
return await ParseJsonContent(Response);
}
}
///
/// Posts an object to an HTTP endpoint as a JSON object
///
/// The object type to post
/// The http client instance
/// The url to post to
/// The object to post
/// Cancels the request
/// Response message
public static async Task PostAsync(this HttpClient Client, string Url, TRequest Object, CancellationToken CancellationToken)
{
return await Client.PostAsync(Url, ToJsonContent(Object), CancellationToken);
}
///
/// Posts an object to an HTTP endpoint as a JSON object, and parses the response object
///
/// The object type to return
/// The object type to post
/// The http client instance
/// The url to post to
/// The object to post
/// Cancels the request
/// The response parsed into the requested type
public static async Task PostAsync(this HttpClient Client, string Url, TRequest Request, CancellationToken CancellationToken)
{
using (HttpResponseMessage Response = await PostAsync(Client, Url, Request, CancellationToken))
{
Response.EnsureSuccessStatusCode();
return await ParseJsonContent(Response);
}
}
///
/// Puts an object to an HTTP endpoint as a JSON object
///
/// The object type to post
/// The http client instance
/// The url to post to
/// The object to post
/// Cancels the request
/// Response message
public static async Task PutAsync(this HttpClient Client, string Url, TRequest Object, CancellationToken CancellationToken)
{
return await Client.PutAsync(Url, ToJsonContent(Object), CancellationToken);
}
///
/// Puts an object to an HTTP endpoint as a JSON object, and parses the response object
///
/// The object type to return
/// The object type to post
/// The http client instance
/// The url to post to
/// The object to post
/// Cancels the request
/// The response parsed into the requested type
public static async Task PutAsync(this HttpClient Client, string Url, TRequest Request, CancellationToken CancellationToken)
{
using (HttpResponseMessage Response = await PutAsync(Client, Url, Request, CancellationToken))
{
Response.EnsureSuccessStatusCode();
return await ParseJsonContent(Response);
}
}
///
/// Converts an object to a JSON http content object
///
/// Type of the object to parse
/// The object instance
/// Http content object
private static HttpContent ToJsonContent(T Object)
{
return new StringContent(JsonSerializer.Serialize(Object), Encoding.UTF8, "application/json");
}
///
/// Parses a HTTP response as a JSON object
///
/// Type of the object to parse
/// The message received
/// Parsed object instance
private static async Task ParseJsonContent(HttpResponseMessage Message)
{
byte[] Bytes = await Message.Content.ReadAsByteArrayAsync();
return JsonSerializer.Deserialize(Bytes);
}
}
}