more work on api

This commit is contained in:
Pilzinsel64
2024-08-16 09:52:48 +02:00
parent 3502efc7f6
commit 8f3f572c28
4 changed files with 62 additions and 16 deletions

View File

@@ -14,7 +14,17 @@ public class ApiClient(string apiUrl) : IApiClient
public ILogger Log { get; set; } = NullLogger.Instance; public ILogger Log { get; set; } = NullLogger.Instance;
public virtual async Task<ApiResponse> SendMessage<TResponse>(string route, ApiMessage message, IMessageSerializer? serializer = null) public virtual Task<ApiResponse> SendRequest(string route)
{
return SendRequest(route, null);
}
public virtual Task<ApiResponse> SendRequest(string route, ApiMessage? message)
{
return SendRequest(route, message, null);
}
public virtual async Task<ApiResponse> SendRequest(string route, ApiMessage? message, IMessageSerializer? serializer)
{ {
serializer ??= Serializer; serializer ??= Serializer;
@@ -25,7 +35,17 @@ public class ApiClient(string apiUrl) : IApiClient
return new(res.StatusCode); return new(res.StatusCode);
} }
public virtual async Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage message, IMessageSerializer? serializer = null) where TResponse : ApiMessage public virtual Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route) where TResponse : ApiMessage
{
return SendRequest<TResponse>(route, null);
}
public virtual Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage? message) where TResponse : ApiMessage
{
return SendRequest<TResponse>(route, message, null);
}
public virtual async Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage? message, IMessageSerializer? serializer) where TResponse : ApiMessage
{ {
serializer ??= Serializer; serializer ??= Serializer;
@@ -40,11 +60,18 @@ public class ApiClient(string apiUrl) : IApiClient
return new(res.StatusCode, result); return new(res.StatusCode, result);
} }
protected virtual async Task<HttpResponseMessage> Send(string route, ApiMessage message, IMessageSerializer serializer) protected virtual async Task<HttpResponseMessage> Send(string route, ApiMessage? message, IMessageSerializer serializer)
{ {
var url = ApiUrl + route; var url = ApiUrl + route;
var content = new StringContent(serializer.Serialize(message)!, null, "application/json"); HttpContent content;
if (message is not null)
content = new StringContent(serializer.Serialize(message)!, null, "application/json");
else
content = new StringContent(string.Empty, null, "application/json");
content.Headers.Add("API-AUTH-KEY", EncodeAuthKey()); content.Headers.Add("API-AUTH-KEY", EncodeAuthKey());
return await httpClient.PostAsync(url, content); return await httpClient.PostAsync(url, content);
} }

View File

@@ -3,7 +3,7 @@
namespace Pilz.Net.Api; namespace Pilz.Net.Api;
public record class ApiRequestInfo( public record class ApiRequestInfo(
ApiMessage Message, ApiMessage? Message,
[property: MemberNotNullWhen(true, "AuthKey")] [property: MemberNotNullWhen(true, "AuthKey")]
bool IsAuthenticated, bool IsAuthenticated,
string? AuthKey); string? AuthKey);

View File

@@ -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."); 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 // Add handler
var fullUrl = ApiUrl + attribute.Route; var fullUrl = attribute.Route;
Log.InfoFormat("Added handler for {0}", fullUrl); Log.InfoFormat("Added handler for {0}", fullUrl);
handlers.Add(fullUrl, handler); handlers.Add(fullUrl, handler);
} }
@@ -94,17 +94,23 @@ 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?.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(); close();
return; return;
} }
// Read input content // Read input content
Log.Debug("Read input content"); Log.Debug("Read input content");
string? contentJson;
if (context.Request.ContentLength64 > 0)
{
using StreamReader input = new(context.Request.InputStream); using StreamReader input = new(context.Request.InputStream);
var contentJson = input.ReadToEnd(); contentJson = input.ReadToEnd();
}
else
contentJson = null;
// Get auth key // Get auth key
Log.Debug("Get auth key"); Log.Debug("Get auth key");
@@ -138,7 +144,7 @@ public class ApiServer(string apiUrl) : IApiServer
close(); close();
} }
protected virtual PrivateApiResult? HandleMessage(string url, string json, string? authKey) protected virtual PrivateApiResult? HandleMessage(string url, string? json, string? authKey)
{ {
// Get handler // Get handler
Log.Debug("Find handler"); Log.Debug("Find handler");
@@ -161,8 +167,11 @@ public class ApiServer(string apiUrl) : IApiServer
// Deserialize // Deserialize
Log.Debug("Deserialize message"); Log.Debug("Deserialize message");
if (serializer.Deserialize(json, targetType) is not ApiMessage message) ApiMessage? message;
return null; if (json != null)
message = serializer.Deserialize(json, targetType);
else
message = null;
// Invoke handler // Invoke handler
Log.Debug("Invoke handler"); Log.Debug("Invoke handler");
@@ -185,7 +194,7 @@ public class ApiServer(string apiUrl) : IApiServer
return new(result, resultStr); 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 infos = handler.Method.GetParameters();
var objs = new List<object?>(); var objs = new List<object?>();
@@ -196,6 +205,8 @@ public class ApiServer(string apiUrl) : IApiServer
objs.Add(getMessage()); objs.Add(getMessage());
else if (info.ParameterType.IsAssignableTo(typeof(ApiRequestInfo))) else if (info.ParameterType.IsAssignableTo(typeof(ApiRequestInfo)))
objs.Add(getRequestInfo()); objs.Add(getRequestInfo());
else
objs.Add(null);
} }
return [.. objs]; return [.. objs];

View File

@@ -12,7 +12,15 @@ public interface IApiClient
ILogger Log { get; set; } ILogger Log { get; set; }
Task<ApiResponse> SendMessage<TResponse>(string route, ApiMessage message, IMessageSerializer? serializer = null); Task<ApiResponse> SendRequest(string route, IMessageSerializer? serializer = null);
Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage message, IMessageSerializer? serializer = null) where TResponse : ApiMessage; Task<ApiResponse> SendRequest(string route, ApiMessage? message);
Task<ApiResponse> SendRequest(string route, ApiMessage? message, IMessageSerializer? serializer);
Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, IMessageSerializer? serializer = null) where TResponse : ApiMessage;
Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage? message) where TResponse : ApiMessage;
Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage? message, IMessageSerializer? serializer) where TResponse : ApiMessage;
} }