support queries
This commit is contained in:
@@ -178,7 +178,8 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
|
||||
// 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))
|
||||
{
|
||||
Log.Info("Request has no path");
|
||||
@@ -188,7 +189,7 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
|
||||
// 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");
|
||||
close();
|
||||
@@ -230,7 +231,7 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
|
||||
// Handle message
|
||||
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");
|
||||
close();
|
||||
@@ -255,7 +256,7 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
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
|
||||
Log.Debug("Check authentication");
|
||||
@@ -282,7 +283,7 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
|
||||
// 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)
|
||||
return new(ApiResult.InternalServerError(), null);
|
||||
|
||||
@@ -301,7 +302,7 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
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 =>
|
||||
{
|
||||
@@ -317,10 +318,11 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
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 objs = new List<object?>();
|
||||
var queryparams = query == null ? [] : HttpUtility.ParseQueryString(query);
|
||||
|
||||
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
|
||||
&& 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?
|
||||
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
|
||||
objs.Add(null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user