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

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<Version>2.0.0</Version>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,24 @@
using System.Linq.Expressions;
using System.Reflection;
namespace Pilz.Extensions.Reflection;
public static class MethodInfoExtensions
{
/// <summary>
/// Source: https://stackoverflow.com/questions/940675/getting-a-delegate-from-methodinfo
/// </summary>
/// <param name="methodInfo"></param>
/// <param name="target"></param>
/// <returns></returns>
public static Delegate CreateDelegate(this MethodInfo methodInfo, object? target)
{
var parmTypes = methodInfo.GetParameters().Select(parm => parm.ParameterType);
var parmAndReturnTypes = parmTypes.Append(methodInfo.ReturnType).ToArray();
var delegateType = Expression.GetDelegateType(parmAndReturnTypes);
if (methodInfo.IsStatic)
return methodInfo.CreateDelegate(delegateType);
return methodInfo.CreateDelegate(delegateType, target);
}
}

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()
{
httpListener.Start();
Listen();
}
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 parameterInfo
|| !parameterInfo.ParameterType.IsAssignableTo(typeof(ApiMessage)))
|| 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;
}
var serializer = Serializer;
if (attribute.Serializer is not null)
// Parse url
var path = context.Request.Url?.PathAndQuery.Replace(ApiUrl, string.Empty);
if (string.IsNullOrWhiteSpace(path) || context.Request.ContentLength64 <= 0)
{
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);
}
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;
}
public virtual void RegisterHandler(string url, Delegate handler)
{
// ...
// 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>

View File

@@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pilz.Plugins.Advanced.UI",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pilz.Plugins.Advanced.UI.Telerik", "Pilz.Plugins.Advanced.UI.Telerik\Pilz.Plugins.Advanced.UI.Telerik.csproj", "{0A837BD6-A19C-4A05-A57D-CBB0CD64B244}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pilz.Extensions", "Pilz.Extensions\Pilz.Extensions.csproj", "{63DA7581-F35E-4EDD-BEAE-01E281B0BDC3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -211,6 +213,14 @@ Global
{0A837BD6-A19C-4A05-A57D-CBB0CD64B244}.Release|Any CPU.Build.0 = Release|Any CPU
{0A837BD6-A19C-4A05-A57D-CBB0CD64B244}.Release|x86.ActiveCfg = Release|Any CPU
{0A837BD6-A19C-4A05-A57D-CBB0CD64B244}.Release|x86.Build.0 = Release|Any CPU
{63DA7581-F35E-4EDD-BEAE-01E281B0BDC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{63DA7581-F35E-4EDD-BEAE-01E281B0BDC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63DA7581-F35E-4EDD-BEAE-01E281B0BDC3}.Debug|x86.ActiveCfg = Debug|Any CPU
{63DA7581-F35E-4EDD-BEAE-01E281B0BDC3}.Debug|x86.Build.0 = Debug|Any CPU
{63DA7581-F35E-4EDD-BEAE-01E281B0BDC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63DA7581-F35E-4EDD-BEAE-01E281B0BDC3}.Release|Any CPU.Build.0 = Release|Any CPU
{63DA7581-F35E-4EDD-BEAE-01E281B0BDC3}.Release|x86.ActiveCfg = Release|Any CPU
{63DA7581-F35E-4EDD-BEAE-01E281B0BDC3}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE