Exemple #1
0
        public async Task <ActionResult <WebhookDefinition> > Handle([FromBody] SaveWebhookDefinitionRequest request, [FromRoute] ApiVersion apiVersion, CancellationToken cancellationToken)
        {
            var webhookId         = request.Id;
            var webhookDefinition = !string.IsNullOrWhiteSpace(webhookId) ? await _store.FindAsync(new EntityIdSpecification <WebhookDefinition>(webhookId), cancellationToken) : default;

            if (webhookDefinition == null)
            {
                webhookDefinition = new WebhookDefinition
                {
                    Id = !string.IsNullOrWhiteSpace(webhookId) ? webhookId : _idGenerator.Generate(),
                }
            }
            ;

            webhookDefinition.Name            = request.Name.Trim();
            webhookDefinition.Path            = request.Path.Trim();
            webhookDefinition.Description     = request.Description?.Trim();
            webhookDefinition.PayloadTypeName = request.PayloadTypeName?.Trim();
            webhookDefinition.IsEnabled       = request.IsEnabled;

            await _store.SaveAsync(webhookDefinition, cancellationToken);

            return(CreatedAtAction("Handle", "Get", new { id = webhookDefinition.Id, apiVersion = apiVersion.ToString() }, webhookDefinition));
        }
    }
        public async Task AddAsync(WebhookDefinition entity, CancellationToken cancellationToken = default)
        {
            await _mediator.Publish(new WebhookDefinitionSaving(entity), cancellationToken);

            await _store.AddAsync(entity, cancellationToken);

            await _mediator.Publish(new WebhookDefinitionSaved(entity), cancellationToken);
        }
Exemple #3
0
        private WebhookDefinition Initialize(WebhookDefinition webhookDefinition)
        {
            if (string.IsNullOrWhiteSpace(webhookDefinition.Id))
            {
                webhookDefinition.Id = _idGenerator.Generate();
            }

            return(webhookDefinition);
        }
Exemple #4
0
 public WebhookDefinitionSurrogate(WebhookDefinition value)
 {
     Id              = value.Id;
     TenantId        = value.TenantId;
     Name            = value.Name;
     Path            = value.Path;
     Description     = value.Description;
     PayloadTypeName = value.PayloadTypeName;
 }
