refactor(BaseHttpClientService): Der Wert von query params wurde zum nullbaren Objekt, um Flag-Parameter hinzuzufügen.

- Aktualisierte Schnittstelle und Logik unter Berücksichtigung dieser Situation
This commit is contained in:
Developer 02 2024-11-22 14:35:22 +01:00
parent dd7f1c1ea0
commit 0c2334cefb
3 changed files with 20 additions and 7 deletions

View File

@ -12,7 +12,7 @@ namespace DigitalData.Core.Abstractions.Client
string? scheme = null, string? scheme = null,
int? port = null, int? port = null,
string path = "", string path = "",
Dictionary<string, string>? queryParams = null, Dictionary<string, object?>? queryParams = null,
HttpMethod? method = null, HttpMethod? method = null,
HttpContent? body = null, HttpContent? body = null,
Dictionary<string, string>? form = null, Dictionary<string, string>? form = null,

View File

@ -30,7 +30,7 @@ namespace DigitalData.Core.Client
string? scheme = null, string? scheme = null,
int? port = null, int? port = null,
string path = "", string path = "",
Dictionary<string, string>? queryParams = null, Dictionary<string, object?>? queryParams = null,
HttpMethod? method = null, HttpMethod? method = null,
HttpContent? body = null, HttpContent? body = null,
Dictionary<string, string>? form = null, Dictionary<string, string>? form = null,
@ -54,14 +54,25 @@ namespace DigitalData.Core.Client
if (queryParams?.Any() ?? false) if (queryParams?.Any() ?? false)
{ {
var query = HttpUtility.ParseQueryString(uriBuilder.Query); var query = HttpUtility.ParseQueryString(uriBuilder.Query);
foreach (var param in queryParams)
query[param.Key] = param.Value;
uriBuilder.Query = query.ToString(); var flagParams = queryParams.Where(param => param.Value is null).Select(param => HttpUtility.UrlEncode(param.Key));
var valueParams = queryParams.Where(param => param.Value is not null);
foreach (var param in valueParams)
query[param.Key] = param.Value switch
{
bool b => b.ToString().ToLower(),
_ => HttpUtility.UrlEncode(param.Value.ToString())
};
var flagQuery = string.Join(QUERY_SEPARATOR, flagParams);
uriBuilder.Query = string.Join(QUERY_SEPARATOR, query.ToString(), flagQuery);
} }
var requestUri = uriBuilder.Uri; var requestUri = uriBuilder.Uri;
Console.WriteLine(requestUri);
var requestMessage = new HttpRequestMessage(method, requestUri); var requestMessage = new HttpRequestMessage(method, requestUri);
// Add headers if provided // Add headers if provided
@ -99,5 +110,7 @@ namespace DigitalData.Core.Client
internal static readonly char[] URI_TRIM_CHARS = { '\\', '/', ' ' }; internal static readonly char[] URI_TRIM_CHARS = { '\\', '/', ' ' };
internal static string UriCombine(params string[] paths) => System.IO.Path.Combine(paths).Replace("\\", "/"); internal static string UriCombine(params string[] paths) => System.IO.Path.Combine(paths).Replace("\\", "/");
internal static readonly char QUERY_SEPARATOR = '&';
} }
} }

View File

@ -35,7 +35,7 @@ namespace DigitalData.Core.Tests.Client
[Test] [Test]
public async Task FetchJsonAsync_ShouldReturnJsonResponse_WithQueryParams() public async Task FetchJsonAsync_ShouldReturnJsonResponse_WithQueryParams()
{ {
var queryParams = new Dictionary<string, string> var queryParams = new Dictionary<string, object?>
{ {
{ "id", "1" } { "id", "1" }
}; };