support queries

This commit is contained in:
Pilzinsel64
2024-12-06 11:52:57 +01:00
parent 98e3217df3
commit 6c60d271ad

View File

@@ -178,7 +178,8 @@ public class ApiServer(string apiUrl) : IApiServer
// Parse url // Parse url
Log.Debug("Parse url"); Log.Debug("Parse url");
var path = context.Request.Url?.PathAndQuery.Replace(ApiUrl, string.Empty); var path = context.Request.Url?.AbsolutePath;
var query = context.Request.Url?.Query;
if (string.IsNullOrWhiteSpace(path)) if (string.IsNullOrWhiteSpace(path))
{ {
Log.Info("Request has no path"); Log.Info("Request has no path");
@@ -188,7 +189,7 @@ public class ApiServer(string apiUrl) : IApiServer
// Find handler // Find handler
Log.Debug("Find handler"); Log.Debug("Find handler");
if (!TryGetHandler(path, context.Request.HttpMethod, out var handler)) if (!TryGetHandler(path, query, context.Request.HttpMethod, out var handler))
{ {
Log.Info("Request handler couldn't be found"); Log.Info("Request handler couldn't be found");
close(); close();
@@ -230,7 +231,7 @@ public class ApiServer(string apiUrl) : IApiServer
// Handle message // Handle message
Log.Debug("Handle mssage"); Log.Debug("Handle mssage");
if (HandleMessage(path, context.Request.HttpMethod, handler, contentJson, authKey) is not PrivateApiResult result) if (HandleMessage(path, query, context.Request.HttpMethod, handler, contentJson, authKey) is not PrivateApiResult result)
{ {
Log.Warn("Request couldn't be handled"); Log.Warn("Request couldn't be handled");
close(); close();
@@ -255,7 +256,7 @@ public class ApiServer(string apiUrl) : IApiServer
close(); close();
} }
protected virtual PrivateApiResult? HandleMessage(string url, string method, PrivateMessageHandler handler, string? json, string? authKey) protected virtual PrivateApiResult? HandleMessage(string url, string? query, string method, PrivateMessageHandler handler, string? json, string? authKey)
{ {
// Check authentication // Check authentication
Log.Debug("Check authentication"); Log.Debug("Check authentication");
@@ -282,7 +283,7 @@ public class ApiServer(string apiUrl) : IApiServer
// Invoke handler // Invoke handler
Log.Debug("Invoke handler"); Log.Debug("Invoke handler");
var parameters = BuildParameters(url, handler, () => message, () => new(message, isAuthenticated, authKeyDecoded, url, method)); var parameters = BuildParameters(url, query, handler, () => message, () => new(message, isAuthenticated, authKeyDecoded, url, method));
if (handler.Handler.DynamicInvoke(parameters) is not ApiResult result) if (handler.Handler.DynamicInvoke(parameters) is not ApiResult result)
return new(ApiResult.InternalServerError(), null); return new(ApiResult.InternalServerError(), null);
@@ -301,7 +302,7 @@ public class ApiServer(string apiUrl) : IApiServer
return new(result, resultStr); return new(result, resultStr);
} }
protected virtual bool TryGetHandler(string url, string method, [NotNullWhen(true)] out PrivateMessageHandler? handler) protected virtual bool TryGetHandler(string url, string? query, string method, [NotNullWhen(true)] out PrivateMessageHandler? handler)
{ {
handler = handlers.FirstOrDefault(handler => handler = handlers.FirstOrDefault(handler =>
{ {
@@ -317,10 +318,11 @@ public class ApiServer(string apiUrl) : IApiServer
return handler != null; return handler != null;
} }
protected virtual object?[]? BuildParameters(string url, PrivateMessageHandler handler, Func<ApiMessage?> getMessage, Func<ApiRequestInfo> getRequestInfo) protected virtual object?[]? BuildParameters(string url, string? query, PrivateMessageHandler handler, Func<ApiMessage?> getMessage, Func<ApiRequestInfo> getRequestInfo)
{ {
var infos = handler.Handler.Method.GetParameters(); var infos = handler.Handler.Method.GetParameters();
var objs = new List<object?>(); var objs = new List<object?>();
var queryparams = query == null ? [] : HttpUtility.ParseQueryString(query);
foreach (var info in infos) foreach (var info in infos)
{ {
@@ -331,6 +333,8 @@ public class ApiServer(string apiUrl) : IApiServer
else if (handler.Parameters.FirstOrDefault(p => p.Name.Equals(info.Name, StringComparison.InvariantCultureIgnoreCase)) is PrivateParameterInfo parameterInfo else if (handler.Parameters.FirstOrDefault(p => p.Name.Equals(info.Name, StringComparison.InvariantCultureIgnoreCase)) is PrivateParameterInfo parameterInfo
&& url.Split('/').ElementAtOrDefault(parameterInfo.Index) is string parameterValue) && url.Split('/').ElementAtOrDefault(parameterInfo.Index) is string parameterValue)
objs.Add(Convert.ChangeType(HttpUtility.UrlDecode(parameterValue), info.ParameterType)); // or Uri.UnescapeDataString(); maybe run this line twice? objs.Add(Convert.ChangeType(HttpUtility.UrlDecode(parameterValue), info.ParameterType)); // or Uri.UnescapeDataString(); maybe run this line twice?
else if (queryparams.AllKeys.FirstOrDefault(n => n != null && n.Equals(info.Name, StringComparison.InvariantCultureIgnoreCase)) is string querykey)
objs.Add(Convert.ChangeType(HttpUtility.HtmlDecode(queryparams.Get(querykey)), info.ParameterType));
else else
objs.Add(null); objs.Add(null);
} }