some work work on api

This commit is contained in:
2024-08-15 09:19:21 +02:00
parent 02a6c3bf6a
commit 4df972c7a4
8 changed files with 194 additions and 39 deletions

View File

@@ -1,49 +1,139 @@
using System.Reflection;
using Pilz.Extensions.Reflection;
using System;
using System.Net;
using System.Reflection;
using System.Reflection.PortableExecutable;
namespace Pilz.Networking.Api;
public class ApiServer : ApiClient, IApiServer
public class ApiServer(string apiUrl) : ApiClient, IApiServer
{
protected readonly Dictionary<string, object> handlers = [];
protected readonly Dictionary<string, Delegate> handlers = [];
protected readonly Dictionary<Type, IMessageSerializer> serializers = [];
protected readonly HttpListener httpListener = new();
public string ApiUrl { get; } = apiUrl;
public IMessageSerializer Serializer { get; set; } = new DefaultMessageSerializer();
public virtual void RegisterHandler<T>(string url, T instance) where T : class
public virtual void Start()
{
var methods = typeof(T).GetMethods(BindingFlags.Instance | BindingFlags.Public);
foreach (var method in methods)
{
if (method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute
|| method.GetParameters().FirstOrDefault() is not ParameterInfo parameterInfo
|| !parameterInfo.ParameterType.IsAssignableTo(typeof(ApiMessage)))
continue;
var serializer = Serializer;
if (attribute.Serializer is not null)
{
if (serializers.TryGetValue(attribute.Serializer, out var s) && s is not null)
serializer = s;
else if (Activator.CreateInstance(attribute.Serializer) is IMessageSerializer ss)
{
serializer = ss;
serializers.Add(attribute.Serializer, ss);
}
}
// ...
}
httpListener.Start();
Listen();
}
public virtual void RegisterHandler(string url, Delegate handler)
public virtual void RegisterHandler<T>(T instance) where T : class
{
// ...
// Get all public instance methods
var methods = typeof(T).GetMethods(BindingFlags.Instance | BindingFlags.Public);
// Register each method
foreach (var method in methods)
RegisterHandler(method.CreateDelegate(instance));
}
public virtual void RegisterHandler(Delegate handler)
{
var method = handler.Method;
// Sanity checks
if (method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute
|| method.GetParameters().FirstOrDefault() is not ParameterInfo parameter
|| !parameter.ParameterType.IsAssignableTo(typeof(ApiMessage))
|| !method.ReturnType.IsAssignableTo(typeof(ApiMessage)))
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.");
// Add handler
handlers.Add(ApiUrl + attribute.Url, handler);
}
protected virtual void Listen()
{
while (httpListener.IsListening)
{
var context = httpListener.GetContext();
if (context.Request.HttpMethod != HttpMethod.Post.Method
|| context.Request.ContentType is not string contentType
|| contentType.Contains("application/json")
|| context.Request.AcceptTypes is null
|| context.Request.AcceptTypes.Contains("application/json"))
{
close();
continue;
}
// Parse url
var path = context.Request.Url?.PathAndQuery.Replace(ApiUrl, string.Empty);
if (string.IsNullOrWhiteSpace(path) || context.Request.ContentLength64 <= 0)
{
close();
continue;
}
// Read input content
using StreamReader input = new(context.Request.InputStream);
var contentJson = input.ReadToEnd();
// Handle message
if (HandleMessage(path, contentJson) is not string resultJson)
{
close();
continue;
}
// Set response parameters
context.Response.StatusCode = (int)args.ResponseStatusCode;
context.Response.StatusDescription = args.ResponseStatusDescription;
// Write response content
context.Response.ContentType = ContentTypes.CONTENT_TYPE_JSON;
using StreamWriter output = new(context.Response.OutputStream);
output.Write(resultJson);
close();
void close() => context.Response.OutputStream.Close();
}
}
protected virtual string? HandleMessage(string url, string json)
{
// ...
// Get handler
if (!handlers.TryGetValue(url, out var handler)
|| handler.Method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute)
return null;
// Get required infos
var targetType = handler.Method.GetParameters().First().ParameterType;
var serializer = GetSerializer(attribute.Serializer);
// Deserialize
if (serializer.Deserialize(json, targetType) is not ApiMessage message)
return null;
// Invoke handler
if (handler.DynamicInvoke(message) is not ApiMessage result)
return null;
// Serializer
if (serializer.Serialize(result) is not string resultStr)
return null;
return resultStr;
}
protected virtual IMessageSerializer GetSerializer(Type? t)
{
if (t is not null)
{
if (serializers.TryGetValue(t, out var s) && s is not null)
return s;
else if (Activator.CreateInstance(t) is IMessageSerializer ss)
{
serializers.Add(t, ss);
return ss;
}
}
return Serializer;
}
}

View File

@@ -1,14 +1,28 @@
namespace Pilz.Networking.Api;
using Newtonsoft.Json;
namespace Pilz.Networking.Api;
public class DefaultMessageSerializer : IMessageSerializer
{
public ApiMessage? Deserialize(string json)
private static JsonSerializerSettings? defaultSerializerSettings;
public JsonSerializerSettings DefaultSerializerSettings => defaultSerializerSettings ??= CreateDefaultSerializerSettings();
protected virtual JsonSerializerSettings CreateDefaultSerializerSettings()
{
// ...
return new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
};
}
public string? Serialize(ApiMessage message)
public virtual string? Serialize(ApiMessage message)
{
// ...
return JsonConvert.SerializeObject(message, DefaultSerializerSettings);
}
public virtual ApiMessage? Deserialize(string json, Type target)
{
return JsonConvert.DeserializeObject(json, target, DefaultSerializerSettings) as ApiMessage;
}
}

View File

@@ -2,6 +2,6 @@
public interface IApiServer : IApiClient
{
void RegisterHandler<T>(string url, T instance) where T : class;
void RegisterHandler(string url, Delegate handler);
void RegisterHandler<T>(T instance) where T : class;
void RegisterHandler(Delegate handler);
}

View File

@@ -3,5 +3,5 @@
public interface IMessageSerializer
{
string? Serialize(ApiMessage message);
ApiMessage? Deserialize(string json);
ApiMessage? Deserialize(string json, Type target);
}

View File

@@ -15,4 +15,8 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Pilz.Extensions\Pilz.Extensions.csproj" />
</ItemGroup>
</Project>