Kommentare zur Dokumentation hinzugefügt und Pakete konfiguriert.

This commit is contained in:
Developer 02
2024-06-20 16:20:50 +02:00
parent b7584a1632
commit 0ad92e7592
19 changed files with 588 additions and 56 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1020 KiB

View File

@@ -4,10 +4,35 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Description>This package provides HTTP client extension methods for the DigitalData.Core library, offering simplified and asynchronous methods for fetching and handling HTTP responses. It includes utility methods for sending GET requests, reading response content as text or JSON, and deserializing JSON into dynamic or strongly-typed objects using Newtonsoft.Json. These extensions facilitate efficient and easy-to-read HTTP interactions in client applications.</Description>
<PackageId>DigitalData.Core.Client</PackageId>
<Version>1.0.0</Version>
<Authors>Digital Data GmbH</Authors>
<Company>Digital Data GmbH</Company>
<Product>Digital Data GmbH</Product>
<Copyright>Copyright 2024</Copyright>
<PackageProjectUrl></PackageProjectUrl>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackageTags>digital data core http client json serilization</PackageTags>
</PropertyGroup>
<ItemGroup>
<None Include="..\DigitalData.Core.Abstractions\Assets\icon.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<None Update="Assets\icon.png">
<PackagePath>\</PackagePath>
<Pack>True</Pack>
</None>
</ItemGroup>
</Project>

View File

@@ -1,59 +1,150 @@
using Newtonsoft.Json;
using System.Net.Http.Json;
using static System.Net.WebRequestMethods;
using System.Threading.Tasks;
namespace DigitalData.Core.Client
{
public static class HttpExtensions
{
public static async Task<string> Text(this HttpResponseMessage response) => await response.Content.ReadAsStringAsync();
/// <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)),
};
}
public static async Task<T?> Json<T>(this HttpResponseMessage response) => await response.Content.ReadFromJsonAsync<T>();
/// <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();
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 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>();
public static async Task<IEnumerable<dynamic>> JsonList(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 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;
}
public static async Task<HttpResponseMessage> 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)),
};
}
/// <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;
}
public static async void ThenAsync<T>(this Task<T> task, Action<T> toDo)
{
var then = await task;
toDo(then);
}
/// <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);
public static async Task<I> ThenAsync<T, I>(this Task<T> task, Func<T, I> toDo)
{
var then = await task;
return toDo(then);
}
/// <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>);
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>
/// 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);
public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
{
foreach (var value in values)
action(value);
}
}
/// <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);
}
}
}

View File

@@ -0,0 +1,7 @@
namespace DigitalData.Core.Client
{
public enum Method
{
GET
}
}