< Summary

Information
Class: Elsa.Extensions.EventExpressionExecutionContextExtensions
Assembly: Elsa.Http
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Extensions/EventExpressionExecutionContextExtensions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 18
Coverable lines: 18
Total lines: 74
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GenerateEventTriggerUrl(...)100%210%
GenerateEventTriggerUrl(...)100%210%
GenerateEventTriggerUrl(...)100%210%
GenerateEventTriggerUrlInternal(...)100%210%
GenerateEventTriggerTokenInternal(...)0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Extensions/EventExpressionExecutionContextExtensions.cs

#LineLine coverage
 1using Elsa.Expressions.Models;
 2using Elsa.Http;
 3using Elsa.SasTokens.Contracts;
 4using Elsa.Workflows.Api;
 5using Elsa.Workflows.Runtime;
 6using Microsoft.Extensions.Options;
 7
 8// ReSharper disable once CheckNamespace
 9namespace Elsa.Extensions;
 10
 11/// <summary>
 12/// Provides extension methods for working with <see cref="ExpressionExecutionContext"/>.
 13/// </summary>
 14public static class EventExpressionExecutionContextExtensions
 15{
 16    /// <summary>
 17    /// Generates a URL that can be used to trigger an event.
 18    /// </summary>
 19    /// <param name="context">The expression execution context.</param>
 20    /// <param name="eventName">The name of the event to trigger.</param>
 21    /// <param name="lifetime">The lifetime of the event trigger token.</param>
 22    /// <returns>A URL that can be used to trigger an event.</returns>
 23    public static string GenerateEventTriggerUrl(this ExpressionExecutionContext context, string eventName, TimeSpan lif
 24    {
 025        var token = context.GenerateEventTriggerTokenInternal(eventName, lifetime);
 026        return context.GenerateEventTriggerUrlInternal(token);
 27    }
 28
 29    /// <summary>
 30    /// Generates a URL that can be used to trigger an event.
 31    /// </summary>
 32    /// <param name="context">The expression execution context.</param>
 33    /// <param name="eventName">The name of the event to trigger.</param>
 34    /// <param name="expiresAt">The expiration date of the event trigger token.</param>
 35    /// <returns>A URL that can be used to trigger an event.</returns>
 36    public static string GenerateEventTriggerUrl(this ExpressionExecutionContext context, string eventName, DateTimeOffs
 37    {
 038        var token = context.GenerateEventTriggerTokenInternal(eventName, expiresAt: expiresAt);
 039        return context.GenerateEventTriggerUrlInternal(token);
 40    }
 41
 42    /// <summary>
 43    /// Generates a URL that can be used to trigger an event.
 44    /// </summary>
 45    /// <param name="context">The expression execution context.</param>
 46    /// <param name="eventName">The name of the event to trigger.</param>
 47    /// <returns>A URL that can be used to trigger an event.</returns>
 48    public static string GenerateEventTriggerUrl(this ExpressionExecutionContext context, string eventName)
 49    {
 050        var token = context.GenerateEventTriggerTokenInternal(eventName);
 051        return context.GenerateEventTriggerUrlInternal(token);
 52    }
 53
 54    private static string GenerateEventTriggerUrlInternal(this ExpressionExecutionContext context, string token)
 55    {
 056        var options = context.GetRequiredService<IOptions<ApiEndpointOptions>>().Value;
 057        var url = $"{options.RoutePrefix}/events/trigger?t={token}";
 058        var absoluteUrlProvider = context.GetRequiredService<IAbsoluteUrlProvider>();
 059        return absoluteUrlProvider.ToAbsoluteUrl(url).ToString();
 60    }
 61
 62    private static string GenerateEventTriggerTokenInternal(this ExpressionExecutionContext context, string eventName, T
 63    {
 064        var workflowInstanceId = context.GetWorkflowExecutionContext().Id;
 065        var payload = new EventTokenPayload(eventName, workflowInstanceId);
 066        var tokenService = context.GetRequiredService<ITokenService>();
 67
 068        return lifetime != null
 069            ? tokenService.CreateToken(payload, lifetime.Value)
 070            : expiresAt != null
 071                ? tokenService.CreateToken(payload, expiresAt.Value)
 072                : tokenService.CreateToken(payload);
 73    }
 74}