using Newtonsoft.Json; using System.Net.Http.Json; using static System.Net.WebRequestMethods; namespace DigitalData.Core.Client { public static class HttpExtensions { public static async Task Text(this HttpResponseMessage response) => await response.Content.ReadAsStringAsync(); public static async Task Json(this HttpResponseMessage response) => await response.Content.ReadFromJsonAsync(); public static async Task Json(this HttpResponseMessage response) { string json = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(json) ?? string.Empty; } public static async Task> JsonList(this HttpResponseMessage response) { string json = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(json) ?? string.Empty; } public static async Task Fetch(this string url, string method = Http.Get) { using HttpClient client = new(); return method switch { Http.Get => await client.GetAsync(url), _ => throw new NotImplementedException(nameof(method)), }; } public static async void ThenAsync(this Task task, Action toDo) { var then = await task; toDo(then); } public static async Task ThenAsync(this Task task, Func toDo) { var then = await task; return toDo(then); } public static async Task ThenAsync(this Task task, Func> toDoAsync) { var then = await task; return await toDoAsync(then); } public static void ForEach(this IEnumerable values, Action action) { foreach (var value in values) action(value); } } }