| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Elsa.Workflows.Attributes; |
| | | 6 | | using Elsa.Workflows.UIHints; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Http; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Send an HTTP request. |
| | | 13 | | /// </summary> |
| | | 14 | | [Activity("Elsa", "HTTP", "Send an HTTP request.", DisplayName = "HTTP Request (flow)", Kind = ActivityKind.Task)] |
| | | 15 | | public class FlowSendHttpRequest : SendHttpRequestBase, IActivityPropertyDefaultValueProvider |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc /> |
| | 70 | 18 | | public FlowSendHttpRequest([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(sourc |
| | | 19 | | { |
| | 70 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// A list of expected status codes to handle. |
| | | 24 | | /// </summary> |
| | | 25 | | [Input( |
| | | 26 | | Description = "A list of expected status codes to handle.", |
| | | 27 | | UIHint = InputUIHints.MultiText, |
| | | 28 | | DefaultValueProvider = typeof(FlowSendHttpRequest), |
| | | 29 | | Order = 5.1f |
| | | 30 | | )] |
| | 63 | 31 | | public Input<ICollection<int>> ExpectedStatusCodes { get; set; } = null!; |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | protected override async ValueTask HandleResponseAsync(ActivityExecutionContext context, HttpResponseMessage respons |
| | | 35 | | { |
| | 12 | 36 | | var expectedStatusCodes = ExpectedStatusCodes.GetOrDefault(context) ?? new List<int>(0); |
| | 12 | 37 | | var statusCode = (int)response.StatusCode; |
| | 12 | 38 | | var hasMatchingStatusCode = expectedStatusCodes.Contains(statusCode); |
| | 12 | 39 | | var outcome = expectedStatusCodes.Any() ? hasMatchingStatusCode ? statusCode.ToString() : "Unmatched status code |
| | 12 | 40 | | var outcomes = new List<string>(); |
| | | 41 | | |
| | 12 | 42 | | if (outcome != null) |
| | 11 | 43 | | outcomes.Add(outcome); |
| | | 44 | | |
| | 12 | 45 | | outcomes.Add("Done"); |
| | 12 | 46 | | await context.CompleteActivityWithOutcomesAsync(outcomes.ToArray()); |
| | 12 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | protected override async ValueTask HandleRequestExceptionAsync(ActivityExecutionContext context, HttpRequestExceptio |
| | | 51 | | { |
| | 1 | 52 | | await context.CompleteActivityWithOutcomesAsync("Failed to connect"); |
| | 1 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | protected override async ValueTask HandleTaskCanceledExceptionAsync(ActivityExecutionContext context, TaskCanceledEx |
| | | 57 | | { |
| | 1 | 58 | | await context.CompleteActivityWithOutcomesAsync("Timeout"); |
| | 1 | 59 | | } |
| | | 60 | | |
| | | 61 | | object IActivityPropertyDefaultValueProvider.GetDefaultValue(PropertyInfo property) |
| | | 62 | | { |
| | 55 | 63 | | if (property.Name == nameof(ExpectedStatusCodes)) |
| | | 64 | | { |
| | 55 | 65 | | return new List<int> { 200 }; |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | return null!; |
| | | 69 | | } |
| | | 70 | | } |