feat(BaseHttpClientService): Optionale Standard-Header und QueryParams hinzugefügt

This commit is contained in:
Developer 02 2024-11-25 10:05:42 +01:00
parent 0334fc4cdf
commit 049e9977f4
2 changed files with 26 additions and 0 deletions

View File

@ -16,12 +16,18 @@ namespace DigitalData.Core.Client
public string Path { get; init; } = string.Empty;
protected IEnumerable<KeyValuePair<string, object>>? _headers;
protected IEnumerable<KeyValuePair<string, object?>>? _queryParams;
public BaseHttpClientService(HttpClient client, CookieContainer cookieContainer, IOptions<HttpClientOptions> clientOptions)
{
_client = client;
_cookies = cookieContainer;
Uri = clientOptions.Value.Uri.Trim(URI_TRIM_CHARS);
Path = clientOptions.Value.Path.Trim(URI_TRIM_CHARS);
_headers = clientOptions.Value.Headers;
_queryParams = clientOptions.Value.QueryParams;
}
public CookieCollection GetCookies(string path = "") => _cookies.GetCookies(uri: new Uri(UriCombine(Uri, path, path.Trim(URI_TRIM_CHARS))));
@ -39,6 +45,22 @@ namespace DigitalData.Core.Client
bool saveCookie = true
)
{
// merge with default headers
if(_headers is not null)
{
var mergedHeaders = headers?.ToList() ?? new List<KeyValuePair<string, object>>();
mergedHeaders.AddRange(_headers);
headers = mergedHeaders;
}
// Add default query parameters
if(_queryParams is not null)
{
var mergedQueryParams = queryParams?.ToList() ?? new List<KeyValuePair<string, object?>>();
mergedQueryParams.AddRange(_queryParams);
queryParams = mergedQueryParams;
}
// set default HTTP method as GET
method ??= HttpMethod.Get;

View File

@ -7,5 +7,9 @@ namespace DigitalData.Core.Client
public string Uri { get; set; } = string.Empty;
public string Path { get; set; } = string.Empty;
public IEnumerable<KeyValuePair<string, object>>? Headers { get; init; } = null;
public IEnumerable<KeyValuePair<string, object?>>? QueryParams { get; init; } = null;
}
}