| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Workflows; |
| | | 3 | | using Elsa.Workflows.Attributes; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Http; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Send an HTTP request. |
| | | 9 | | /// </summary> |
| | | 10 | | [Activity("Elsa", "HTTP", "Send an HTTP request.", DisplayName = "HTTP Request", Kind = ActivityKind.Task)] |
| | | 11 | | public class SendHttpRequest : SendHttpRequestBase |
| | | 12 | | { |
| | | 13 | | /// <inheritdoc /> |
| | 13 | 14 | | public SendHttpRequest([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, l |
| | | 15 | | { |
| | 13 | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// A list of expected status codes to handle and the corresponding activity to execute when the status code matches |
| | | 20 | | /// </summary> |
| | | 21 | | [Input( |
| | | 22 | | Description = "A list of expected status codes to handle and the corresponding activity to execute when the stat |
| | | 23 | | UIHint = "http-status-codes" |
| | | 24 | | )] |
| | 47 | 25 | | public ICollection<HttpStatusCodeCase> ExpectedStatusCodes { get; set; } = new List<HttpStatusCodeCase>(); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The activity to execute when the HTTP status code does not match any of the expected status codes. |
| | | 29 | | /// </summary> |
| | | 30 | | [Port] |
| | 24 | 31 | | public IActivity? UnmatchedStatusCode { get; set; } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The activity to execute when the HTTP request fails to connect. |
| | | 35 | | /// </summary> |
| | | 36 | | [Port] |
| | 15 | 37 | | public IActivity? FailedToConnect { get; set; } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// The activity to execute when the HTTP request times out. |
| | | 41 | | /// </summary> |
| | | 42 | | [Port] |
| | 15 | 43 | | public IActivity? Timeout { get; set; } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | protected override async ValueTask HandleResponseAsync(ActivityExecutionContext context, HttpResponseMessage respons |
| | | 47 | | { |
| | 10 | 48 | | var expectedStatusCodes = ExpectedStatusCodes; |
| | 10 | 49 | | var statusCode = (int)response.StatusCode; |
| | 15 | 50 | | var matchingCase = expectedStatusCodes.FirstOrDefault(x => x.StatusCode == statusCode); |
| | 10 | 51 | | var activity = matchingCase != null ? matchingCase.Activity : UnmatchedStatusCode; |
| | | 52 | | |
| | 10 | 53 | | await context.ScheduleActivityAsync(activity, OnChildActivityCompletedAsync); |
| | 10 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | protected override async ValueTask HandleRequestExceptionAsync(ActivityExecutionContext context, HttpRequestExceptio |
| | | 58 | | { |
| | 1 | 59 | | await context.ScheduleActivityAsync(FailedToConnect, OnChildActivityCompletedAsync); |
| | 1 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | protected override async ValueTask HandleTaskCanceledExceptionAsync(ActivityExecutionContext context, TaskCanceledEx |
| | | 64 | | { |
| | 1 | 65 | | await context.ScheduleActivityAsync(Timeout, OnChildActivityCompletedAsync); |
| | 1 | 66 | | } |
| | | 67 | | |
| | | 68 | | private async ValueTask OnChildActivityCompletedAsync(ActivityCompletedContext context) |
| | | 69 | | { |
| | 0 | 70 | | await context.TargetContext.CompleteActivityAsync(); |
| | 0 | 71 | | } |
| | | 72 | | } |