// Copyright Epic Games, Inc. All Rights Reserved. using System.Net.Http; using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; 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 request, CancellationToken cancellationToken) { return await client.PostAsync(url, ToJsonContent(request), 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 obj, CancellationToken cancellationToken) { return await client.PutAsync(url, ToJsonContent(obj), 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 obj) { return new StringContent(JsonSerializer.Serialize(obj), 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, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); } } }