using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; namespace DigitalData.Core.Legacy.Client { /// /// Extension methods for HttpClient and HttpResponseMessage. /// public static class HttpExtensions { /// /// Reads the content of the HTTP response message as a string. /// /// The HTTP response message. /// A task representing the response content as a string. public static async Task Text(this HttpResponseMessage response) => await response.Content.ReadAsStringAsync(); /// /// Reads the content of the HTTP response message and deserializes it to a specified type. /// /// The type to deserialize the response content to. /// The HTTP response message. /// A task representing the deserialized response content. public static async Task Json(this HttpResponseMessage response) => JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); /// /// Reads the content of the HTTP response message and deserializes it to a dynamic object. /// /// The HTTP response message. /// A task representing the deserialized response content as a dynamic object. public static async Task Json(this HttpResponseMessage response) { string json = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(json) ?? string.Empty; } /// /// Reads the content of the HTTP response message and deserializes it to a list of dynamic objects. /// /// The HTTP response message. /// A task representing the deserialized response content as a list of dynamic objects. public static async Task> JsonList(this HttpResponseMessage response) { string json = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(json) ?? string.Empty; } /// /// Executes an action when a task is completed. /// /// The type of the task result. /// The task to await. /// The action to execute when the task is completed. public static async void ThenAsync(this Task task, Action toDo) { var then = await task; toDo(then); } /// /// Executes a function when a task is completed and returns the result. /// /// The type of the task result. /// The return type of the function. /// The task to await. /// The function to execute when the task is completed. /// A task representing the result of the function. public static async Task ThenAsync(this Task task, Func toDo) { var then = await task; return toDo(then); } /// /// Executes an asynchronous function when a task is completed and returns the result. /// /// The type of the task result. /// The return type of the asynchronous function. /// The task to await. /// The asynchronous function to execute when the task is completed. /// A task representing the result of the asynchronous function. public static async Task ThenAsync(this Task task, Func> toDoAsync) { var then = await task; return await toDoAsync(then); } /// /// Executes an action for each element in an enumerable collection. /// /// The type of the elements in the collection. /// The enumerable collection of elements. /// The action to execute for each element. public static void ForEach(this IEnumerable values, Action action) { foreach (var value in values) action(value); } } }