More changes to the endpoint auto setup

This commit is contained in:
2021-10-25 16:10:41 -04:00
parent 58881e5ad6
commit ab8937f7cd
4 changed files with 75 additions and 33 deletions

View File

@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TOOHUCardAPI.Data;
using MethodMap = System.Collections.Generic.Dictionary<string, System.Reflection.MethodInfo>;
namespace TOOHUCardAPI.Controllers
{
public class MethodBasedController : ControllerBase
{
public delegate Task<object> EndpointHandler(string requestBody);
private static Dictionary<Type, MethodMap> MethodMapByType;
protected Task<object> InvokeEndpointHandlerForMethod<T>(object _this, string method, string body)
{
MethodMap registeredEndpointHandlers = GetMethodMapForType<T>();
if (registeredEndpointHandlers.ContainsKey(method))
{
return ((EndpointHandler) registeredEndpointHandlers[method]
.CreateDelegate(typeof(EndpointHandler), _this)).Invoke(body);
}
throw new MissingEndpointHandlerException(method);
}
private MethodMap GetMethodMapForType<T>()
{
if (!MethodMapByType.ContainsKey(typeof(T)))
{
RegisterEndpointHandlers<T>();
}
return MethodMapByType[typeof(T)];
}
private static MethodMap RegisterEndpointHandlers<T>()
{
MethodInfo[] methods = typeof(T).GetMethods();
return methods
.Aggregate(new MethodMap(), (handlers, m) =>
{
Attribute attr = m.GetCustomAttribute(typeof(EndpointHandlerAttribute), false);
if (attr != null)
{
EndpointHandlerAttribute e = (EndpointHandlerAttribute) attr;
handlers.Add(e.Method, m);
}
return handlers;
});
}
}
}

View File

@@ -6,50 +6,31 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using TOOHUCardAPI.Data; using TOOHUCardAPI.Data;
using TOOHUCardAPI.Models;
namespace TOOHUCardAPI.Controllers namespace TOOHUCardAPI.Controllers
{ {
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
public class PlayerDataController : ControllerBase public class PlayerDataController : MethodBasedController
{ {
private delegate Task<object> EndpointHandler(string requestBody);
// Mapping of method => handler
private Dictionary<string, EndpointHandler> registeredEndpointHandlers;
private void RegisterEndpointHandlers()
{
MethodInfo[] methods = typeof(PlayerDataController).GetMethods();
registeredEndpointHandlers = methods
.Aggregate(new Dictionary<string, EndpointHandler>(), (handlers, m) =>
{
Attribute attr = m.GetCustomAttribute(typeof(EndpointHandlerAttribute), false);
if (attr != null)
{
EndpointHandlerAttribute e = (EndpointHandlerAttribute) attr;
handlers.Add(e.Method, (EndpointHandler)m.CreateDelegate(typeof(EndpointHandler), this));
}
return handlers;
});
}
public PlayerDataController()
{
RegisterEndpointHandlers();
}
/** /**
* The game uses a single endpoint for player data. * The game uses a single endpoint for player data.
* The object they send has a method field that's used to decide what's being done * The object they send has a method field that's used to decide what's being done
* So let's use a single entry point and redirect based on that * So let's use a single entry point and redirect based on that
*/ */
[HttpPost] [HttpPost]
public async Task<object> EntryPoint() public async Task<object> EntryPoint(IMethodBasedRequest methodRequest)
{ {
return null; return null;
} }
} }
public class MissingEndpointHandlerException : Exception
{
public MissingEndpointHandlerException(string? method) : base($"Handler for [{method}] is either missing or incorrectly setup.")
{
}
}
} }

View File

@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;
using Newtonsoft.Json;
namespace TOOHUCardAPI.Models
{
public interface IMethodBasedRequest
{
[JsonPropertyName("method")]
public string Method { get; }
}
}

View File

@@ -10,8 +10,4 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Controllers" />
</ItemGroup>
</Project> </Project>