2022-11-23 10:20:00 +00:00
|
|
|
using Elsa.Common.Features;
|
2022-12-31 15:16:43 +00:00
|
|
|
using Elsa.Extensions;
|
2022-05-27 10:36:39 +00:00
|
|
|
using Elsa.Features.Abstractions;
|
2022-11-23 10:20:00 +00:00
|
|
|
using Elsa.Features.Attributes;
|
2022-05-27 10:36:39 +00:00
|
|
|
using Elsa.Features.Services;
|
2022-10-25 13:52:55 +00:00
|
|
|
using Elsa.Http.ContentWriters;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Http.Handlers;
|
|
|
|
|
using Elsa.Http.Implementations;
|
|
|
|
|
using Elsa.Http.Options;
|
2022-10-25 13:52:55 +00:00
|
|
|
using Elsa.Http.Parsers;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Http.Services;
|
2023-01-16 18:42:45 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Routing;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
2022-05-27 10:36:39 +00:00
|
|
|
namespace Elsa.Http.Features;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
2022-11-23 10:20:00 +00:00
|
|
|
[DependsOn(typeof(MemoryCacheFeature))]
|
2022-05-27 10:36:39 +00:00
|
|
|
public class HttpFeature : FeatureBase
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
2022-05-27 10:36:39 +00:00
|
|
|
public HttpFeature(IModule module) : base(module)
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 18:42:45 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate to configure <see cref="HttpActivityOptions"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<HttpActivityOptions>? ConfigureHttpOptions { get; set; }
|
|
|
|
|
|
2023-01-24 20:03:50 +00:00
|
|
|
public Func<IServiceProvider, IHttpEndpointAuthorizationHandler> HttpEndpointAuthorizationHandler { get; set; } = ActivatorUtilities.GetServiceOrCreateInstance<AllowAnonymousHttpEndpointAuthorizationHandler>;
|
|
|
|
|
public Func<IServiceProvider, IHttpEndpointWorkflowFaultHandler> HttpEndpointWorkflowFaultHandler { get; set; } = ActivatorUtilities.GetServiceOrCreateInstance<DefaultHttpEndpointWorkflowFaultHandler>;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
2023-01-16 18:42:45 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate to configure the <see cref="HttpClient"/> used when by the <see cref="SendHttpRequest"/> activity.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<IServiceProvider, HttpClient> HttpClient { get; set; } = (_, _) => { };
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate to configure the <see cref="HttpClientBuilder"/> for <see cref="HttpClient"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<IHttpClientBuilder> HttpClientBuilder { get; set; } = _ => { };
|
|
|
|
|
|
2022-11-27 12:28:43 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Configure()
|
2022-05-26 10:47:31 +00:00
|
|
|
{
|
2023-01-16 18:42:45 +00:00
|
|
|
Module.UseWorkflowManagement(management =>
|
2022-11-27 12:28:43 +00:00
|
|
|
{
|
2023-01-16 18:42:45 +00:00
|
|
|
management.AddVariableTypes(new[]
|
|
|
|
|
{
|
|
|
|
|
typeof(RouteData),
|
|
|
|
|
typeof(HttpRequest),
|
|
|
|
|
typeof(HttpResponse)
|
|
|
|
|
}, "HTTP");
|
|
|
|
|
|
|
|
|
|
management.AddActivitiesFrom<HttpFeature>();
|
|
|
|
|
});
|
2022-05-26 10:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-27 12:28:43 +00:00
|
|
|
/// <inheritdoc />
|
2022-05-26 10:47:31 +00:00
|
|
|
public override void Apply()
|
|
|
|
|
{
|
2022-11-19 21:30:43 +00:00
|
|
|
var configureOptions = ConfigureHttpOptions ?? (options =>
|
|
|
|
|
{
|
|
|
|
|
options.BasePath = "/workflows";
|
|
|
|
|
options.BaseUrl = new Uri("http://localhost");
|
|
|
|
|
});
|
2023-01-16 18:42:45 +00:00
|
|
|
|
2022-11-19 21:30:43 +00:00
|
|
|
Services.Configure(configureOptions);
|
2022-05-26 10:47:31 +00:00
|
|
|
|
2023-01-16 18:42:45 +00:00
|
|
|
var httpClientBuilder = Services.AddHttpClient<SendHttpRequest>(HttpClient);
|
|
|
|
|
HttpClientBuilder(httpClientBuilder);
|
|
|
|
|
|
2022-05-26 10:47:31 +00:00
|
|
|
Services
|
|
|
|
|
.AddSingleton<IRouteMatcher, RouteMatcher>()
|
|
|
|
|
.AddSingleton<IRouteTable, RouteTable>()
|
2022-11-19 21:30:43 +00:00
|
|
|
.AddSingleton<IAbsoluteUrlProvider, DefaultAbsoluteUrlProvider>()
|
2023-01-24 20:03:50 +00:00
|
|
|
.AddSingleton<IHttpBookmarkProcessor, HttpBookmarkProcessor>()
|
2022-05-26 10:47:31 +00:00
|
|
|
.AddNotificationHandlersFrom<UpdateRouteTable>()
|
2022-10-25 13:52:55 +00:00
|
|
|
.AddHttpContextAccessor()
|
|
|
|
|
|
2023-01-24 20:48:05 +00:00
|
|
|
// Add content parsers.
|
2023-01-16 18:42:45 +00:00
|
|
|
.AddSingleton<IHttpContentParser, StringHttpContentParser>()
|
|
|
|
|
.AddSingleton<IHttpContentParser, JsonHttpContentParser>()
|
|
|
|
|
.AddSingleton<IHttpContentParser, XmlHttpContentParser>()
|
|
|
|
|
|
2023-01-24 20:48:05 +00:00
|
|
|
// Add HTTP content factories.
|
|
|
|
|
.AddSingleton<IHttpContentFactory, TextContentFactory>()
|
|
|
|
|
.AddSingleton<IHttpContentFactory, JsonContentFactory>()
|
|
|
|
|
.AddSingleton<IHttpContentFactory, FormUrlEncodedHttpContentFactory>()
|
2022-10-25 13:52:55 +00:00
|
|
|
;
|
2022-05-26 10:47:31 +00:00
|
|
|
}
|
|
|
|
|
}
|