refactor: Umbenennung und Konfiguration für Legacy-Projekt
- DigitalData.Core.Client.Legacy wurde in DigitalData.Core.Legacy.Client umbenannt. - Projektdateien von DigitalData.Core.Client wurden nach DigitalData.Core.Legacy.Client kopiert. - DigitalData.Core.Legacy.Client wurde für .NET Framework konfiguriert.
This commit is contained in:
74
DigitalData.Core.Client.Legacy/BaseHttpClientService.cs
Normal file
74
DigitalData.Core.Client.Legacy/BaseHttpClientService.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DigitalData.Core.Legacy.Client
|
||||
{
|
||||
public class BaseHttpClientService
|
||||
{
|
||||
protected readonly HttpClient _client;
|
||||
protected readonly CookieContainer _cookies;
|
||||
|
||||
public string Uri { get; }
|
||||
|
||||
public BaseHttpClientService(HttpClient client, CookieContainer cookieContainer, IOptions<HttpClientOptions> clientOptions)
|
||||
{
|
||||
_client = client;
|
||||
_cookies = cookieContainer;
|
||||
Uri = clientOptions.Value.Uri;
|
||||
}
|
||||
|
||||
public CookieCollection GetCookies(string route = "") => _cookies.GetCookies(uri: new Uri(Uri + route));
|
||||
|
||||
public async Task<HttpResponseMessage> FetchAsync(
|
||||
string route = "",
|
||||
HttpMethod method = null,
|
||||
HttpContent body = null,
|
||||
Dictionary<string, string> form = null,
|
||||
bool sendWithCookie = true,
|
||||
bool saveCookie = true
|
||||
)
|
||||
{
|
||||
// set default HTTP method as GET
|
||||
if(method is null)
|
||||
method = HttpMethod.Get;
|
||||
|
||||
// create URL
|
||||
var requestUriStr = Uri + route;
|
||||
var requestUri = new Uri(requestUriStr);
|
||||
|
||||
var requestMessage = new HttpRequestMessage(method, requestUriStr);
|
||||
|
||||
// Add cookie to request
|
||||
if (sendWithCookie)
|
||||
{
|
||||
var cookieHeader = _cookies.GetCookieHeader(requestUri);
|
||||
if (!string.IsNullOrWhiteSpace(cookieHeader))
|
||||
{
|
||||
requestMessage.Headers.Add("Cookie", cookieHeader);
|
||||
}
|
||||
}
|
||||
|
||||
// Add body content if provided
|
||||
if (body != null && form != null)
|
||||
throw new InvalidOperationException("Body content and form data cannot both be set.");
|
||||
else if (body != null)
|
||||
requestMessage.Content = body;
|
||||
else if (form != null)
|
||||
requestMessage.Content = new FormUrlEncodedContent(form);
|
||||
|
||||
var response = await _client.SendAsync(requestMessage);
|
||||
|
||||
// Add response cookies to cookies
|
||||
if (saveCookie)
|
||||
if (response.Headers.TryGetValues("Set-Cookie", out var cookies))
|
||||
foreach (var cookie in cookies)
|
||||
_cookies.SetCookies(requestUri, cookie);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
DigitalData.Core.Client.Legacy/DIExtensions.cs
Normal file
37
DigitalData.Core.Client.Legacy/DIExtensions.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace DigitalData.Core.Legacy.Client
|
||||
{
|
||||
public static class DIExtensions
|
||||
{
|
||||
public static IServiceCollection AddHttpClientService(this IServiceCollection services, string uri)
|
||||
{
|
||||
services.TryAddSingleton<HttpClient>();
|
||||
services.TryAddSingleton<CookieContainer>();
|
||||
services.AddSingleton<BaseHttpClientService>();
|
||||
services.Configure<HttpClientOptions>(opt => opt.Uri = uri);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHttpClientService<TClientOptions>(this IServiceCollection services, Action<TClientOptions> clientOptions = null, bool setAsDefaultBase = false)
|
||||
where TClientOptions : HttpClientOptions
|
||||
{
|
||||
services.TryAddSingleton<HttpClient>();
|
||||
services.TryAddSingleton<CookieContainer>();
|
||||
services.AddSingleton<HttpClientService<TClientOptions>>();
|
||||
|
||||
if(clientOptions != null)
|
||||
services.Configure(clientOptions);
|
||||
|
||||
if (setAsDefaultBase)
|
||||
services.AddSingleton<BaseHttpClientService, HttpClientService<TClientOptions>>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,14 +40,37 @@
|
||||
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.8.0.0\lib\net462\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Extensions.Options, Version=8.0.0.2, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Extensions.Options.8.0.2\lib\net462\Microsoft.Extensions.Options.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Extensions.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Extensions.Primitives.8.0.0\lib\net462\Microsoft.Extensions.Primitives.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
|
||||
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Numerics" />
|
||||
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
@@ -56,9 +79,16 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DIExtensions.cs" />
|
||||
<Compile Include="HttpClientOptions.cs" />
|
||||
<Compile Include="BaseHttpClientService.cs" />
|
||||
<Compile Include="HttpClientService.cs" />
|
||||
<Compile Include="HttpExtensions.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ServiceFactory.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
7
DigitalData.Core.Client.Legacy/HttpClientOptions.cs
Normal file
7
DigitalData.Core.Client.Legacy/HttpClientOptions.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace DigitalData.Core.Legacy.Client
|
||||
{
|
||||
public class HttpClientOptions
|
||||
{
|
||||
public string Uri { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
14
DigitalData.Core.Client.Legacy/HttpClientService.cs
Normal file
14
DigitalData.Core.Client.Legacy/HttpClientService.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace DigitalData.Core.Legacy.Client
|
||||
{
|
||||
public class HttpClientService<TClientOptions> : BaseHttpClientService
|
||||
where TClientOptions : HttpClientOptions
|
||||
{
|
||||
public HttpClientService(HttpClient client, CookieContainer cookieContainer, IOptions<TClientOptions> clientOptions) : base(client, cookieContainer, clientOptions)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
103
DigitalData.Core.Client.Legacy/HttpExtensions.cs
Normal file
103
DigitalData.Core.Client.Legacy/HttpExtensions.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DigitalData.Core.Legacy
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for HttpClient and HttpResponseMessage.
|
||||
/// </summary>
|
||||
public static class HttpExtensions
|
||||
{
|
||||
/// <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) => JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
|
||||
|
||||
/// <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>
|
||||
/// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,11 @@ using System.Runtime.InteropServices;
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("DigitalData.Core.Client.Legacy")]
|
||||
[assembly: AssemblyTitle("DigitalData.Core.Legacy.Client")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Digital Data GmbH")]
|
||||
[assembly: AssemblyProduct("DigitalData.Core.Client.Legacy")]
|
||||
[assembly: AssemblyProduct("DigitalData.Core.Legacy.Client")]
|
||||
[assembly: AssemblyCopyright("Copyright © Digital Data GmbH 2024")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
26
DigitalData.Core.Client.Legacy/ServiceFactory.cs
Normal file
26
DigitalData.Core.Client.Legacy/ServiceFactory.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
|
||||
namespace DigitalData.Core.Legacy.Client
|
||||
{
|
||||
public static class ServiceFactory
|
||||
{
|
||||
private static readonly IServiceCollection _services = new ServiceCollection();
|
||||
private static readonly Lazy<IServiceProvider> _lazyProvider = new Lazy<IServiceProvider>(Build);
|
||||
|
||||
public static IServiceCollection Services => !_lazyProvider.IsValueCreated
|
||||
? _services
|
||||
: throw new InvalidOperationException(
|
||||
"Service provider has already been created. " +
|
||||
"Further modifications to the service collection are not allowed. " +
|
||||
"This is to ensure that the dependency injection container remains in a consistent state.");
|
||||
|
||||
private static IServiceProvider Build() => _services.BuildServiceProvider();
|
||||
|
||||
public static T Provide<T>() => _lazyProvider.Value.GetRequiredService<T>();
|
||||
|
||||
public static HttpClientService<TOptions> ProvideHttpClientService<TOptions>()
|
||||
where TOptions : HttpClientOptions
|
||||
=> _lazyProvider.Value.GetRequiredService<HttpClientService<TOptions>>();
|
||||
}
|
||||
}
|
||||
11
DigitalData.Core.Client.Legacy/app.config
Normal file
11
DigitalData.Core.Client.Legacy/app.config
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<runtime>
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
</configuration>
|
||||
@@ -3,6 +3,13 @@
|
||||
<package id="Microsoft.Bcl.AsyncInterfaces" version="8.0.0" targetFramework="net462" />
|
||||
<package id="Microsoft.Extensions.DependencyInjection" version="8.0.0" targetFramework="net462" />
|
||||
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="8.0.0" targetFramework="net462" />
|
||||
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net462" />
|
||||
<package id="Microsoft.Extensions.Options" version="8.0.2" targetFramework="net462" />
|
||||
<package id="Microsoft.Extensions.Primitives" version="8.0.0" targetFramework="net462" />
|
||||
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net462" />
|
||||
<package id="System.Buffers" version="4.5.1" targetFramework="net462" />
|
||||
<package id="System.Memory" version="4.5.5" targetFramework="net462" />
|
||||
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net462" />
|
||||
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net462" />
|
||||
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net462" />
|
||||
<package id="System.ValueTuple" version="4.5.0" targetFramework="net462" />
|
||||
</packages>
|
||||
@@ -17,7 +17,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Client", "
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DigitalData.Core.Abstractions", "DigitalData.Core.Abstractions\DigitalData.Core.Abstractions.csproj", "{13E40DF1-6123-4838-9BF8-086C94E6ADF6}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Client.Legacy", "DigitalData.Core.Client.Legacy\DigitalData.Core.Client.Legacy.csproj", "{E009A053-A9F4-48F2-984F-EF5C376A9B14}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Legacy.Client", "DigitalData.Core.Client.Legacy\DigitalData.Core.Legacy.Client.csproj", "{E009A053-A9F4-48F2-984F-EF5C376A9B14}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
||||
Reference in New Issue
Block a user