more work on api
This commit is contained in:
@@ -61,7 +61,7 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
throw new NotSupportedException("The first parameter needs to be of type ApiMessage and must return an ApiResult object and the method must have the MessageHandlerAttribute.");
|
||||
|
||||
// Add handler
|
||||
var fullUrl = ApiUrl + attribute.Route;
|
||||
var fullUrl = attribute.Route;
|
||||
Log.InfoFormat("Added handler for {0}", fullUrl);
|
||||
handlers.Add(fullUrl, handler);
|
||||
}
|
||||
@@ -94,17 +94,23 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
// Parse url
|
||||
Log.Debug("Parse url");
|
||||
var path = context.Request.Url?.PathAndQuery.Replace(ApiUrl, string.Empty);
|
||||
if (string.IsNullOrWhiteSpace(path) || context.Request.ContentLength64 <= 0)
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
{
|
||||
Log.Info("Request has no content");
|
||||
Log.Info("Request has no path");
|
||||
close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Read input content
|
||||
Log.Debug("Read input content");
|
||||
using StreamReader input = new(context.Request.InputStream);
|
||||
var contentJson = input.ReadToEnd();
|
||||
string? contentJson;
|
||||
if (context.Request.ContentLength64 > 0)
|
||||
{
|
||||
using StreamReader input = new(context.Request.InputStream);
|
||||
contentJson = input.ReadToEnd();
|
||||
}
|
||||
else
|
||||
contentJson = null;
|
||||
|
||||
// Get auth key
|
||||
Log.Debug("Get auth key");
|
||||
@@ -138,7 +144,7 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
close();
|
||||
}
|
||||
|
||||
protected virtual PrivateApiResult? HandleMessage(string url, string json, string? authKey)
|
||||
protected virtual PrivateApiResult? HandleMessage(string url, string? json, string? authKey)
|
||||
{
|
||||
// Get handler
|
||||
Log.Debug("Find handler");
|
||||
@@ -161,8 +167,11 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
|
||||
// Deserialize
|
||||
Log.Debug("Deserialize message");
|
||||
if (serializer.Deserialize(json, targetType) is not ApiMessage message)
|
||||
return null;
|
||||
ApiMessage? message;
|
||||
if (json != null)
|
||||
message = serializer.Deserialize(json, targetType);
|
||||
else
|
||||
message = null;
|
||||
|
||||
// Invoke handler
|
||||
Log.Debug("Invoke handler");
|
||||
@@ -185,7 +194,7 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
return new(result, resultStr);
|
||||
}
|
||||
|
||||
protected virtual object?[]? BuildParameters(Delegate handler, Func<ApiMessage> getMessage, Func<ApiRequestInfo> getRequestInfo)
|
||||
protected virtual object?[]? BuildParameters(Delegate handler, Func<ApiMessage?> getMessage, Func<ApiRequestInfo> getRequestInfo)
|
||||
{
|
||||
var infos = handler.Method.GetParameters();
|
||||
var objs = new List<object?>();
|
||||
@@ -196,6 +205,8 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
objs.Add(getMessage());
|
||||
else if (info.ParameterType.IsAssignableTo(typeof(ApiRequestInfo)))
|
||||
objs.Add(getRequestInfo());
|
||||
else
|
||||
objs.Add(null);
|
||||
}
|
||||
|
||||
return [.. objs];
|
||||
|
||||
Reference in New Issue
Block a user