more work on api
This commit is contained in:
@@ -14,7 +14,17 @@ public class ApiClient(string apiUrl) : IApiClient
|
||||
|
||||
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;
|
||||
|
||||
@@ -25,7 +35,17 @@ public class ApiClient(string apiUrl) : IApiClient
|
||||
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;
|
||||
|
||||
@@ -40,11 +60,18 @@ public class ApiClient(string apiUrl) : IApiClient
|
||||
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 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());
|
||||
|
||||
return await httpClient.PostAsync(url, content);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Pilz.Net.Api;
|
||||
|
||||
public record class ApiRequestInfo(
|
||||
ApiMessage Message,
|
||||
ApiMessage? Message,
|
||||
[property: MemberNotNullWhen(true, "AuthKey")]
|
||||
bool IsAuthenticated,
|
||||
string? AuthKey);
|
||||
|
||||
@@ -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");
|
||||
string? contentJson;
|
||||
if (context.Request.ContentLength64 > 0)
|
||||
{
|
||||
using StreamReader input = new(context.Request.InputStream);
|
||||
var contentJson = input.ReadToEnd();
|
||||
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];
|
||||
|
||||
@@ -12,7 +12,15 @@ public interface IApiClient
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user