mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-13 05:48:35 +00:00
Plugin endpoints - Added DataModelJsonPluginEndPoint
This commit is contained in:
parent
18592502fd
commit
c47e873510
@ -68,6 +68,7 @@
|
|||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Ccontrollers/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Ccontrollers/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Cendpoints/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Cendpoints/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Cendpoints_005Ceventargs/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Cinterfaces/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Cinterfaces/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Cjson/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Cjson/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=stores/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=stores/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
|||||||
@ -0,0 +1,71 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Artemis.Core.DataModelExpansions;
|
||||||
|
using Artemis.Core.Modules;
|
||||||
|
using EmbedIO;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace Artemis.Core.Services
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a plugin web endpoint receiving an object of type <typeparamref name="T" /> and returning any
|
||||||
|
/// <see cref="object" /> or <see langword="null" />.
|
||||||
|
/// <para>Note: Both will be deserialized and serialized respectively using JSON.</para>
|
||||||
|
/// </summary>
|
||||||
|
public class DataModelJsonPluginEndPoint<T> : PluginEndPoint where T : DataModel
|
||||||
|
{
|
||||||
|
private readonly Module<T>? _module;
|
||||||
|
private readonly DataModelExpansion<T>? _dataModelExpansion;
|
||||||
|
|
||||||
|
internal DataModelJsonPluginEndPoint(Module<T> module, string name, PluginsModule pluginsModule) : base(module, name, pluginsModule)
|
||||||
|
{
|
||||||
|
_module = module ?? throw new ArgumentNullException(nameof(module));
|
||||||
|
|
||||||
|
ThrowOnFail = true;
|
||||||
|
Accepts = MimeType.Json;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal DataModelJsonPluginEndPoint(DataModelExpansion<T> dataModelExpansion, string name, PluginsModule pluginsModule) : base(dataModelExpansion, name, pluginsModule)
|
||||||
|
{
|
||||||
|
_dataModelExpansion = dataModelExpansion ?? throw new ArgumentNullException(nameof(dataModelExpansion));
|
||||||
|
|
||||||
|
ThrowOnFail = true;
|
||||||
|
Accepts = MimeType.Json;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether or not the end point should throw an exception if deserializing the received JSON fails.
|
||||||
|
/// If set to <see langword="false" /> malformed JSON is silently ignored; if set to <see langword="true" /> malformed
|
||||||
|
/// JSON throws a <see cref="JsonException" />.
|
||||||
|
/// </summary>
|
||||||
|
public bool ThrowOnFail { get; set; }
|
||||||
|
|
||||||
|
#region Overrides of PluginEndPoint
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override async Task ProcessRequest(IHttpContext context)
|
||||||
|
{
|
||||||
|
if (context.Request.HttpVerb != HttpVerbs.Post && context.Request.HttpVerb != HttpVerbs.Put)
|
||||||
|
throw HttpException.MethodNotAllowed("This end point only accepts POST and PUT calls");
|
||||||
|
|
||||||
|
context.Response.ContentType = MimeType.Json;
|
||||||
|
|
||||||
|
using TextReader reader = context.OpenRequestText();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_module != null)
|
||||||
|
JsonConvert.PopulateObject(await reader.ReadToEndAsync(), _module.DataModel);
|
||||||
|
else
|
||||||
|
JsonConvert.PopulateObject(await reader.ReadToEndAsync(), _dataModelExpansion!.DataModel);
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
if (ThrowOnFail)
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using EmbedIO;
|
||||||
|
|
||||||
|
namespace Artemis.Core.Services
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Provides data about endpoint request related events
|
||||||
|
/// </summary>
|
||||||
|
public class EndpointRequestEventArgs : EventArgs
|
||||||
|
{
|
||||||
|
internal EndpointRequestEventArgs(IHttpContext context)
|
||||||
|
{
|
||||||
|
Context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the HTTP context of the request
|
||||||
|
/// </summary>
|
||||||
|
public IHttpContext Context { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -57,6 +57,16 @@ namespace Artemis.Core.Services
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public event EventHandler<EndpointExceptionEventArgs>? RequestException;
|
public event EventHandler<EndpointExceptionEventArgs>? RequestException;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Occurs whenever a request is about to be processed
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<EndpointRequestEventArgs>? ProcessingRequest;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Occurs whenever a request was processed
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<EndpointRequestEventArgs>? ProcessedRequest;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called whenever the end point has to process a request
|
/// Called whenever the end point has to process a request
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -72,11 +82,29 @@ namespace Artemis.Core.Services
|
|||||||
RequestException?.Invoke(this, new EndpointExceptionEventArgs(e));
|
RequestException?.Invoke(this, new EndpointExceptionEventArgs(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Invokes the <see cref="ProcessingRequest" /> event
|
||||||
|
/// </summary>
|
||||||
|
protected virtual void OnProcessingRequest(IHttpContext context)
|
||||||
|
{
|
||||||
|
ProcessingRequest?.Invoke(this, new EndpointRequestEventArgs(context));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Invokes the <see cref="ProcessedRequest" /> event
|
||||||
|
/// </summary>
|
||||||
|
protected virtual void OnProcessedRequest(IHttpContext context)
|
||||||
|
{
|
||||||
|
ProcessedRequest?.Invoke(this, new EndpointRequestEventArgs(context));
|
||||||
|
}
|
||||||
|
|
||||||
internal async Task InternalProcessRequest(IHttpContext context)
|
internal async Task InternalProcessRequest(IHttpContext context)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
OnProcessingRequest(context);
|
||||||
await ProcessRequest(context);
|
await ProcessRequest(context);
|
||||||
|
OnProcessedRequest(context);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Artemis.Core.DataModelExpansions;
|
||||||
|
using Artemis.Core.Modules;
|
||||||
using EmbedIO;
|
using EmbedIO;
|
||||||
using EmbedIO.WebApi;
|
using EmbedIO.WebApi;
|
||||||
|
|
||||||
@ -43,6 +45,24 @@ namespace Artemis.Core.Services
|
|||||||
/// <returns>The resulting end point</returns>
|
/// <returns>The resulting end point</returns>
|
||||||
JsonPluginEndPoint<T> AddResponsiveJsonEndPoint<T>(PluginFeature feature, string endPointName, Func<T, object?> requestHandler);
|
JsonPluginEndPoint<T> AddResponsiveJsonEndPoint<T>(PluginFeature feature, string endPointName, Func<T, object?> requestHandler);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a new endpoint that directly maps received JSON to the data model of the provided <paramref name="module" />.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The data model type of the module</typeparam>
|
||||||
|
/// <param name="module">The module whose datamodel to apply the received JSON to</param>
|
||||||
|
/// <param name="endPointName">The name of the end point, must be unique</param>
|
||||||
|
/// <returns>The resulting end point</returns>
|
||||||
|
DataModelJsonPluginEndPoint<T> AddDataModelJsonEndPoint<T>(Module<T> module, string endPointName) where T : DataModel;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a new endpoint that directly maps received JSON to the data model of the provided <paramref name="dataModelExpansion" />.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The data model type of the module</typeparam>
|
||||||
|
/// <param name="dataModelExpansion">The data model expansion whose datamodel to apply the received JSON to</param>
|
||||||
|
/// <param name="endPointName">The name of the end point, must be unique</param>
|
||||||
|
/// <returns>The resulting end point</returns>
|
||||||
|
DataModelJsonPluginEndPoint<T> AddDataModelJsonEndPoint<T>(DataModelExpansion<T> dataModelExpansion, string endPointName) where T : DataModel;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a new endpoint for the given plugin feature receiving an a <see cref="string" />.
|
/// Adds a new endpoint for the given plugin feature receiving an a <see cref="string" />.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -3,6 +3,8 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Artemis.Core.DataModelExpansions;
|
||||||
|
using Artemis.Core.Modules;
|
||||||
using EmbedIO;
|
using EmbedIO;
|
||||||
using EmbedIO.WebApi;
|
using EmbedIO.WebApi;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
@ -125,6 +127,29 @@ namespace Artemis.Core.Services
|
|||||||
return endPoint;
|
return endPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DataModelJsonPluginEndPoint<T> AddDataModelJsonEndPoint<T>(Module<T> module, string endPointName) where T : DataModel
|
||||||
|
{
|
||||||
|
if (module == null) throw new ArgumentNullException(nameof(module));
|
||||||
|
if (endPointName == null) throw new ArgumentNullException(nameof(endPointName));
|
||||||
|
DataModelJsonPluginEndPoint<T> endPoint = new(module, endPointName, PluginsModule);
|
||||||
|
PluginsModule.AddPluginEndPoint(endPoint);
|
||||||
|
return endPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataModelJsonPluginEndPoint<T> AddDataModelJsonEndPoint<T>(DataModelExpansion<T> dataModelExpansion, string endPointName) where T : DataModel
|
||||||
|
{
|
||||||
|
if (dataModelExpansion == null) throw new ArgumentNullException(nameof(dataModelExpansion));
|
||||||
|
if (endPointName == null) throw new ArgumentNullException(nameof(endPointName));
|
||||||
|
DataModelJsonPluginEndPoint<T> endPoint = new(dataModelExpansion, endPointName, PluginsModule);
|
||||||
|
PluginsModule.AddPluginEndPoint(endPoint);
|
||||||
|
return endPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleDataModelRequest<T>(Module<T> module, T value) where T : DataModel
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void RemovePluginEndPoint(PluginEndPoint endPoint)
|
public void RemovePluginEndPoint(PluginEndPoint endPoint)
|
||||||
{
|
{
|
||||||
PluginsModule.RemovePluginEndPoint(endPoint);
|
PluginsModule.RemovePluginEndPoint(endPoint);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user