Erweiterungsmethoden für HttpResponseMessage hinzugefügt, um JSON und dynamische Antworten zu verarbeiten. Fetch-Methode implementiert, um HTTP-GET-Anfragen auszuführen. ThenAsync für Task-Fortsetzungen und ForEach für IEnumerable-Iteration hinzugefügt.
This commit is contained in:
parent
a01cb0e2a8
commit
73ea081f7d
13
DigitalData.Core.Client/DigitalData.Core.Client.csproj
Normal file
13
DigitalData.Core.Client/DigitalData.Core.Client.csproj
Normal file
@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
59
DigitalData.Core.Client/HttpExtensions.cs
Normal file
59
DigitalData.Core.Client/HttpExtensions.cs
Normal file
@ -0,0 +1,59 @@
|
||||
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<string> Text(this HttpResponseMessage response) => await response.Content.ReadAsStringAsync();
|
||||
|
||||
public static async Task<T?> Json<T>(this HttpResponseMessage response) => await response.Content.ReadFromJsonAsync<T>();
|
||||
|
||||
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<IEnumerable<dynamic>> JsonList(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)),
|
||||
};
|
||||
}
|
||||
|
||||
public static async void ThenAsync<T>(this Task<T> task, Action<T> toDo)
|
||||
{
|
||||
var then = await task;
|
||||
toDo(then);
|
||||
}
|
||||
|
||||
public static async Task<I> ThenAsync<T, I>(this Task<T> task, Func<T, I> toDo)
|
||||
{
|
||||
var then = await task;
|
||||
return toDo(then);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
|
||||
{
|
||||
foreach (var value in values)
|
||||
action(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
12
DigitalData.Core.Tests/Client/HttpExtensionsTest.cs
Normal file
12
DigitalData.Core.Tests/Client/HttpExtensionsTest.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DigitalData.Core.Tests.Client
|
||||
{
|
||||
internal class HttpExtensionsTest
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Tests", "D
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.DTO", "DigitalData.Core.DTO\DigitalData.Core.DTO.csproj", "{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Client", "DigitalData.Core.Client\DigitalData.Core.Client.csproj", "{6A80FFEC-9B83-40A7-8C78-124440B48B33}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -45,6 +47,10 @@ Global
|
||||
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0B051A5F-BD38-47D1-BAFF-D44BA30D3FB7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6A80FFEC-9B83-40A7-8C78-124440B48B33}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user