| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using FastEndpoints; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.AspNetCore.Builder; |
| | | 8 | | using Microsoft.AspNetCore.Http; |
| | | 9 | | using Microsoft.AspNetCore.Routing; |
| | | 10 | | using Microsoft.Extensions.DependencyInjection; |
| | | 11 | | |
| | | 12 | | // ReSharper disable once CheckNamespace |
| | | 13 | | namespace Elsa.Extensions; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Provides extension methods to add the FastEndpoints middleware configured for use with Elsa API endpoints. |
| | | 17 | | /// </summary> |
| | | 18 | | [PublicAPI] |
| | | 19 | | public static class WebApplicationExtensions |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Register the FastEndpoints middleware configured for use with with Elsa API endpoints. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="app"></param> |
| | | 25 | | /// <param name="routePrefix">The route prefix to apply to Elsa API endpoints.</param> |
| | | 26 | | /// <example>E.g. "elsa/api" will expose endpoints like this: "/elsa/api/workflow-definitions"</example> |
| | | 27 | | public static IApplicationBuilder UseWorkflowsApi(this IApplicationBuilder app, string routePrefix = "elsa/api") |
| | | 28 | | { |
| | 1 | 29 | | return app.UseFastEndpoints(config => |
| | 1 | 30 | | { |
| | 1 | 31 | | config.Endpoints.RoutePrefix = routePrefix; |
| | 1 | 32 | | config.Serializer.RequestDeserializer = DeserializeRequestAsync; |
| | 1 | 33 | | config.Serializer.ResponseSerializer = SerializeRequestAsync; |
| | 1 | 34 | | |
| | 1 | 35 | | config.Binding.ValueParserFor<DateTimeOffset>(s => |
| | 1 | 36 | | new(DateTimeOffset.TryParse(s.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, ou |
| | 2 | 37 | | }); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Register the FastEndpoints middleware configured for use with with Elsa API endpoints. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="routes">The <see cref="IEndpointRouteBuilder"/> to register the endpoints with.</param> |
| | | 44 | | /// <param name="routePrefix">The route prefix to apply to Elsa API endpoints.</param> |
| | | 45 | | /// <example>E.g. "elsa/api" will expose endpoints like this: "/elsa/api/workflow-definitions"</example> |
| | | 46 | | public static IEndpointRouteBuilder MapWorkflowsApi(this IEndpointRouteBuilder routes, string routePrefix = "elsa/ap |
| | 0 | 47 | | routes.MapFastEndpoints(config => |
| | 0 | 48 | | { |
| | 0 | 49 | | config.Endpoints.RoutePrefix = routePrefix; |
| | 0 | 50 | | config.Serializer.RequestDeserializer = DeserializeRequestAsync; |
| | 0 | 51 | | config.Serializer.ResponseSerializer = SerializeRequestAsync; |
| | 0 | 52 | | }); |
| | | 53 | | |
| | | 54 | | private static ValueTask<object?> DeserializeRequestAsync(HttpRequest httpRequest, Type modelType, JsonSerializerCon |
| | | 55 | | { |
| | 2 | 56 | | var serializer = httpRequest.HttpContext.RequestServices.GetRequiredService<IApiSerializer>(); |
| | 2 | 57 | | var options = serializer.GetOptions(); |
| | | 58 | | |
| | 2 | 59 | | return serializerContext == null |
| | 2 | 60 | | ? JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, options, cancellationToken) |
| | 2 | 61 | | : JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, serializerContext, cancellationToken); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | private static Task SerializeRequestAsync(HttpResponse httpResponse, object? dto, string contentType, JsonSerializer |
| | | 65 | | { |
| | 3 | 66 | | var serializer = httpResponse.HttpContext.RequestServices.GetRequiredService<IApiSerializer>(); |
| | 3 | 67 | | var options = serializer.GetOptions(); |
| | | 68 | | |
| | 3 | 69 | | httpResponse.ContentType = contentType; |
| | 3 | 70 | | return serializerContext == null |
| | 3 | 71 | | ? JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto?.GetType() ?? typeof(object), options, cancellat |
| | 3 | 72 | | : JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto?.GetType() ?? typeof(object), serializerContext, |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | } |