< Summary

Information
Class: Elsa.Extensions.BookmarkExecutionContextExtensions
Assembly: Elsa.Http
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Extensions/BookmarkExecutionContextExtensions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 21
Coverable lines: 21
Total lines: 79
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

File(s)

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

#LineLine coverage
 1using Elsa.Expressions.Models;
 2using Elsa.Http;
 3using Elsa.SasTokens.Contracts;
 4using Elsa.Workflows;
 5using Elsa.Workflows.Api;
 6using Elsa.Workflows.Runtime;
 7using Microsoft.Extensions.Options;
 8
 9// ReSharper disable once CheckNamespace
 10namespace Elsa.Extensions;
 11
 12/// <summary>
 13/// Provides extension methods for working with <see cref="ExpressionExecutionContext"/> and generating bookmark trigger
 14/// </summary>
 15public static class BookmarkExecutionContextExtensions
 16{
 017    public static string GenerateBookmarkTriggerUrl(this ActivityExecutionContext context, string bookmarkId, TimeSpan l
 018    public static string GenerateBookmarkTriggerUrl(this ActivityExecutionContext context, string bookmarkId, DateTimeOf
 019    public static string GenerateBookmarkTriggerUrl(this ActivityExecutionContext context, string bookmarkId) => context
 20
 21    /// <summary>
 22    /// Generates a URL that can be used to resume a bookmarked workflow.
 23    /// </summary>
 24    /// <param name="context">The expression execution context.</param>
 25    /// <param name="bookmarkId">The ID of the bookmark to resume.</param>
 26    /// <param name="lifetime">The lifetime of the bookmark trigger token.</param>
 27    /// <returns>A URL that can be used to resume a bookmarked workflow.</returns>
 28    public static string GenerateBookmarkTriggerUrl(this ExpressionExecutionContext context, string bookmarkId, TimeSpan
 29    {
 030        var token = context.GenerateBookmarkTriggerTokenInternal(bookmarkId, lifetime);
 031        return context.GenerateBookmarkTriggerUrlInternal(token);
 32    }
 33
 34    /// <summary>
 35    /// Generates a URL that can be used to resume a bookmarked workflow.
 36    /// </summary>
 37    /// <param name="context">The expression execution context.</param>
 38    /// <param name="bookmarkId">The ID of the bookmark to resume.</param>
 39    /// <param name="expiresAt">The expiration date of the bookmark trigger token.</param>
 40    /// <returns>A URL that can be used to resume a bookmarked workflow.</returns>
 41    public static string GenerateBookmarkTriggerUrl(this ExpressionExecutionContext context, string bookmarkId, DateTime
 42    {
 043        var token = context.GenerateBookmarkTriggerTokenInternal(bookmarkId, expiresAt: expiresAt);
 044        return context.GenerateBookmarkTriggerUrlInternal(token);
 45    }
 46
 47    /// <summary>
 48    /// Generates a URL that can be used to resume a bookmarked workflow.
 49    /// </summary>
 50    /// <param name="context">The expression execution context.</param>
 51    /// <param name="bookmarkId">The ID of the bookmark to resume.</param>
 52    /// <returns>A URL that can be used to trigger an event.</returns>
 53    public static string GenerateBookmarkTriggerUrl(this ExpressionExecutionContext context, string bookmarkId)
 54    {
 055        var token = context.GenerateBookmarkTriggerTokenInternal(bookmarkId);
 056        return context.GenerateBookmarkTriggerUrlInternal(token);
 57    }
 58
 59    private static string GenerateBookmarkTriggerUrlInternal(this ExpressionExecutionContext context, string token)
 60    {
 061        var options = context.GetRequiredService<IOptions<ApiEndpointOptions>>().Value;
 062        var url = $"{options.RoutePrefix}/bookmarks/resume?t={token}";
 063        var absoluteUrlProvider = context.GetRequiredService<IAbsoluteUrlProvider>();
 064        return absoluteUrlProvider.ToAbsoluteUrl(url).ToString();
 65    }
 66
 67    private static string GenerateBookmarkTriggerTokenInternal(this ExpressionExecutionContext context, string bookmarkI
 68    {
 069        var workflowInstanceId = context.GetWorkflowExecutionContext().Id;
 070        var payload = new BookmarkTokenPayload(bookmarkId, workflowInstanceId);
 071        var tokenService = context.GetRequiredService<ITokenService>();
 72
 073        return lifetime != null
 074            ? tokenService.CreateToken(payload, lifetime.Value)
 075            : expiresAt != null
 076                ? tokenService.CreateToken(payload, expiresAt.Value)
 077                : tokenService.CreateToken(payload);
 78    }
 79}