using Newtonsoft.Json;
using System.Net.Http.Json;
namespace DigitalData.Core.Client
{
///
/// Extension methods for HttpClient and HttpResponseMessage.
///
public static class HttpExtensions
{
///
/// Fetches data from the specified URL using the given HTTP method.
///
/// The URL to fetch data from.
/// The HTTP method to use for the request. Defaults to GET.
/// A task representing the HTTP response message.
public static async Task Fetch(this string url, Method method = Method.GET)
{
using HttpClient client = new();
return method switch
{
Method.GET => await client.GetAsync(url),
_ => throw new NotImplementedException(nameof(method)),
};
}
///
/// 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) => await response.Content.ReadFromJsonAsync();
///
/// 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;
}
///
/// Fetches data from the specified URL using the given HTTP method and reads the response content as a string.
///
/// The URL to fetch data from.
/// The HTTP method to use for the request. Defaults to GET.
/// A task representing the response content as a string.
public static async Task FetchText(this string url, Method method = Method.GET) => await url.Fetch(method: method).ThenAsync(Text);
///
/// Fetches data from the specified URL using the given HTTP method and deserializes the response content to a specified type.
///
/// The type to deserialize the response content to.
/// The URL to fetch data from.
/// The HTTP method to use for the request. Defaults to GET.
/// A task representing the deserialized response content.
public static async Task FetchJson(this string url, Method method = Method.GET) => await url.Fetch(method: method).ThenAsync(Json);
///
/// Fetches data from the specified URL using the given HTTP method and deserializes the response content to a dynamic object.
///
/// The URL to fetch data from.
/// The HTTP method to use for the request. Defaults to GET.
/// A task representing the deserialized response content as a dynamic object.
public static async Task FetchJson(this string url, Method method = Method.GET) => await url.Fetch(method: method).ThenAsync(Json);
///
/// Fetches data from the specified URL using the given HTTP method and deserializes the response content to a list of dynamic objects.
///
/// The URL to fetch data from.
/// The HTTP method to use for the request. Defaults to GET.
/// A task representing the deserialized response content as a list of dynamic objects.
public static async Task> FetchJsonList(this string url, Method method = Method.GET) => await url.Fetch(method: method).ThenAsync(JsonList);
///
/// 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);
}
}
}