< Summary

Information
Class: Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Execute.PostEndpoint
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/Execute/PostEndpoint.cs
Line coverage
85%
Covered lines: 34
Uncovered lines: 6
Coverable lines: 40
Total lines: 77
Line coverage: 85%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Configure()100%11100%
HandleAsync()83.33%131280.64%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/Execute/PostEndpoint.cs

#LineLine coverage
 1using System.Text.Json;
 2using Elsa.Abstractions;
 3using Elsa.Workflows.Management;
 4using Elsa.Workflows.Runtime;
 5using JetBrains.Annotations;
 6
 7namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Execute;
 8
 9/// <summary>
 10/// An API endpoint that executes a given workflow definition through POST method.
 11/// </summary>
 12[PublicAPI]
 1013internal class PostEndpoint(
 1014    IWorkflowDefinitionService workflowDefinitionService,
 1015    IWorkflowRuntime workflowRuntime,
 1016    IWorkflowStarter workflowStarter,
 1017    IApiSerializer apiSerializer)
 18    : ElsaEndpointWithoutRequest<Response>
 19{
 20    /// <inheritdoc />
 21    public override void Configure()
 22    {
 123        Routes("/workflow-definitions/{definitionId}/execute");
 124        ConfigurePermissions("exec:workflow-definitions");
 125        Verbs(FastEndpoints.Http.POST);
 126    }
 27    /// <inheritdoc />
 28    public override async Task HandleAsync(CancellationToken cancellationToken)
 29    {
 930        PostRequest? request = null;
 31
 932        if (HttpContext.Request.ContentType?.Contains("application/json") ?? false)
 33        {
 834            using var reader = new StreamReader(HttpContext.Request.Body);
 835            var body = await reader.ReadToEndAsync();
 36
 837            if (!string.IsNullOrWhiteSpace(body))
 38            {
 39                try
 40                {
 741                    request = JsonSerializer.Deserialize<PostRequest>(body, new JsonSerializerOptions
 742                    {
 743                        PropertyNameCaseInsensitive = true
 744                    });
 745                }
 046                catch
 47                {
 048                    AddError("Invalid request body.");
 049                }
 50            }
 851        }
 52
 953        request ??= new();
 54
 955        var definitionId = Route<string>("definitionId");
 56
 957        if (string.IsNullOrWhiteSpace(definitionId))
 058            AddError("Missing workflow definition ID.");
 59        else
 960            request.DefinitionId = definitionId;
 61
 962        if (ValidationFailed)
 63        {
 064            await Send.ErrorsAsync(cancellation: cancellationToken);
 065            return;
 66        }
 67
 968        await WorkflowExecutionHelper.ExecuteWorkflowAsync(
 969            request,
 970            workflowDefinitionService,
 971            workflowRuntime,
 972            workflowStarter,
 973            apiSerializer,
 974            HttpContext,
 975            cancellationToken);
 976    }
 77}