2022-08-27 18:42:06 +00:00
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
using Elsa.Workflows.Core.Serialization;
|
|
|
|
|
using FastEndpoints;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Extensions;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides extension methods to add the FastEndpoints middleware configured for use with Elsa API endpoints.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class WebApplicationExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Register the FastEndpoints middleware configured for use with with Elsa API endpoints.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="app"></param>
|
|
|
|
|
/// <param name="routePrefix">The route prefix to apply to Elsa API endpoints.</param>
|
|
|
|
|
/// <example>E.g. "elsa/api" will expose endpoints like this: "/elsa/api/workflow-definitions"</example>
|
2022-12-30 12:11:29 +00:00
|
|
|
public static IApplicationBuilder UseWorkflowsApi(this IApplicationBuilder app, string routePrefix = "elsa/api") =>
|
2022-12-09 11:25:39 +00:00
|
|
|
app.UseFastEndpoints(config =>
|
2022-08-27 18:42:06 +00:00
|
|
|
{
|
|
|
|
|
config.Endpoints.RoutePrefix = routePrefix;
|
|
|
|
|
config.Serializer.RequestDeserializer = DeserializeRequestAsync;
|
|
|
|
|
config.Serializer.ResponseSerializer = SerializeRequestAsync;
|
2022-12-09 11:25:39 +00:00
|
|
|
});
|
2022-08-27 18:42:06 +00:00
|
|
|
|
2022-12-09 11:25:39 +00:00
|
|
|
private static ValueTask<object?> DeserializeRequestAsync(HttpRequest httpRequest, Type modelType, JsonSerializerContext? serializerContext, CancellationToken cancellationToken)
|
2022-08-27 18:42:06 +00:00
|
|
|
{
|
|
|
|
|
var serializerOptionsProvider = httpRequest.HttpContext.RequestServices.GetRequiredService<SerializerOptionsProvider>();
|
|
|
|
|
var options = serializerOptionsProvider.CreateApiOptions();
|
|
|
|
|
|
|
|
|
|
return serializerContext == null
|
|
|
|
|
? JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, options, cancellationToken)
|
|
|
|
|
: JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, serializerContext, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 11:25:39 +00:00
|
|
|
private static Task SerializeRequestAsync(HttpResponse httpResponse, object? dto, string contentType, JsonSerializerContext? serializerContext, CancellationToken cancellationToken)
|
2022-08-27 18:42:06 +00:00
|
|
|
{
|
|
|
|
|
var serializerOptionsProvider = httpResponse.HttpContext.RequestServices.GetRequiredService<SerializerOptionsProvider>();
|
|
|
|
|
var options = serializerOptionsProvider.CreateApiOptions();
|
|
|
|
|
|
|
|
|
|
httpResponse.ContentType = contentType;
|
|
|
|
|
return serializerContext == null
|
|
|
|
|
? JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto.GetType(), options, cancellationToken)
|
|
|
|
|
: JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto.GetType(), serializerContext, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|