2023-08-06 18:55:24 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2023-01-16 18:42:45 +00:00
|
|
|
using Elsa.Extensions;
|
2023-08-06 18:55:24 +00:00
|
|
|
using Elsa.Http.Models;
|
2023-03-03 13:48:05 +00:00
|
|
|
using Elsa.Workflows.Core;
|
2022-10-25 13:52:55 +00:00
|
|
|
using Elsa.Workflows.Core.Attributes;
|
2023-03-07 12:45:58 +00:00
|
|
|
using Elsa.Workflows.Core.Contracts;
|
|
|
|
|
using JetBrains.Annotations;
|
2022-10-25 13:52:55 +00:00
|
|
|
|
|
|
|
|
namespace Elsa.Http;
|
|
|
|
|
|
2023-03-07 12:45:58 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Send an HTTP request.
|
|
|
|
|
/// </summary>
|
2023-08-06 18:55:24 +00:00
|
|
|
[Activity("Elsa", "HTTP", "Send an HTTP request.", DisplayName = "HTTP Request", Kind = ActivityKind.Task)]
|
|
|
|
|
public class SendHttpRequest : SendHttpRequestBase
|
2023-03-07 12:45:58 +00:00
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
2023-08-06 18:55:24 +00:00
|
|
|
public SendHttpRequest([CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line)
|
2023-03-07 12:45:58 +00:00
|
|
|
{
|
|
|
|
|
}
|
2023-08-06 18:55:24 +00:00
|
|
|
|
2023-03-07 12:45:58 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A list of expected status codes to handle and the corresponding activity to execute when the status code matches.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Input(
|
|
|
|
|
Description = "A list of expected status codes to handle and the corresponding activity to execute when the status code matches.",
|
2023-03-11 21:14:39 +00:00
|
|
|
UIHint = "http-status-codes"
|
2023-03-07 12:45:58 +00:00
|
|
|
)]
|
|
|
|
|
public ICollection<HttpStatusCodeCase> ExpectedStatusCodes { get; set; } = new List<HttpStatusCodeCase>();
|
2023-03-07 13:54:21 +00:00
|
|
|
|
2023-03-07 12:45:58 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// The activity to execute when the HTTP status code does not match any of the expected status codes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Port]
|
2023-03-11 21:14:39 +00:00
|
|
|
public IActivity? UnmatchedStatusCode { get; set; }
|
2023-03-07 12:45:58 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected override async ValueTask HandleResponseAsync(ActivityExecutionContext context, HttpResponseMessage response)
|
|
|
|
|
{
|
|
|
|
|
var expectedStatusCodes = ExpectedStatusCodes;
|
|
|
|
|
var statusCode = (int)response.StatusCode;
|
|
|
|
|
var matchingCase = expectedStatusCodes.FirstOrDefault(x => x.StatusCode == statusCode);
|
2023-03-11 21:14:39 +00:00
|
|
|
var activity = matchingCase?.Activity ?? UnmatchedStatusCode;
|
2023-03-07 12:45:58 +00:00
|
|
|
|
|
|
|
|
await context.ScheduleActivityAsync(activity, OnChildActivityCompletedAsync);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async ValueTask OnChildActivityCompletedAsync(ActivityExecutionContext context, ActivityExecutionContext childContext)
|
|
|
|
|
{
|
|
|
|
|
await context.CompleteActivityAsync();
|
|
|
|
|
}
|
2022-10-27 10:05:30 +00:00
|
|
|
}
|