Exemple #5
0
        private ActivityType CreateWebhookActivityType(WebhookDefinition webhook)
        {
            var activityTypeName    = webhook.Name.EndsWith(WebhooksActivityTypeSuffix) ? webhook.Name : $"{webhook.Name}{WebhooksActivityTypeSuffix}";
            var activityDisplayName = activityTypeName.Humanize();

            var descriptor = new ActivityDescriptor
            {
                Type            = activityTypeName,
                DisplayName     = activityDisplayName,
                Category        = WebhooksActivityCategory,
                Outcomes        = new[] { OutcomeNames.Done },
                Traits          = ActivityTraits.Trigger,
                InputProperties = new[]
        public async Task <bool> SaveAsync(WebhookDefinition model, bool?references = true, CancellationToken token = default)
        {
            var persisted = false;

            using (var scope = TransactionScopeOption.Required.AsTransactionScopeFlow())
            {
                var result = await collection.ReplaceOneAsync(x => x.Id == model.Id, model, new ReplaceOptions { IsUpsert = true });

                persisted = result.IsAcknowledged;
                scope.Complete();
            }
            return(persisted);
        }
Exemple #7
0
        public bool Save(WebhookDefinition model, bool?references = true)
        {
            var inserted = false;

            using (var scope = TransactionScopeOption.Required.AsTransactionScope())
            {
                using (var db = factory.OpenDbConnection())
                {
                    inserted = db.Save(model, references.Value);
                }
                scope.Complete();
            }
            return(inserted);
        }
Exemple #8
0
        public async Task <bool> SaveAsync(WebhookDefinition model, bool?references = true, CancellationToken token = default)
        {
            var inserted = false;

            using (var scope = TransactionScopeOption.Required.AsTransactionScopeFlow())
            {
                using (var db = factory.OpenDbConnection())
                {
                    inserted = await db.SaveAsync(model, references ?? false);
                }
                scope.Complete();
            }
            return(inserted);
        }
        public bool Save(WebhookDefinition model, bool?references = true)
        {
            var persisted = false;

            using (var scope = TransactionScopeOption.Required.AsTransactionScope())
            {
                var result = collection.ReplaceOne(x => x.Id == model.Id, model, new ReplaceOptions {
                    IsUpsert = true
                });
                persisted = result.IsAcknowledged;
                scope.Complete();
            }
            return(persisted);
        }
Exemple #10
0
        private ActivityType CreateWebhookActivityType(WebhookDefinition webhook)
        {
            var typeName    = webhook.Name;
            var displayName = webhook.Name;

            var descriptor = new ActivityDescriptor
            {
                Type            = typeName,
                DisplayName     = displayName,
                Category        = WebhookActivityCategory,
                Outcomes        = new[] { OutcomeNames.Done },
                Traits          = ActivityTraits.Trigger,
                InputProperties = new[]
                {
                    new ActivityInputDescriptor(
                        nameof(HttpEndpoint.Methods),
                        typeof(HashSet <string>),
                        ActivityInputUIHints.Dropdown,
                        "Request Method",
                        "Specify what request method this webhook should handle. Leave empty to handle both GET and POST requests",
                        new[] { "", "GET", "POST" },
                        "Webhooks",
                        0,
                        "POST",
                        SyntaxNames.Literal,
                        new[] { SyntaxNames.JavaScript, SyntaxNames.Liquid })
                }
            };

            async ValueTask <IActivity> ActivateActivityAsync(ActivityExecutionContext context)
            {
                var activity = await _activityActivator.ActivateActivityAsync <HttpEndpoint>(context);

                activity.Path        = webhook.Path;
                activity.ReadContent = true;
                activity.TargetType  = webhook.PayloadTypeName is not null and not "" ? Type.GetType(webhook.PayloadTypeName) : throw new Exception($"Type {webhook.PayloadTypeName} not found");
                return(activity);
            }

            return(new ActivityType
            {
                TypeName = webhook.Name,
                Type = typeof(HttpEndpoint),
                Description = webhook.Description is not null and not "" ? webhook.Description : $"A webhook at {webhook.Path}",
                DisplayName = webhook.Name,
                ActivateAsync = ActivateActivityAsync,
                Describe = () => descriptor
            });
Exemple #11
0
        private static bool ShouldSend(WebhookDefinition webhook, bool isUpdate, string?name)
        {
            if (webhook.SendAlways)
            {
                return(true);
            }

            if (isUpdate)
            {
                return(webhook.SendConfirm);
            }
            else
            {
                return(!string.IsNullOrWhiteSpace(name) && string.Equals(name, webhook.Name, StringComparison.Ordinal));
            }
        }
        public async Task PublishAsync(WebhookDefinition webhook, object data)
        {
            var webhookSubscriptions = await _webhookSubscriptionBusiness.GetByWebhookIdAsync(webhook.Id);

            if (webhookSubscriptions == null || (webhookSubscriptions != null && webhookSubscriptions.Count == 0))
            {
                return;
            }

            var webhookEvent = await InsertWebhookEventAsync(webhook.Id, data);

            foreach (var webhookSubscription in webhookSubscriptions)
            {
                await _webhookSender.SendAsync(new WebhookSenderArgs
                {
                    WebhookEventId        = webhookEvent.Id,
                    Data                  = webhookEvent.Data,
                    WebhookName           = webhook.Name,
                    WebhookSubscriptionId = webhookSubscription.Id,
                    Secret                = webhookSubscription.Secret,
                    WebhookUri            = webhookSubscription.WebhookUri
                });
            }
        }
 public Task RestoreAsync(WebhookDefinition model, bool?references = null)
 {
     model.IsDeleted = false;
     return(SaveAsync(model, references));
 }
Exemple #14
0
 public async Task AddAsync(WebhookDefinition entity, CancellationToken cancellationToken = default)
 {
     entity = Initialize(entity);
     await _store.AddAsync(entity, cancellationToken);
 }
 public WebhookDefinitionSaving(WebhookDefinition webhookDefinition) : base(webhookDefinition)
 {
 }
 public bool Erase(WebhookDefinition model)
 {
     return(EraseByKey(model.Id));
 }
Exemple #17
0
 public Task DeleteAsync(WebhookDefinition entity, CancellationToken cancellationToken) => _store.DeleteAsync(entity, cancellationToken);
 public void Trash(WebhookDefinition model, bool?references = null)
 {
     model.IsDeleted = true;
     Save(model, references);
 }
 public async Task TrashAsync(WebhookDefinition model, bool?references = null)
 {
     model.IsDeleted = true;
     await SaveAsync(model, references);
 }
Exemple #20
0
 public Task TrashAsync(WebhookDefinition model, bool?references = null)
 {
     model.IsDeleted = true;
     return(SaveAsync(model, references ?? false));
 }
Exemple #21
0
 public async Task UpdateAsync(WebhookDefinition entity, CancellationToken cancellationToken)
 {
     entity = Initialize(entity);
     await _store.UpdateAsync(entity, cancellationToken);
 }
Exemple #22
0
 public WebhookDefinitionDeleting(WebhookDefinition webhookDefinition) : base(webhookDefinition)
 {
 }
 public WebhookDefinitionNotification(WebhookDefinition webhookDefinition) => WebhookDefinition = webhookDefinition;
 public Task <bool> EraseAsync(WebhookDefinition model, CancellationToken token = default)
 {
     return(EraseByKeyAsync(model.Id, token));
 }
 public void Restore(WebhookDefinition model, bool?references = null)
 {
     model.IsDeleted = false;
     Save(model, references);
 }
Exemple #26
0
 public bool Erase(WebhookDefinition model) => EraseByKey(model.Id);