more work on api & rename to Pilz.Net

This commit is contained in:
Pilzinsel64
2024-08-16 06:59:39 +02:00
parent f57aef5f4f
commit 2efb4f141c
91 changed files with 299 additions and 241 deletions

View File

@@ -0,0 +1,20 @@
namespace Pilz.Net.Api;
public record class ApiAuthCheckEventArgs(string AuthKey)
{
private bool hasDenyed;
public bool Valid { get; set; }
public void Deny()
{
Valid = false;
hasDenyed = true;
}
public void Permit()
{
if (!hasDenyed)
Valid = true;
}
}

42
Pilz.Net/Api/ApiClient.cs Normal file
View File

@@ -0,0 +1,42 @@
namespace Pilz.Net.Api;
public class ApiClient(string apiUrl) : IApiClient
{
protected readonly HttpClient httpClient = new();
public virtual string ApiUrl { get; } = apiUrl;
public string? AuthKey { get; set; }
public virtual IMessageSerializer Serializer { get; set; } = new DefaultMessageSerializer();
public virtual async Task<ApiResponse> SendMessage<TResponse>(string url, ApiMessage message, IMessageSerializer? serializer)
{
serializer ??= Serializer;
var res = await Send(url, message, serializer);
return new(res.StatusCode);
}
public virtual async Task<ApiResponse<TResponse>> SendRequest<TResponse>(string url, ApiMessage message, IMessageSerializer? serializer) where TResponse : ApiMessage
{
serializer ??= Serializer;
var res = await Send(url, message, serializer);
TResponse? result = null;
if (res.IsSuccessStatusCode)
result = serializer.Deserialize(await res.Content.ReadAsStringAsync(), typeof(TResponse)) as TResponse;
return new(res.StatusCode, result);
}
protected virtual async Task<HttpResponseMessage> Send(string url, ApiMessage message, IMessageSerializer serializer)
{
var fullRequestUrl = ApiUrl + url;
var content = new StringContent(serializer.Serialize(message)!, null, "application/json");
content.Headers.Add("API-AUTH-KEY", AuthKey);
return await httpClient.PostAsync(fullRequestUrl, content);
}
}

View File

@@ -0,0 +1,5 @@
namespace Pilz.Net.Api;
public abstract class ApiMessage
{
}

View File

@@ -0,0 +1,13 @@
using System.Net;
namespace Pilz.Net.Api;
public record class ApiResponse(
HttpStatusCode StatusCode)
{
public void EnsureOk()
{
if (StatusCode != HttpStatusCode.OK)
throw new Exception("Api return is not ok!");
}
}

View File

@@ -0,0 +1,15 @@
using System.Net;
namespace Pilz.Net.Api;
public record class ApiResponse<T>(
HttpStatusCode StatusCode,
T? Message)
where T : ApiMessage
{
public void EnsureOk()
{
if (StatusCode != HttpStatusCode.OK)
throw new Exception("Api return is not ok!");
}
}

18
Pilz.Net/Api/ApiResult.cs Normal file
View File

@@ -0,0 +1,18 @@
using System.Net;
namespace Pilz.Net.Api;
public record class ApiResult(
HttpStatusCode StatusCode,
ApiMessage? Message = null)
{
public static ApiResult Ok()
{
return new(HttpStatusCode.OK);
}
public static ApiResult Ok(ApiMessage message)
{
return new(HttpStatusCode.OK, message);
}
}

176
Pilz.Net/Api/ApiServer.cs Normal file
View File

