feat(IHttpClientOptions): Abfrage-Parameter und Header hinzugefügt.
- Geordnete DI-Erweiterungen.
This commit is contained in:
parent
997fd533ac
commit
c65eefb954
@ -2,8 +2,12 @@
|
||||
{
|
||||
public interface IHttpClientOptions
|
||||
{
|
||||
public string Uri { get; set; }
|
||||
string Uri { get; init; }
|
||||
|
||||
public string Path { get; set; }
|
||||
string? Path { get; init; }
|
||||
|
||||
IEnumerable<KeyValuePair<string, object>>? Headers { get; init; }
|
||||
|
||||
IEnumerable<KeyValuePair<string, object?>>? QueryParams { get; init; }
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using DigitalData.Core.Abstractions.Client;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using System.Net;
|
||||
@ -7,30 +8,13 @@ namespace DigitalData.Core.Client
|
||||
{
|
||||
public static class DIExtensions
|
||||
{
|
||||
public static IServiceCollection AddHttpClientService(this IServiceCollection services, string uri, string path = "")
|
||||
public static IServiceCollection AddHttpClientService(this IServiceCollection services, IConfigurationSection? section)
|
||||
{
|
||||
services.TryAddSingleton<HttpClient>();
|
||||
services.TryAddSingleton<CookieContainer>();
|
||||
services.AddSingleton<IBaseHttpClientService, BaseHttpClientService>();
|
||||
services.Configure<HttpClientOptions>(opt =>
|
||||
{
|
||||
opt.Uri = uri;
|
||||
opt.Path = path;
|
||||
});
|
||||
|
||||
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.TryAddSingleton<IHttpClientService<TClientOptions>, HttpClientService<TClientOptions>>();
|
||||
services.Configure(clientOptions ?? (_ => { }));
|
||||
|
||||
if (setAsDefaultBase)
|
||||
services.TryAddSingleton<IBaseHttpClientService, HttpClientService<TClientOptions>>();
|
||||
services.TryAddSingleton<IBaseHttpClientService, BaseHttpClientService>();
|
||||
if(section is not null)
|
||||
services.Configure<HttpClientOptions>(section);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
|
||||
@ -29,6 +29,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@ -4,12 +4,12 @@ namespace DigitalData.Core.Client
|
||||
{
|
||||
public class HttpClientOptions : IHttpClientOptions
|
||||
{
|
||||
public string Uri { get; set; } = string.Empty;
|
||||
public required string Uri { get; init; }
|
||||
|
||||
public string Path { get; set; } = string.Empty;
|
||||
public string? Path { get; init; }
|
||||
|
||||
public IEnumerable<KeyValuePair<string, object>>? Headers { get; init; } = null;
|
||||
public IEnumerable<KeyValuePair<string, object>>? Headers { get; init; }
|
||||
|
||||
public IEnumerable<KeyValuePair<string, object?>>? QueryParams { get; init; } = null;
|
||||
public IEnumerable<KeyValuePair<string, object?>>? QueryParams { get; init; }
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user