add check context events

This commit is contained in:
2024-11-08 07:36:46 +01:00
parent a7a3cfa683
commit 2e420ad902
3 changed files with 18 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
using System.Net;
namespace Pilz.Net.Api;
public class ApiContextEventArgs(HttpListenerContext context) : EventArgs
{
public HttpListenerContext Context { get; } = context;
}

View File

@@ -13,6 +13,8 @@ public class ApiServer(string apiUrl) : IApiServer
protected readonly HttpListener httpListener = new();
public event OnCheckAuthenticationEventHandler? OnCheckAuthentication;
public event OnCheckContextEventHandler? OnCheckContext;
public event OnCheckContextEventHandler? OnCheckContextCompleted;
protected record PrivateApiResult(ApiResult Original, string? ResultJson);
@@ -104,6 +106,7 @@ public class ApiServer(string apiUrl) : IApiServer
Receive();
// Check context
OnCheckContext?.Invoke(this, new(context));
try
{
CheckContext(context);
@@ -114,6 +117,10 @@ public class ApiServer(string apiUrl) : IApiServer
if (DebugMode)
throw;
}
finally
{
OnCheckContextCompleted?.Invoke(this, new(context));
}
// Listen for new request
if (!AllowMultipleRequests)

View File

@@ -5,8 +5,11 @@ namespace Pilz.Net.Api;
public interface IApiServer
{
public delegate void OnCheckAuthenticationEventHandler(object sender, ApiAuthCheckEventArgs e);
public delegate void OnCheckContextEventHandler(object sender, ApiContextEventArgs e);
event OnCheckAuthenticationEventHandler? OnCheckAuthentication;
event OnCheckContextEventHandler OnCheckContext;
event OnCheckContextEventHandler OnCheckContextCompleted;
string ApiUrl { get; }