feat(BaseHttpClientService): Optionale Standard-Header und QueryParams hinzugefügt
This commit is contained in:
parent
0334fc4cdf
commit
049e9977f4
@ -16,12 +16,18 @@ namespace DigitalData.Core.Client
|
|||||||
|
|
||||||
public string Path { get; init; } = string.Empty;
|
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)
|
public BaseHttpClientService(HttpClient client, CookieContainer cookieContainer, IOptions<HttpClientOptions> clientOptions)
|
||||||
{
|
{
|
||||||
_client = client;
|
_client = client;
|
||||||
_cookies = cookieContainer;
|
_cookies = cookieContainer;
|
||||||
Uri = clientOptions.Value.Uri.Trim(URI_TRIM_CHARS);
|
Uri = clientOptions.Value.Uri.Trim(URI_TRIM_CHARS);
|
||||||
Path = clientOptions.Value.Path.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))));
|
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
|
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
|
// set default HTTP method as GET
|
||||||
method ??= HttpMethod.Get;
|
method ??= HttpMethod.Get;
|
||||||
|
|
||||||
|
|||||||
@ -7,5 +7,9 @@ namespace DigitalData.Core.Client
|
|||||||
public string Uri { get; set; } = string.Empty;
|
public string Uri { get; set; } = string.Empty;
|
||||||
|
|
||||||
public string Path { 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user