elsa-core/src/modules/Elsa.Modules.Http/Activities/HttpEndpoint.cs

60 lines
2.2 KiB
C#
Raw Normal View History

2022-01-04 08:42:12 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Elsa.Attributes;
using Elsa.Extensions;
2022-01-04 08:42:12 +00:00
using Elsa.Management.Models;
using Elsa.Models;
2022-02-22 11:51:59 +00:00
using Elsa.Modules.Http.Models;
2022-01-04 08:42:12 +00:00
2022-02-01 19:45:11 +00:00
namespace Elsa.Modules.Http;
2022-01-04 08:42:12 +00:00
[Activity("Http", "Waits for an inbound HTTP request that matches the specified path and methods", category: "HTTP")]
public class HttpEndpoint : Trigger<HttpRequestModel>
2022-01-04 08:42:12 +00:00
{
public const string InputKey = "HttpRequest";
2022-01-04 08:42:12 +00:00
[Input] public Input<string> Path { get; set; } = default!;
[Input(
Options = new[] { "GET", "POST", "PUT" },
UIHint = InputUIHints.CheckList
)]
public Input<ICollection<string>> SupportedMethods { get; set; } = new(new[] { HttpMethod.Get.Method });
[Input(
Description = "Allow authenticated requests only",
Category = "Security"
)]
public Input<bool> Authorize { get; set; } = new(false);
[Input(
Description = "Provide a policy to evaluate. If the policy fails, the request is forbidden.",
Category = "Security"
)]
public Input<string?> Policy { get; set; } = new(default(string?));
2022-01-04 08:42:12 +00:00
2022-03-07 11:01:27 +00:00
protected override IEnumerable<object> GetTriggerData(TriggerIndexingContext context) => GetBookmarkData(context.ExpressionExecutionContext);
protected override void Execute(ActivityExecutionContext context)
{
// If we did not receive external input, it means we are just now encountering this activity.
if (!context.TryGetInput<HttpRequestModel>(InputKey, out var request))
{
// Create bookmarks for when we receive the expected HTTP request.
context.CreateBookmarks(GetBookmarkData(context.ExpressionExecutionContext));
return;
}
// Provide the received HTTP request as output.
context.Set(Result, request);
}
2022-01-04 08:42:12 +00:00
2022-03-07 11:01:27 +00:00
private IEnumerable<object> GetBookmarkData(ExpressionExecutionContext context)
2022-01-04 08:42:12 +00:00
{
2022-03-07 11:01:27 +00:00
// Generate bookmark data for path and selected methods.
var path = context.Get(Path);
var methods = context.Get(SupportedMethods);
2022-03-07 11:01:27 +00:00
return methods!.Select(x => new HttpBookmarkData(path!, x.ToLowerInvariant())).Cast<object>().ToArray();
2022-01-04 08:42:12 +00:00
}
}