2023-09-22 09:13:23 +00:00
|
|
|
using System.Reflection;
|
2023-08-06 18:55:24 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using Elsa.Extensions;
|
|
|
|
|
using Elsa.Workflows.Core;
|
|
|
|
|
using Elsa.Workflows.Core.Attributes;
|
2023-09-22 09:13:23 +00:00
|
|
|
using Elsa.Workflows.Core.Contracts;
|
2023-08-06 18:55:24 +00:00
|
|
|
using Elsa.Workflows.Core.Models;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Http;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Send an HTTP request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Activity("Elsa", "HTTP", "Send an HTTP request.", DisplayName = "HTTP Request (flow)", Kind = ActivityKind.Task)]
|
2023-09-22 09:13:23 +00:00
|
|
|
public class FlowSendHttpRequest : SendHttpRequestBase, IActivityPropertyDefaultValueProvider
|
2023-08-06 18:55:24 +00:00
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public FlowSendHttpRequest([CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line)
|
|
|
|
|
{
|
|
|
|
|
}
|
2023-09-22 09:13:23 +00:00
|
|
|
|
2023-08-06 18:55:24 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A list of expected status codes to handle.
|
|
|
|
|
/// </summary>
|
2023-09-22 09:13:23 +00:00
|
|
|
[Input(
|
|
|
|
|
Description = "A list of expected status codes to handle.",
|
|
|
|
|
UIHint = InputUIHints.MultiText,
|
|
|
|
|
DefaultValueProvider = typeof(FlowSendHttpRequest)
|
|
|
|
|
)]
|
2023-08-06 18:55:24 +00:00
|
|
|
public Input<ICollection<int>> ExpectedStatusCodes { get; set; } = default!;
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected override async ValueTask HandleResponseAsync(ActivityExecutionContext context, HttpResponseMessage response)
|
|
|
|
|
{
|
|
|
|
|
var expectedStatusCodes = ExpectedStatusCodes.GetOrDefault(context) ?? new List<int>(0);
|
|
|
|
|
var statusCode = (int)response.StatusCode;
|
|
|
|
|
var hasMatchingStatusCode = expectedStatusCodes.Contains(statusCode);
|
2023-11-22 19:46:12 +00:00
|
|
|
var outcome = expectedStatusCodes.Any() ? hasMatchingStatusCode ? statusCode.ToString() : "Unmatched status code" : default;
|
|
|
|
|
var outcomes = new List<string>();
|
2023-08-06 18:55:24 +00:00
|
|
|
|
2023-11-22 19:46:12 +00:00
|
|
|
if (outcome != null)
|
|
|
|
|
outcomes.Add(outcome);
|
|
|
|
|
|
|
|
|
|
outcomes.Add("Done");
|
|
|
|
|
|
|
|
|
|
context.JournalData["StatusCode"] = statusCode;
|
|
|
|
|
await context.CompleteActivityWithOutcomesAsync(outcomes.ToArray());
|
2023-08-06 18:55:24 +00:00
|
|
|
}
|
2023-09-07 18:34:53 +00:00
|
|
|
|
2023-09-10 19:31:57 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected override async ValueTask HandleRequestExceptionAsync(ActivityExecutionContext context, HttpRequestException exception)
|
|
|
|
|
{
|
|
|
|
|
await context.CompleteActivityWithOutcomesAsync("Failed to connect");
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-18 13:56:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected override async ValueTask HandleTaskCanceledExceptionAsync(ActivityExecutionContext context, TaskCanceledException exception)
|
|
|
|
|
{
|
|
|
|
|
await context.CompleteActivityWithOutcomesAsync("Timeout");
|
|
|
|
|
}
|
2023-09-22 09:13:23 +00:00
|
|
|
|
|
|
|
|
object IActivityPropertyDefaultValueProvider.GetDefaultValue(PropertyInfo property)
|
|
|
|
|
{
|
|
|
|
|
if (property.Name == nameof(ExpectedStatusCodes))
|
|
|
|
|
{
|
|
|
|
|
return new List<int> { 200 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return default!;
|
|
|
|
|
}
|
2023-08-06 18:55:24 +00:00
|
|
|
}
|