150 lines
7.9 KiB
C#
150 lines
7.9 KiB
C#
using Newtonsoft.Json;
|
|
using System.Net.Http.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DigitalData.Core.Client
|
|
{
|
|
/// <summary>
|
|
/// Extension methods for HttpClient and HttpResponseMessage.
|
|
/// </summary>
|
|
public static class HttpExtensions
|
|
{
|
|
/// <summary>
|
|
/// Fetches data from the specified URL using the given HTTP method.
|
|
/// </summary>
|
|
/// <param name="url">The URL to fetch data from.</param>
|
|
/// <param name="method">The HTTP method to use for the request. Defaults to GET.</param>
|
|
/// <returns>A task representing the HTTP response message.</returns>
|
|
public static async Task<HttpResponseMessage> 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)),
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads the content of the HTTP response message as a string.
|
|
/// </summary>
|
|
/// <param name="response">The HTTP response message.</param>
|
|
/// <returns>A task representing the response content as a string.</returns>
|
|
public static async Task<string> Text(this HttpResponseMessage response) => await response.Content.ReadAsStringAsync();
|
|
|
|
/// <summary>
|
|
/// Reads the content of the HTTP response message and deserializes it to a specified type.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type to deserialize the response content to.</typeparam>
|
|
/// <param name="response">The HTTP response message.</param>
|
|
/// <returns>A task representing the deserialized response content.</returns>
|
|
public static async Task<T?> Json<T>(this HttpResponseMessage response) => await response.Content.ReadFromJsonAsync<T>();
|
|
|
|
/// <summary>
|
|
/// Reads the content of the HTTP response message and deserializes it to a dynamic object.
|
|
/// </summary>
|
|
/// <param name="response">The HTTP response message.</param>
|
|
/// <returns>A task representing the deserialized response content as a dynamic object.</returns>
|
|
public static async Task<dynamic> Json(this HttpResponseMessage response)
|
|
{
|
|
string json = await response.Content.ReadAsStringAsync();
|
|
return JsonConvert.DeserializeObject<dynamic>(json) ?? string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads the content of the HTTP response message and deserializes it to a list of dynamic objects.
|
|
/// </summary>
|
|
/// <param name="response">The HTTP response message.</param>
|
|
/// <returns>A task representing the deserialized response content as a list of dynamic objects.</returns>
|
|
public static async Task<IEnumerable<dynamic>> JsonList(this HttpResponseMessage response)
|
|
{
|
|
string json = await response.Content.ReadAsStringAsync();
|
|
return JsonConvert.DeserializeObject<dynamic>(json) ?? string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetches data from the specified URL using the given HTTP method and reads the response content as a string.
|
|
/// </summary>
|
|
/// <param name="url">The URL to fetch data from.</param>
|
|
/// <param name="method">The HTTP method to use for the request. Defaults to GET.</param>
|
|
/// <returns>A task representing the response content as a string.</returns>
|
|
public static async Task<string> FetchText(this string url, Method method = Method.GET) => await url.Fetch(method: method).ThenAsync(Text);
|
|
|
|
/// <summary>
|
|
/// Fetches data from the specified URL using the given HTTP method and deserializes the response content to a specified type.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type to deserialize the response content to.</typeparam>
|
|
/// <param name="url">The URL to fetch data from.</param>
|
|
/// <param name="method">The HTTP method to use for the request. Defaults to GET.</param>
|
|
/// <returns>A task representing the deserialized response content.</returns>
|
|
public static async Task<T?> FetchJson<T>(this string url, Method method = Method.GET) => await url.Fetch(method: method).ThenAsync(Json<T>);
|
|
|
|
/// <summary>
|
|
/// Fetches data from the specified URL using the given HTTP method and deserializes the response content to a dynamic object.
|
|
/// </summary>
|
|
/// <param name="url">The URL to fetch data from.</param>
|
|
/// <param name="method">The HTTP method to use for the request. Defaults to GET.</param>
|
|
/// <returns>A task representing the deserialized response content as a dynamic object.</returns>
|
|
public static async Task<dynamic> FetchJson(this string url, Method method = Method.GET) => await url.Fetch(method: method).ThenAsync(Json);
|
|
|
|
/// <summary>
|
|
/// Fetches data from the specified URL using the given HTTP method and deserializes the response content to a list of dynamic objects.
|
|
/// </summary>
|
|
/// <param name="url">The URL to fetch data from.</param>
|
|
/// <param name="method">The HTTP method to use for the request. Defaults to GET.</param>
|
|
/// <returns>A task representing the deserialized response content as a list of dynamic objects.</returns>
|
|
public static async Task<IEnumerable<dynamic>> FetchJsonList(this string url, Method method = Method.GET) => await url.Fetch(method: method).ThenAsync(JsonList);
|
|
|
|
/// <summary>
|
|
/// Executes an action when a task is completed.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the task result.</typeparam>
|
|
/// <param name="task">The task to await.</param>
|
|
/// <param name="toDo">The action to execute when the task is completed.</param>
|
|
public static async void ThenAsync<T>(this Task<T> task, Action<T> toDo)
|
|
{
|
|
var then = await task;
|
|
toDo(then);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes a function when a task is completed and returns the result.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the task result.</typeparam>
|
|
/// <typeparam name="I">The return type of the function.</typeparam>
|
|
/// <param name="task">The task to await.</param>
|
|
/// <param name="toDo">The function to execute when the task is completed.</param>
|
|
/// <returns>A task representing the result of the function.</returns>
|
|
public static async Task<I> ThenAsync<T, I>(this Task<T> task, Func<T, I> toDo)
|
|
{
|
|
var then = await task;
|
|
return toDo(then);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes an asynchronous function when a task is completed and returns the result.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the task result.</typeparam>
|
|
/// <typeparam name="I">The return type of the asynchronous function.</typeparam>
|
|
/// <param name="task">The task to await.</param>
|
|
/// <param name="toDoAsync">The asynchronous function to execute when the task is completed.</param>
|
|
/// <returns>A task representing the result of the asynchronous function.</returns>
|
|
public static async Task<I> ThenAsync<T, I>(this Task<T> task, Func<T, Task<I>> toDoAsync)
|
|
{
|
|
var then = await task;
|
|
return await toDoAsync(then);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes an action for each element in an enumerable collection.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the elements in the collection.</typeparam>
|
|
/// <param name="values">The enumerable collection of elements.</param>
|
|
/// <param name="action">The action to execute for each element.</param>
|
|
public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
|
|
{
|
|
foreach (var value in values)
|
|
action(value);
|
|
}
|
|
}
|
|
} |