feat(IHttpClientOptions):
Basispfad zu http-Client-Optionen hinzugefügt
This commit is contained in:
@@ -9,24 +9,27 @@ namespace DigitalData.Core.Client
|
||||
public class BaseHttpClientService : IBaseHttpClientService
|
||||
{
|
||||
protected readonly HttpClient _client;
|
||||
protected readonly CookieContainer _cookies;
|
||||
protected readonly CookieContainer _cookies;
|
||||
|
||||
[StringSyntax("Uri")]
|
||||
public string Uri { get; init; }
|
||||
|
||||
public string Path { get; init; } = string.Empty;
|
||||
|
||||
public BaseHttpClientService(HttpClient client, CookieContainer cookieContainer, IOptions<HttpClientOptions> clientOptions)
|
||||
{
|
||||
_client = client;
|
||||
_cookies = cookieContainer;
|
||||
Uri = clientOptions.Value.Uri;
|
||||
Uri = clientOptions.Value.Uri.Trim(URI_TRIM_CHARS);
|
||||
Path = clientOptions.Value.Path.Trim(URI_TRIM_CHARS);
|
||||
}
|
||||
|
||||
public CookieCollection GetCookies(string route = "") => _cookies.GetCookies(uri: new Uri(Uri + route));
|
||||
public CookieCollection GetCookies(string path = "") => _cookies.GetCookies(uri: new Uri(UriCombine(Uri, path, path.Trim(URI_TRIM_CHARS))));
|
||||
|
||||
public async Task<HttpResponseMessage> FetchAsync(
|
||||
string? scheme = null,
|
||||
int? port = null,
|
||||
string? path = null,
|
||||
string path = "",
|
||||
Dictionary<string, string>? queryParams = null,
|
||||
HttpMethod? method = null,
|
||||
HttpContent? body = null,
|
||||
@@ -41,11 +44,11 @@ namespace DigitalData.Core.Client
|
||||
|
||||
// create URL
|
||||
var uriBuilder = new UriBuilder(Uri);
|
||||
if(scheme is not null)
|
||||
if (scheme is not null)
|
||||
uriBuilder.Scheme = scheme;
|
||||
if (port is int portInt)
|
||||
uriBuilder.Port = portInt;
|
||||
uriBuilder.Path = path;
|
||||
uriBuilder.Path = UriCombine(Path, path.Trim(URI_TRIM_CHARS));
|
||||
|
||||
// Add query parameters if provided
|
||||
if (queryParams?.Count > 0)
|
||||
@@ -92,5 +95,9 @@ namespace DigitalData.Core.Client
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
internal static readonly char[] URI_TRIM_CHARS = { '\\', '/', ' ' };
|
||||
|
||||
internal static string UriCombine(params string[] paths) => System.IO.Path.Combine(paths).Replace("\\", "/");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user