feat(BaseHttpClientService.FetchAsync): Schema-, Port-, Pfad- und Query-Parameter-Optionen hinzugefügt
This commit is contained in:
parent
e44b2895c9
commit
48afa6b433
@ -4,12 +4,15 @@ namespace DigitalData.Core.Abstractions.Client
|
||||
{
|
||||
public interface IBaseHttpClientService
|
||||
{
|
||||
public string Uri { get; init; }
|
||||
string Uri { get; init; }
|
||||
|
||||
public CookieCollection GetCookies(string route = "");
|
||||
CookieCollection GetCookies(string route = "");
|
||||
|
||||
Task<HttpResponseMessage> FetchAsync(
|
||||
string route = "",
|
||||
string? scheme = null,
|
||||
int? port = null,
|
||||
string? path = null,
|
||||
Dictionary<string, string>? queryParams = null,
|
||||
HttpMethod? method = null,
|
||||
HttpContent? body = null,
|
||||
Dictionary<string, string>? form = null,
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Web;
|
||||
|
||||
namespace DigitalData.Core.Client
|
||||
{
|
||||
@ -23,7 +24,10 @@ namespace DigitalData.Core.Client
|
||||
public CookieCollection GetCookies(string route = "") => _cookies.GetCookies(uri: new Uri(Uri + route));
|
||||
|
||||
public async Task<HttpResponseMessage> FetchAsync(
|
||||
string route = "",
|
||||
string? scheme = null,
|
||||
int? port = null,
|
||||
string? path = null,
|
||||
Dictionary<string, string>? queryParams = null,
|
||||
HttpMethod? method = null,
|
||||
HttpContent? body = null,
|
||||
Dictionary<string, string>? form = null,
|
||||
@ -36,10 +40,26 @@ namespace DigitalData.Core.Client
|
||||
method ??= HttpMethod.Get;
|
||||
|
||||
// create URL
|
||||
var requestUriStr = Uri + route;
|
||||
var requestUri = new Uri(requestUriStr);
|
||||
var uriBuilder = new UriBuilder(Uri);
|
||||
if(scheme is not null)
|
||||
uriBuilder.Scheme = scheme;
|
||||
if (port is int portInt)
|
||||
uriBuilder.Port = portInt;
|
||||
uriBuilder.Path = path;
|
||||
|
||||
var requestMessage = new HttpRequestMessage(method, requestUriStr);
|
||||
// Add query parameters if provided
|
||||
if (queryParams?.Count > 0)
|
||||
{
|
||||
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
|
||||
foreach (var param in queryParams)
|
||||
query[param.Key] = param.Value;
|
||||
|
||||
uriBuilder.Query = query.ToString();
|
||||
}
|
||||
|
||||
var requestUri = uriBuilder.Uri;
|
||||
|
||||
var requestMessage = new HttpRequestMessage(method, requestUri);
|
||||
|
||||
// Add headers if provided
|
||||
headers?.ForEach(header => requestMessage.Headers.Add(header.Key, header.Value));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user