| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.Workflows.Api.Security; |
| | | 4 | | using Elsa.Workflows.Management; |
| | | 5 | | using Elsa.Workflows.Runtime; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Execute; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// An API endpoint that executes a given workflow definition through POST method. |
| | | 12 | | /// </summary> |
| | | 13 | | [PublicAPI] |
| | 12 | 14 | | internal class PostEndpoint( |
| | 12 | 15 | | IWorkflowDefinitionService workflowDefinitionService, |
| | 12 | 16 | | IWorkflowRuntime workflowRuntime, |
| | 12 | 17 | | IWorkflowStarter workflowStarter, |
| | 12 | 18 | | IApiSerializer apiSerializer, |
| | 12 | 19 | | WorkflowDefinitionScriptAuthorizationService scriptAuthorizationService) |
| | | 20 | | : ElsaEndpointWithoutRequest<Response> |
| | | 21 | | { |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public override void Configure() |
| | | 24 | | { |
| | 3 | 25 | | Routes("/workflow-definitions/{definitionId}/execute"); |
| | 3 | 26 | | ConfigurePermissions("exec:workflow-definitions"); |
| | 3 | 27 | | Verbs(FastEndpoints.Http.POST); |
| | 3 | 28 | | } |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 31 | | { |
| | 9 | 32 | | PostRequest? request = null; |
| | | 33 | | |
| | 9 | 34 | | if (HttpContext.Request.ContentType?.Contains("application/json") ?? false) |
| | | 35 | | { |
| | 8 | 36 | | using var reader = new StreamReader(HttpContext.Request.Body); |
| | 8 | 37 | | var body = await reader.ReadToEndAsync(cancellationToken); |
| | | 38 | | |
| | 8 | 39 | | if (!string.IsNullOrWhiteSpace(body)) |
| | | 40 | | { |
| | | 41 | | try |
| | | 42 | | { |
| | 7 | 43 | | request = JsonSerializer.Deserialize<PostRequest>(body, new JsonSerializerOptions |
| | 7 | 44 | | { |
| | 7 | 45 | | PropertyNameCaseInsensitive = true |
| | 7 | 46 | | }); |
| | 7 | 47 | | } |
| | 0 | 48 | | catch |
| | | 49 | | { |
| | 0 | 50 | | AddError("Invalid request body."); |
| | 0 | 51 | | } |
| | | 52 | | } |
| | 8 | 53 | | } |
| | | 54 | | |
| | 9 | 55 | | request ??= new(); |
| | | 56 | | |
| | 9 | 57 | | var definitionId = Route<string>("definitionId"); |
| | | 58 | | |
| | 9 | 59 | | if (string.IsNullOrWhiteSpace(definitionId)) |
| | 0 | 60 | | AddError("Missing workflow definition ID."); |
| | | 61 | | else |
| | 9 | 62 | | request.DefinitionId = definitionId; |
| | | 63 | | |
| | 9 | 64 | | if (ValidationFailed) |
| | | 65 | | { |
| | 0 | 66 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | 0 | 67 | | return; |
| | | 68 | | } |
| | | 69 | | |
| | 9 | 70 | | await WorkflowExecutionHelper.ExecuteWorkflowAsync( |
| | 9 | 71 | | request, |
| | 9 | 72 | | workflowDefinitionService, |
| | 9 | 73 | | workflowRuntime, |
| | 9 | 74 | | workflowStarter, |
| | 9 | 75 | | apiSerializer, |
| | 9 | 76 | | scriptAuthorizationService, |
| | 9 | 77 | | HttpContext, |
| | 9 | 78 | | cancellationToken); |
| | 9 | 79 | | } |
| | | 80 | | } |