@@ -0,0 +1,176 @@
using Pilz.Extensions.Reflection;
using System.Net;
using System.Reflection;
using static Pilz.Net.Api.IApiServer;
namespace Pilz.Net.Api;
public class ApiServer(string apiUrl) : IApiServer
{
protected readonly Dictionary<string, Delegate> handlers = [];
protected readonly Dictionary<Type, IMessageSerializer> serializers = [];
protected readonly HttpListener httpListener = new();
public event OnCheckAuthenticationEventHandler? OnCheckAuthentication;
protected record PrivateApiResult(ApiResult Original, string? ResultJson);
public string ApiUrl { get; } = apiUrl;
public virtual bool EnableAuth { get; set; }
public IMessageSerializer Serializer { get; set; } = new DefaultMessageSerializer();
public virtual void Start()
{
httpListener.Start();
Listen();
}
public virtual void Stop()
{
httpListener.Stop();
}
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(ApiResult)))
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
handlers.Add(ApiUrl + attribute.Route, handler);
}
protected virtual void Listen()
{
while (httpListener.IsListening)
CheckContext();
}
protected virtual void CheckContext()
{
var context = httpListener.GetContext();
void close() => context.Response.OutputStream.Close();
if (context.Request.HttpMethod != HttpMethod.Post.Method
|| context.Request.ContentType is not string contentType
|| !contentType.Contains("application/json"))
{
close();
return;
}
// Parse url
var path = context.Request.Url?.PathAndQuery.Replace(ApiUrl, string.Empty);
if (string.IsNullOrWhiteSpace(path) || context.Request.ContentLength64 <= 0)
{
close();
return;
}
// Read input content
using StreamReader input = new(context.Request.InputStream);
var contentJson = input.ReadToEnd();
// Get auth key
if (context.Request.Headers.Get("API-AUTH-KEY") is not string authKey)
authKey = null!;
// Handle message
if (HandleMessage(path, contentJson, authKey) is not PrivateApiResult result)
{
close();
return;
}
// Set response parameters
context.Response.StatusCode = (int)result.Original.StatusCode;
// Write response content
if (result.ResultJson is not null)
{
context.Response.ContentType = "application/json";
using StreamWriter output = new(context.Response.OutputStream);
output.Write(result);
}
close();
}
protected virtual PrivateApiResult? HandleMessage(string url, string json, string? authKey)
{
// Get handler
if (!handlers.TryGetValue(url, out var handler)
|| handler.Method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute)
return null;
// Check authentication
if (attribute.RequiesAuth && (string.IsNullOrWhiteSpace(authKey) || !CheckAuthentication(authKey)))
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 ApiResult result)
return null;
// Return result without message
if (result.Message is null)
return new(result, null);
// Serializer
if (serializer.Serialize(result.Message) is not string resultStr)
return null;
// Return result with message
return new(result, 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;
}
protected virtual bool CheckAuthentication(string authKey)
{
if (OnCheckAuthentication != null)
{
var args = new ApiAuthCheckEventArgs(authKey);
OnCheckAuthentication?.Invoke(this, args);
return args.Valid;
}
return false;
}
}

View File

@@ -0,0 +1,28 @@
using Newtonsoft.Json;
namespace Pilz.Net.Api;
public class DefaultMessageSerializer : IMessageSerializer
{
private static JsonSerializerSettings? defaultSerializerSettings;
public JsonSerializerSettings DefaultSerializerSettings => defaultSerializerSettings ??= CreateDefaultSerializerSettings();
protected virtual JsonSerializerSettings CreateDefaultSerializerSettings()
{
return new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
};
}
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

@@ -0,0 +1,14 @@
namespace Pilz.Net.Api;
public interface IApiClient
{
string ApiUrl { get; }
string? AuthKey { get; set; }
IMessageSerializer Serializer { get; }
Task<ApiResponse> SendMessage<TResponse>(string url, ApiMessage message, IMessageSerializer? serializer = null);
Task<ApiResponse<TResponse>> SendRequest<TResponse>(string url, ApiMessage message, IMessageSerializer? serializer = null) where TResponse : ApiMessage;
}

View File

@@ -0,0 +1,22 @@
namespace Pilz.Net.Api;
public interface IApiServer
{
public delegate void OnCheckAuthenticationEventHandler(object sender, ApiAuthCheckEventArgs e);
event OnCheckAuthenticationEventHandler? OnCheckAuthentication;
string ApiUrl { get; }
bool EnableAuth { get; set; }
IMessageSerializer Serializer { get; }
void Start();
void Stop();
void RegisterHandler<T>(T instance) where T : class;
void RegisterHandler(Delegate handler);
}

View File

@@ -0,0 +1,7 @@
namespace Pilz.Net.Api;
public interface IMessageSerializer
{
string? Serialize(ApiMessage message);
ApiMessage? Deserialize(string json, Type target);
}

View File

@@ -0,0 +1,9 @@
namespace Pilz.Net.Api;
[AttributeUsage(AttributeTargets.Method)]
public class MessageHandlerAttribute(string route) : Attribute
{
public string Route { get; set; } = route;
public Type? Serializer { get; set; }
public bool RequiesAuth { get; set; }
}