Skip to content

Commit

Permalink
Add JSON editor UI hint to HTTP activities
Browse files Browse the repository at this point in the history
This commit introduces a JSON editor UI hint to HTTP activities within Elsa Workflow. It includes creating new classes for a JSON editor options provider and a handler for this UI hint. Also, these additions are registered within the Elsa Workflow. The hint is applied to the 'RequestHeaders' and 'ResponseHeaders' input fields found in HTTP-related activity classes.
  • Loading branch information
sfmskywalker committed Dec 28, 2023
1 parent 2a5cf04 commit 5202b39
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/modules/Elsa.Http/Activities/SendHttpRequestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ protected SendHttpRequestBase(string? source = default, int? line = default) : b
/// <summary>
/// The headers to send along with the request.
/// </summary>
[Input(Description = "The headers to send along with the request.", Category = "Advanced")]
[Input(
Description = "The headers to send along with the request.",
UIHint = InputUIHints.JsonEditor,
Category = "Advanced"
)]
public Input<HttpHeaders?> RequestHeaders { get; set; } = new(new HttpHeaders());

/// <summary>
Expand Down Expand Up @@ -161,7 +165,7 @@ private HttpRequestMessage PrepareRequest(ActivityExecutionContext context)
var addAuthorizationWithoutValidation = DisableAuthorizationHeaderValidation.GetOrDefault(context);

if (!string.IsNullOrWhiteSpace(authorization))
if(addAuthorizationWithoutValidation)
if (addAuthorizationWithoutValidation)
request.Headers.TryAddWithoutValidation("Authorization", authorization);
else
request.Headers.Authorization = AuthenticationHeaderValue.Parse(authorization);
Expand Down
6 changes: 5 additions & 1 deletion src/modules/Elsa.Http/Activities/WriteHttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public WriteHttpResponse([CallerFilePath] string? source = default, [CallerLineN
/// <summary>
/// The headers to return along with the response.
/// </summary>
[Input(Description = "The headers to send along with the response.", Category = "Advanced")]
[Input(
Description = "The headers to send along with the response.",
UIHint = InputUIHints.JsonEditor,
Category = "Advanced"
)]
public Input<HttpHeaders?> ResponseHeaders { get; set; } = new(new HttpHeaders());

/// <inheritdoc />
Expand Down
2 changes: 2 additions & 0 deletions src/modules/Elsa.Workflows.Core/Features/WorkflowsFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Elsa.Workflows.Services;
using Elsa.Workflows.UIHints.CheckList;
using Elsa.Workflows.UIHints.Dropdown;
using Elsa.Workflows.UIHints.JsonEditor;
using Microsoft.Extensions.DependencyInjection;

namespace Elsa.Workflows.Features;
Expand Down Expand Up @@ -181,6 +182,7 @@ private void AddElsaCore(IServiceCollection services)
// UI hints.
.AddSingleton<IUIHintHandler, DropDownUIHintHandler>()
.AddSingleton<IUIHintHandler, CheckListUIHintHandler>()
.AddSingleton<IUIHintHandler, JsonEditorUIHintHandler>()

// Logging
.AddLogging();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class StaticDropDownOptionsProvider : IPropertyUIHandler
var wrappedPropertyType = propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Input<>)
? propertyInfo.PropertyType.GetGenericArguments()[0]
: propertyInfo.PropertyType;
if (!wrappedPropertyType.IsEnum)

if (!wrappedPropertyType.IsEnum)
return new(dictionary);

var enumValues = Enum.GetValues(wrappedPropertyType).Cast<object>().ToList();
var enumSelectListItems = enumValues.Select(x => new SelectListItem(x.ToString()!, x.ToString()!)).ToList();
var enumProps = new DropDownProps
Expand All @@ -36,7 +36,6 @@ public class StaticDropDownOptionsProvider : IPropertyUIHandler

dictionary[InputUIHints.DropDown] = enumProps;
return new(dictionary);

}

var selectListItems = (inputOptions as ICollection<string>)?.Select(x => new SelectListItem(x, x)).ToList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Reflection;
using Elsa.Workflows.UIHints.CodeEditor;

// ReSharper disable once CheckNamespace
namespace Elsa.CSharp.Activities;

internal class JsonCodeOptionsProvider : CodeEditorOptionsProviderBase
{
protected override string GetLanguage(PropertyInfo propertyInfo, object? context) => "json";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Reflection;
using Elsa.CSharp.Activities;
using Elsa.Workflows.Contracts;

namespace Elsa.Workflows.UIHints.JsonEditor;

/// <inheritdoc />
public class JsonEditorUIHintHandler : IUIHintHandler
{
/// <inheritdoc />
public string UIHint => InputUIHints.JsonEditor;

/// <inheritdoc />
public ValueTask<IEnumerable<Type>> GetPropertyUIHandlersAsync(PropertyInfo propertyInfo, CancellationToken cancellationToken)
{
return new(new[] { typeof(JsonCodeOptionsProvider) });
}
}

0 comments on commit 5202b39

Please sign in to comment.