From c65eefb9540b8f8bcef9e911cd2c55f5827261a4 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 25 Nov 2024 10:30:33 +0100 Subject: [PATCH] =?UTF-8?q?feat(IHttpClientOptions):=20Abfrage-Parameter?= =?UTF-8?q?=20und=20Header=20hinzugef=C3=BCgt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Geordnete DI-Erweiterungen. --- .../Client/IHttpClientOptions.cs | 8 ++++-- DigitalData.Core.Client/DIExtensions.cs | 26 ++++--------------- .../DigitalData.Core.Client.csproj | 3 ++- DigitalData.Core.Client/HttpClientOptions.cs | 8 +++--- 4 files changed, 17 insertions(+), 28 deletions(-) diff --git a/DigitalData.Core.Abstractions/Client/IHttpClientOptions.cs b/DigitalData.Core.Abstractions/Client/IHttpClientOptions.cs index ba6a311..5600249 100644 --- a/DigitalData.Core.Abstractions/Client/IHttpClientOptions.cs +++ b/DigitalData.Core.Abstractions/Client/IHttpClientOptions.cs @@ -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>? Headers { get; init; } + + IEnumerable>? QueryParams { get; init; } } } \ No newline at end of file diff --git a/DigitalData.Core.Client/DIExtensions.cs b/DigitalData.Core.Client/DIExtensions.cs index 8291df2..4ec3b4c 100644 --- a/DigitalData.Core.Client/DIExtensions.cs +++ b/DigitalData.Core.Client/DIExtensions.cs @@ -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(); services.TryAddSingleton(); - services.AddSingleton(); - services.Configure(opt => - { - opt.Uri = uri; - opt.Path = path; - }); - - return services; - } - - public static IServiceCollection AddHttpClientService(this IServiceCollection services, Action? clientOptions = null, bool setAsDefaultBase = false) - where TClientOptions : HttpClientOptions - { - services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton, HttpClientService>(); - services.Configure(clientOptions ?? (_ => { })); - - if (setAsDefaultBase) - services.TryAddSingleton>(); + services.TryAddSingleton(); + if(section is not null) + services.Configure(section); return services; } diff --git a/DigitalData.Core.Client/DigitalData.Core.Client.csproj b/DigitalData.Core.Client/DigitalData.Core.Client.csproj index 34f4596..9baa91b 100644 --- a/DigitalData.Core.Client/DigitalData.Core.Client.csproj +++ b/DigitalData.Core.Client/DigitalData.Core.Client.csproj @@ -1,4 +1,4 @@ - + net7.0;net8.0 @@ -29,6 +29,7 @@ + diff --git a/DigitalData.Core.Client/HttpClientOptions.cs b/DigitalData.Core.Client/HttpClientOptions.cs index 050855c..da859e9 100644 --- a/DigitalData.Core.Client/HttpClientOptions.cs +++ b/DigitalData.Core.Client/HttpClientOptions.cs @@ -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>? Headers { get; init; } = null; + public IEnumerable>? Headers { get; init; } - public IEnumerable>? QueryParams { get; init; } = null; + public IEnumerable>? QueryParams { get; init; } } } \ No newline at end of file