more work on api
This commit is contained in:
19
Pilz.Networking/Api/ApiResult.cs
Normal file
19
Pilz.Networking/Api/ApiResult.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace Pilz.Networking.Api;
|
||||||
|
|
||||||
|
public record class ApiResult(
|
||||||
|
HttpStatusCode StatusCode,
|
||||||
|
ApiMessage? Message = null,
|
||||||
|
string? StatusDescription = null)
|
||||||
|
{
|
||||||
|
public static ApiResult Ok()
|
||||||
|
{
|
||||||
|
return new(HttpStatusCode.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApiResult Ok(ApiMessage message)
|
||||||
|
{
|
||||||
|
return new(HttpStatusCode.OK, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
using Pilz.Extensions.Reflection;
|
using Pilz.Extensions.Reflection;
|
||||||
using System;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Reflection.PortableExecutable;
|
|
||||||
|
|
||||||
namespace Pilz.Networking.Api;
|
namespace Pilz.Networking.Api;
|
||||||
|
|
||||||
@@ -12,6 +10,8 @@ public class ApiServer(string apiUrl) : ApiClient, IApiServer
|
|||||||
protected readonly Dictionary<Type, IMessageSerializer> serializers = [];
|
protected readonly Dictionary<Type, IMessageSerializer> serializers = [];
|
||||||
protected readonly HttpListener httpListener = new();
|
protected readonly HttpListener httpListener = new();
|
||||||
|
|
||||||
|
protected record PrivateApiResult(ApiResult Original, string? ResultJson);
|
||||||
|
|
||||||
public string ApiUrl { get; } = apiUrl;
|
public string ApiUrl { get; } = apiUrl;
|
||||||
|
|
||||||
public IMessageSerializer Serializer { get; set; } = new DefaultMessageSerializer();
|
public IMessageSerializer Serializer { get; set; } = new DefaultMessageSerializer();
|
||||||
@@ -40,8 +40,8 @@ public class ApiServer(string apiUrl) : ApiClient, IApiServer
|
|||||||
if (method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute
|
if (method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute
|
||||||
|| method.GetParameters().FirstOrDefault() is not ParameterInfo parameter
|
|| method.GetParameters().FirstOrDefault() is not ParameterInfo parameter
|
||||||
|| !parameter.ParameterType.IsAssignableTo(typeof(ApiMessage))
|
|| !parameter.ParameterType.IsAssignableTo(typeof(ApiMessage))
|
||||||
|| !method.ReturnType.IsAssignableTo(typeof(ApiMessage)))
|
|| !method.ReturnType.IsAssignableTo(typeof(ApiResult)))
|
||||||
throw new NotSupportedException("The first parameter needs to be of type ApiMessage and must also return an ApiMessage 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
|
||||||
handlers.Add(ApiUrl + attribute.Url, handler);
|
handlers.Add(ApiUrl + attribute.Url, handler);
|
||||||
@@ -96,7 +96,7 @@ public class ApiServer(string apiUrl) : ApiClient, IApiServer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual string? HandleMessage(string url, string json)
|
protected virtual PrivateApiResult? HandleMessage(string url, string json)
|
||||||
{
|
{
|
||||||
// Get handler
|
// Get handler
|
||||||
if (!handlers.TryGetValue(url, out var handler)
|
if (!handlers.TryGetValue(url, out var handler)
|
||||||
@@ -112,14 +112,19 @@ public class ApiServer(string apiUrl) : ApiClient, IApiServer
|
|||||||
return null;
|
return null;
|
||||||
|
|
||||||
// Invoke handler
|
// Invoke handler
|
||||||
if (handler.DynamicInvoke(message) is not ApiMessage result)
|
if (handler.DynamicInvoke(message) is not ApiResult result)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
// Return result without message
|
||||||
|
if (result.Message is null)
|
||||||
|
return new(result, null);
|
||||||
|
|
||||||
// Serializer
|
// Serializer
|
||||||
if (serializer.Serialize(result) is not string resultStr)
|
if (serializer.Serialize(result.Message) is not string resultStr)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return resultStr;
|
// Return result with message
|
||||||
|
return new(result, resultStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual IMessageSerializer GetSerializer(Type? t)
|
protected virtual IMessageSerializer GetSerializer(Type? t)
|
||||||
|
|||||||
Reference in New Issue
Block a user