From bc986ec003f1f33cfe5df11d57400bbc5b381657 Mon Sep 17 00:00:00 2001 From: Konstantin Mikhailyuk Date: Sat, 20 Mar 2021 08:45:08 +0200 Subject: [PATCH] #667 - Move webhook client api to ElsaClient (#774) * WIP #667 - Webhook activity project * WIP #667 - Webhook definition with InMemoryStore * WIP #667 - Add Webhook activity provider * Prevent part assembly generation See https://github.com/dotnet/aspnetcore/issues/24171 * Make optional args optional * Small changes * Move Refit client api to ElsaClient Co-authored-by: Sipke Schoorstra --- .../WebhookActivityTypeProvider.cs | 1 - .../Extensions/ElsaOptionsExtensions.cs | 4 +- .../Models/WebhookDefinition.cs | 17 ++++---- src/clients/Elsa.Client/ElsaClient.cs | 1 - .../Extensions/ServiceCollectionExtensions.cs | 5 ++- src/clients/Elsa.Client/IElsaClient.cs | 1 - .../Services/IWebhookDefinitionsApi.cs | 17 ++++++++ .../Models/GetWebhookDefinitionByIdRequest.cs | 1 + .../Services/IWebhookDefinitionService.cs | 1 + .../Surrogates/RuntimeTypeModelExtensions.cs | 6 ++- .../Surrogates/WebhookDefinitionSurrogate.cs | 42 +++++++++++++++++++ ...GrpcServicesServiceCollectionExtensions.cs | 7 +++- .../Extensions/ServiceCollectionExtensions.cs | 5 ++- 13 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 src/clients/Elsa.Client/Services/IWebhookDefinitionsApi.cs create mode 100644 src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/WebhookDefinitionSurrogate.cs diff --git a/src/activities/Elsa.Activities.Webhooks/ActivityTypes/WebhookActivityTypeProvider.cs b/src/activities/Elsa.Activities.Webhooks/ActivityTypes/WebhookActivityTypeProvider.cs index 49c8ee5d1..b92805132 100644 --- a/src/activities/Elsa.Activities.Webhooks/ActivityTypes/WebhookActivityTypeProvider.cs +++ b/src/activities/Elsa.Activities.Webhooks/ActivityTypes/WebhookActivityTypeProvider.cs @@ -11,7 +11,6 @@ using Elsa.Metadata; using Elsa.Persistence.Specifications; using Elsa.Services; using Elsa.Services.Models; -using Newtonsoft.Json.Linq; namespace Elsa.Activities.Webhooks.ActivityTypes { diff --git a/src/activities/Elsa.Activities.Webhooks/Extensions/ElsaOptionsExtensions.cs b/src/activities/Elsa.Activities.Webhooks/Extensions/ElsaOptionsExtensions.cs index 79ceb86f4..d3267ed1e 100644 --- a/src/activities/Elsa.Activities.Webhooks/Extensions/ElsaOptionsExtensions.cs +++ b/src/activities/Elsa.Activities.Webhooks/Extensions/ElsaOptionsExtensions.cs @@ -1,10 +1,10 @@ using System; using Elsa; -using Elsa.Activities.Webhooks; using Elsa.Activities.Webhooks.ActivityTypes; using Elsa.Activities.Webhooks.Options; using Elsa.Activities.Webhooks.Persistence; using Elsa.Activities.Webhooks.Persistence.Decorators; +using Elsa.ActivityProviders; // ReSharper disable once CheckNamespace namespace Microsoft.Extensions.DependencyInjection @@ -23,7 +23,7 @@ namespace Microsoft.Extensions.DependencyInjection // Services. services - .AddActivityTypeProvider() + .AddScoped() .AddScoped(options.WebhookDefinitionStoreFactory); services.Decorate(); diff --git a/src/activities/Elsa.Activities.Webhooks/Models/WebhookDefinition.cs b/src/activities/Elsa.Activities.Webhooks/Models/WebhookDefinition.cs index 41d1cc5a4..8fc02f1f8 100644 --- a/src/activities/Elsa.Activities.Webhooks/Models/WebhookDefinition.cs +++ b/src/activities/Elsa.Activities.Webhooks/Models/WebhookDefinition.cs @@ -1,19 +1,22 @@ using System; +using System.Runtime.Serialization; using Elsa.Models; -using Microsoft.AspNetCore.Http; namespace Elsa.Activities.Webhooks.Models { - public class WebhookDefinition : Entity, ITenantScope + [DataContract] + public class WebhookDefinition : IEntity, ITenantScope { - public string Name { get; set; } = default!; + [DataMember(Order = 1)] public string Id { get; set; } = default!; - public PathString Path { get; set; } + [DataMember(Order = 2)] public string? TenantId { get; set; } - public string? Description { get; set; } + [DataMember(Order = 3)] public string Name { get; set; } = default!; - public string? PayloadTypeName { get; set; } + [DataMember(Order = 4)] public string Path { get; set; } = default!; - public string? TenantId { get; set; } + [DataMember(Order = 5)] public string? Description { get; set; } + + [DataMember(Order = 6)] public string? PayloadTypeName { get; set; } } } diff --git a/src/clients/Elsa.Client/ElsaClient.cs b/src/clients/Elsa.Client/ElsaClient.cs index 6bbe84b41..79fbf746d 100644 --- a/src/clients/Elsa.Client/ElsaClient.cs +++ b/src/clients/Elsa.Client/ElsaClient.cs @@ -1,4 +1,3 @@ -using Elsa.Activities.Webhooks.Services; using Elsa.Client.Services; namespace Elsa.Client diff --git a/src/clients/Elsa.Client/Extensions/ServiceCollectionExtensions.cs b/src/clients/Elsa.Client/Extensions/ServiceCollectionExtensions.cs index 3796078b7..476da9cb5 100644 --- a/src/clients/Elsa.Client/Extensions/ServiceCollectionExtensions.cs +++ b/src/clients/Elsa.Client/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Net.Http; using Elsa.Client.Converters; using Elsa.Client.Options; @@ -27,7 +27,8 @@ namespace Elsa.Client.Extensions .AddApiClient(refitSettings, httpClientFactory) .AddApiClient(refitSettings, httpClientFactory) .AddApiClient(refitSettings, httpClientFactory) - .AddApiClient(refitSettings, httpClientFactory); + .AddApiClient(refitSettings, httpClientFactory) + .AddApiClient(refitSettings, httpClientFactory); return services .AddTransient(); diff --git a/src/clients/Elsa.Client/IElsaClient.cs b/src/clients/Elsa.Client/IElsaClient.cs index 47cb3dd45..3677f0d22 100644 --- a/src/clients/Elsa.Client/IElsaClient.cs +++ b/src/clients/Elsa.Client/IElsaClient.cs @@ -1,4 +1,3 @@ -using Elsa.Activities.Webhooks.Services; using Elsa.Client.Services; namespace Elsa.Client diff --git a/src/clients/Elsa.Client/Services/IWebhookDefinitionsApi.cs b/src/clients/Elsa.Client/Services/IWebhookDefinitionsApi.cs new file mode 100644 index 000000000..f44fe9b7d --- /dev/null +++ b/src/clients/Elsa.Client/Services/IWebhookDefinitionsApi.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Elsa.Activities.Webhooks.Models; +using Refit; + +namespace Elsa.Client.Services +{ + public interface IWebhookDefinitionsApi + { + [Get("/v1/webhook-definitions")] + Task> ListAsync(CancellationToken cancellationToken = default); + + [Get("/v1/webhook-definitions/{webhookDefinitionId}")] + Task GetByIdAsync(string webhookDefinitionId, CancellationToken cancellationToken = default); + } +} diff --git a/src/dashboards/blazor/ElsaDashboard.Shared/Rpc/WebhookDefinition/Models/GetWebhookDefinitionByIdRequest.cs b/src/dashboards/blazor/ElsaDashboard.Shared/Rpc/WebhookDefinition/Models/GetWebhookDefinitionByIdRequest.cs index d443cc9fb..3204cde92 100644 --- a/src/dashboards/blazor/ElsaDashboard.Shared/Rpc/WebhookDefinition/Models/GetWebhookDefinitionByIdRequest.cs +++ b/src/dashboards/blazor/ElsaDashboard.Shared/Rpc/WebhookDefinition/Models/GetWebhookDefinitionByIdRequest.cs @@ -1,5 +1,6 @@ using ProtoBuf; +// ReSharper disable once CheckNamespace namespace ElsaDashboard.Shared.Rpc { [ProtoContract] diff --git a/src/dashboards/blazor/ElsaDashboard.Shared/Rpc/WebhookDefinition/Services/IWebhookDefinitionService.cs b/src/dashboards/blazor/ElsaDashboard.Shared/Rpc/WebhookDefinition/Services/IWebhookDefinitionService.cs index 5009065f4..8e81e7e2f 100644 --- a/src/dashboards/blazor/ElsaDashboard.Shared/Rpc/WebhookDefinition/Services/IWebhookDefinitionService.cs +++ b/src/dashboards/blazor/ElsaDashboard.Shared/Rpc/WebhookDefinition/Services/IWebhookDefinitionService.cs @@ -4,6 +4,7 @@ using Elsa.Activities.Webhooks.Models; using ProtoBuf.Grpc; using ProtoBuf.Grpc.Configuration; +// ReSharper disable once CheckNamespace namespace ElsaDashboard.Shared.Rpc { [Service] diff --git a/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/RuntimeTypeModelExtensions.cs b/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/RuntimeTypeModelExtensions.cs index 3a635b489..14f98443b 100644 --- a/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/RuntimeTypeModelExtensions.cs +++ b/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/RuntimeTypeModelExtensions.cs @@ -1,5 +1,6 @@ -using System; +using System; using System.Collections.Generic; +using Elsa.Activities.Webhooks.Models; using Elsa.Client.Models; using ProtoBuf.Meta; @@ -16,7 +17,8 @@ namespace ElsaDashboard.Shared.Surrogates [typeof(VersionOptions)] = typeof(VersionOptionsSurrogate), [typeof(WorkflowInstance)] = typeof(WorkflowInstanceSurrogate), [typeof(WorkflowDefinition)] = typeof(WorkflowDefinitionSurrogate), - [typeof(WorkflowBlueprint)] = typeof(WorkflowBlueprintSurrogate) + [typeof(WorkflowBlueprint)] = typeof(WorkflowBlueprintSurrogate), + [typeof(WebhookDefinition)] = typeof(WebhookDefinitionSurrogate), }; /// diff --git a/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/WebhookDefinitionSurrogate.cs b/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/WebhookDefinitionSurrogate.cs new file mode 100644 index 000000000..b87cf35bd --- /dev/null +++ b/src/dashboards/blazor/ElsaDashboard.Shared/Surrogates/WebhookDefinitionSurrogate.cs @@ -0,0 +1,42 @@ +using Elsa.Activities.Webhooks.Models; +using ProtoBuf; + +namespace ElsaDashboard.Shared.Surrogates +{ + [ProtoContract(IgnoreListHandling = true)] + public class WebhookDefinitionSurrogate + { + public WebhookDefinitionSurrogate(WebhookDefinition value) + { + Id = value.Id; + TenantId = value.TenantId; + Name = value.Name; + Path = value.Path; + Description = value.Description; + PayloadTypeName = value.PayloadTypeName; + } + + [ProtoMember(1)] public string? Id { get; } + [ProtoMember(2)] public string? TenantId { get; } + [ProtoMember(3)] public string? Name { get; } + [ProtoMember(4)] public string? Path { get; } + [ProtoMember(5)] public string? Description { get; } + [ProtoMember(6)] public string? PayloadTypeName { get; } + + public static implicit operator WebhookDefinition?(WebhookDefinitionSurrogate? surrogate) => + surrogate != null + ? new WebhookDefinition + { + Id = surrogate.Id ?? string.Empty, + TenantId = surrogate.TenantId, + Name = surrogate.Name ?? string.Empty, + Path = surrogate.Path ?? string.Empty, + Description = surrogate.Description, + PayloadTypeName = surrogate.PayloadTypeName, + } + : default; + + public static implicit operator WebhookDefinitionSurrogate?(WebhookDefinition? source) => + source != null ? new WebhookDefinitionSurrogate(source) : default; + } +} \ No newline at end of file diff --git a/src/dashboards/blazor/ElsaDashboard.WebAssembly/Extensions/GrpcServicesServiceCollectionExtensions.cs b/src/dashboards/blazor/ElsaDashboard.WebAssembly/Extensions/GrpcServicesServiceCollectionExtensions.cs index af0d67f70..9c4930698 100644 --- a/src/dashboards/blazor/ElsaDashboard.WebAssembly/Extensions/GrpcServicesServiceCollectionExtensions.cs +++ b/src/dashboards/blazor/ElsaDashboard.WebAssembly/Extensions/GrpcServicesServiceCollectionExtensions.cs @@ -1,5 +1,6 @@ -using System; +using System; using System.Net.Http; +using System.Threading.Tasks; using ElsaDashboard.WebAssembly.Options; using Grpc.Core; using Grpc.Net.Client; @@ -26,13 +27,15 @@ namespace ElsaDashboard.WebAssembly.Extensions //var tokenManager = sp.GetRequiredService(); var credentials = CallCredentials.FromInterceptor( - async (context, metadata) => + (context, metadata) => { //var accessToken = await tokenManager.GetAccessTokenAsync(); var accessToken = ""; if (!string.IsNullOrEmpty(accessToken)) metadata.Add("Authorization", $"Bearer {accessToken}"); + + return Task.CompletedTask; }); var channel = GrpcChannel.ForAddress( diff --git a/src/dashboards/blazor/ElsaDashboard.WebAssembly/Extensions/ServiceCollectionExtensions.cs b/src/dashboards/blazor/ElsaDashboard.WebAssembly/Extensions/ServiceCollectionExtensions.cs index 7fd643cf4..740abbaac 100644 --- a/src/dashboards/blazor/ElsaDashboard.WebAssembly/Extensions/ServiceCollectionExtensions.cs +++ b/src/dashboards/blazor/ElsaDashboard.WebAssembly/Extensions/ServiceCollectionExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using ElsaDashboard.Shared.Rpc; using ElsaDashboard.Shared.Surrogates; using ElsaDashboard.WebAssembly.Options; @@ -22,7 +22,8 @@ namespace ElsaDashboard.WebAssembly.Extensions .AddGrpcClient() .AddGrpcClient() .AddGrpcClient() - .AddGrpcClient(); + .AddGrpcClient() + .AddGrpcClient(); } } } \ No newline at end of file