Skip to content

Commit

Permalink
Feature/async formatting2 (#528)
Browse files Browse the repository at this point in the history
* Make everything async ready.

* Testing the extension interface and extracting predefined rules.

* Resolve reference.

* Do not check for data.

* Timestamp transform.
  • Loading branch information
SebastianStehle authored May 30, 2020
1 parent 3774f26 commit 80bb064
Show file tree
Hide file tree
Showing 25 changed files with 798 additions and 437 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AlgoliaActionHandler(RuleEventFormatter formatter)
});
}

protected override (string Description, AlgoliaJob Data) CreateJob(EnrichedEvent @event, AlgoliaAction action)
protected override async Task<(string Description, AlgoliaJob Data)> CreateJobAsync(EnrichedEvent @event, AlgoliaAction action)
{
if (@event is EnrichedContentEvent contentEvent)
{
Expand All @@ -45,7 +45,7 @@ protected override (string Description, AlgoliaJob Data) CreateJob(EnrichedEvent
AppId = action.AppId,
ApiKey = action.ApiKey,
ContentId = contentId,
IndexName = Format(action.IndexName, @event)
IndexName = await FormatAsync(action.IndexName, @event)
};

if (contentEvent.Type == EnrichedContentEventType.Deleted ||
Expand All @@ -64,7 +64,8 @@ protected override (string Description, AlgoliaJob Data) CreateJob(EnrichedEvent

if (!string.IsNullOrEmpty(action.Document))
{
jsonString = Format(action.Document, @event)?.Trim();
jsonString = await FormatAsync(action.Document, @event);
jsonString = jsonString?.Trim();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public AzureQueueActionHandler(RuleEventFormatter formatter)
});
}

protected override (string Description, AzureQueueJob Data) CreateJob(EnrichedEvent @event, AzureQueueAction action)
protected override async Task<(string Description, AzureQueueJob Data)> CreateJobAsync(EnrichedEvent @event, AzureQueueAction action)
{
var queueName = Format(action.Queue, @event);
var queueName = await FormatAsync(action.Queue, @event);

var ruleDescription = $"Send AzureQueueJob to azure queue '{queueName}'";
var ruleJob = new AzureQueueJob
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public CommentActionHandler(RuleEventFormatter formatter, ICommandBus commandBus
this.commandBus = commandBus;
}

protected override (string Description, CommentJob Data) CreateJob(EnrichedEvent @event, CommentAction action)
protected override async Task<(string Description, CommentJob Data)> CreateJobAsync(EnrichedEvent @event, CommentAction action)
{
if (@event is EnrichedContentEvent contentEvent)
{
var text = Format(action.Text, @event);
var text = await FormatAsync(action.Text, @event);

var actor = contentEvent.Actor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public DiscourseActionHandler(RuleEventFormatter formatter, IHttpClientFactory h
this.httpClientFactory = httpClientFactory;
}

protected override (string Description, DiscourseJob Data) CreateJob(EnrichedEvent @event, DiscourseAction action)
protected override async Task<(string Description, DiscourseJob Data)> CreateJobAsync(EnrichedEvent @event, DiscourseAction action)
{
var url = $"{action.Url.ToString().TrimEnd('/')}/posts.json?api_key={action.ApiKey}&api_username={action.ApiUsername}";

var json = new Dictionary<string, object>
{
["title"] = Format(action.Title, @event)
["title"] = await FormatAsync(action.Title, @event)
};

if (action.Topic.HasValue)
Expand All @@ -47,7 +47,7 @@ protected override (string Description, DiscourseJob Data) CreateJob(EnrichedEve
json.Add("category", action.Category.Value);
}

json["raw"] = Format(action.Text, @event);
json["raw"] = await FormatAsync(action.Text, @event);

var requestBody = ToJson(json);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ElasticSearchActionHandler(RuleEventFormatter formatter)
});
}

protected override (string Description, ElasticSearchJob Data) CreateJob(EnrichedEvent @event, ElasticSearchAction action)
protected override async Task<(string Description, ElasticSearchJob Data)> CreateJobAsync(EnrichedEvent @event, ElasticSearchAction action)
{
if (@event is EnrichedContentEvent contentEvent)
{
Expand All @@ -46,7 +46,7 @@ protected override (string Description, ElasticSearchJob Data) CreateJob(Enriche

var ruleJob = new ElasticSearchJob
{
IndexName = Format(action.IndexName, @event),
IndexName = await FormatAsync(action.IndexName, @event),
ServerHost = action.Host.ToString(),
ServerUser = action.Username,
ServerPassword = action.Password,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ public EmailActionHandler(RuleEventFormatter formatter)
{
}

protected override (string Description, EmailJob Data) CreateJob(EnrichedEvent @event, EmailAction action)
protected override async Task<(string Description, EmailJob Data)> CreateJobAsync(EnrichedEvent @event, EmailAction action)
{
var ruleJob = new EmailJob
{
ServerHost = action.ServerHost,
ServerUseSsl = action.ServerUseSsl,
ServerPassword = action.ServerPassword,
ServerPort = action.ServerPort,
ServerUsername = Format(action.ServerUsername, @event),
MessageFrom = Format(action.MessageFrom, @event),
MessageTo = Format(action.MessageTo, @event),
MessageSubject = Format(action.MessageSubject, @event),
MessageBody = Format(action.MessageBody, @event)
ServerUsername = await FormatAsync(action.ServerUsername, @event),
MessageFrom = await FormatAsync(action.MessageFrom, @event),
MessageTo = await FormatAsync(action.MessageTo, @event),
MessageSubject = await FormatAsync(action.MessageSubject, @event),
MessageBody = await FormatAsync(action.MessageBody, @event)
};

var description = $"Send an email to {action.MessageTo}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public KafkaActionHandler(RuleEventFormatter formatter, KafkaProducer kafkaProdu
this.kafkaProducer = kafkaProducer;
}

protected override (string Description, KafkaJob Data) CreateJob(EnrichedEvent @event, KafkaAction action)
protected override async Task<(string Description, KafkaJob Data)> CreateJobAsync(EnrichedEvent @event, KafkaAction action)
{
string value, key;

if (!string.IsNullOrEmpty(action.Payload))
{
value = Format(action.Payload, @event);
value = await FormatAsync(action.Payload, @event);
}
else
{
Expand All @@ -42,7 +42,7 @@ protected override (string Description, KafkaJob Data) CreateJob(EnrichedEvent @

if (!string.IsNullOrEmpty(action.Key))
{
key = Format(action.Key, @event);
key = await FormatAsync(action.Key, @event);
}
else
{
Expand All @@ -54,14 +54,14 @@ protected override (string Description, KafkaJob Data) CreateJob(EnrichedEvent @
TopicName = action.TopicName,
MessageKey = key,
MessageValue = value,
Headers = ParseHeaders(action.Headers, @event),
Headers = await ParseHeadersAsync(action.Headers, @event),
Schema = action.Schema
};

return (Description, ruleJob);
}

private Dictionary<string, string> ParseHeaders(string headers, EnrichedEvent @event)
private async Task<Dictionary<string, string>> ParseHeadersAsync(string headers, EnrichedEvent @event)
{
if (string.IsNullOrWhiteSpace(headers))
{
Expand All @@ -81,7 +81,7 @@ private Dictionary<string, string> ParseHeaders(string headers, EnrichedEvent @e
var key = line.Substring(0, indexEqual);
var val = line.Substring(indexEqual + 1);

val = Format(val, @event);
val = await FormatAsync(val, @event);

headersDictionary[key] = val;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Avro;
using Avro.Generic;
using Confluent.Kafka;
using Confluent.SchemaRegistry;
using Confluent.SchemaRegistry.Serdes;
using GraphQL.Types;
using Microsoft.Extensions.Options;
using Squidex.Infrastructure.Json;
using Squidex.Infrastructure.Json.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ public MediumActionHandler(RuleEventFormatter formatter, IHttpClientFactory http
this.serializer = serializer;
}

protected override (string Description, MediumJob Data) CreateJob(EnrichedEvent @event, MediumAction action)
protected override async Task<(string Description, MediumJob Data)> CreateJobAsync(EnrichedEvent @event, MediumAction action)
{
var ruleJob = new MediumJob { AccessToken = action.AccessToken, PublicationId = action.PublicationId };

var requestBody = new
{
title = Format(action.Title, @event),
title = await FormatAsync(action.Title, @event),
contentFormat = action.IsHtml ? "html" : "markdown",
content = Format(action.Content, @event),
canonicalUrl = Format(action.CanonicalUrl, @event),
tags = ParseTags(@event, action)
content = await FormatAsync(action.Content, @event),
canonicalUrl = await FormatAsync(action.CanonicalUrl, @event),
tags = await ParseTagsAsync(@event, action)
};

ruleJob.RequestBody = ToJson(requestBody);

return (Description, ruleJob);
}

private string[] ParseTags(EnrichedEvent @event, MediumAction action)
private async Task<string[]> ParseTagsAsync(EnrichedEvent @event, MediumAction action)
{
if (string.IsNullOrWhiteSpace(action.Tags))
{
Expand All @@ -69,7 +69,7 @@ private string[] ParseTags(EnrichedEvent @event, MediumAction action)

try
{
var jsonTags = Format(action.Tags, @event);
var jsonTags = await FormatAsync(action.Tags, @event);

return serializer.Deserialize<string[]>(jsonTags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public NotificationActionHandler(RuleEventFormatter formatter, ICommandBus comma
{
if (@event is EnrichedUserEventBase userEvent)
{
var text = Format(action.Text, @event);
var text = await FormatAsync(action.Text, @event);

var actor = userEvent.Actor;

Expand All @@ -60,7 +60,7 @@ public NotificationActionHandler(RuleEventFormatter formatter, ICommandBus comma

if (!string.IsNullOrWhiteSpace(action.Url))
{
var url = Format(action.Url, @event);
var url = await FormatAsync(action.Url, @event);

if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out var uri))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public PrerenderActionHandler(RuleEventFormatter formatter, IHttpClientFactory h
this.httpClientFactory = httpClientFactory;
}

protected override (string Description, PrerenderJob Data) CreateJob(EnrichedEvent @event, PrerenderAction action)
protected override async Task<(string Description, PrerenderJob Data)> CreateJobAsync(EnrichedEvent @event, PrerenderAction action)
{
var url = Format(action.Url, @event);
var url = await FormatAsync(action.Url, @event);

var request = new { prerenderToken = action.Token, url };
var requestBody = ToJson(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public SlackActionHandler(RuleEventFormatter formatter, IHttpClientFactory httpC
this.httpClientFactory = httpClientFactory;
}

protected override (string Description, SlackJob Data) CreateJob(EnrichedEvent @event, SlackAction action)
protected override async Task<(string Description, SlackJob Data)> CreateJobAsync(EnrichedEvent @event, SlackAction action)
{
var body = new { text = Format(action.Text, @event) };
var body = new { text = await FormatAsync(action.Text, @event) };

var ruleJob = new SlackJob
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public TweetActionHandler(RuleEventFormatter formatter, IOptions<TwitterOptions>
this.twitterOptions = twitterOptions.Value;
}

protected override (string Description, TweetJob Data) CreateJob(EnrichedEvent @event, TweetAction action)
protected override async Task<(string Description, TweetJob Data)> CreateJobAsync(EnrichedEvent @event, TweetAction action)
{
var ruleJob = new TweetJob
{
Text = Format(action.Text, @event),
Text = await FormatAsync(action.Text, @event),
AccessToken = action.AccessToken,
AccessSecret = action.AccessSecret
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ public WebhookActionHandler(RuleEventFormatter formatter, IHttpClientFactory htt
this.httpClientFactory = httpClientFactory;
}

protected override (string Description, WebhookJob Data) CreateJob(EnrichedEvent @event, WebhookAction action)
protected override async Task<(string Description, WebhookJob Data)> CreateJobAsync(EnrichedEvent @event, WebhookAction action)
{
string requestBody;

if (!string.IsNullOrEmpty(action.Payload))
{
requestBody = Format(action.Payload, @event);
requestBody = await FormatAsync(action.Payload, @event);
}
else
{
requestBody = ToEnvelopeJson(@event);
}

var requestUrl = Format(action.Url, @event);
var requestUrl = await FormatAsync(action.Url, @event);

var ruleDescription = $"Send event to webhook '{requestUrl}'";
var ruleJob = new WebhookJob
{
RequestUrl = Format(action.Url.ToString(), @event),
RequestUrl = await FormatAsync(action.Url.ToString(), @event),
RequestSignature = $"{requestBody}{action.SharedSecret}".Sha256Base64(),
RequestBody = requestBody
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System.Threading.Tasks;
using Squidex.Domain.Apps.Core.Rules.EnrichedEvents;

namespace Squidex.Domain.Apps.Core.HandleRules
{
public interface IRuleEventFormatter
{
(bool Match, string?, int ReplacedLength) Format(EnrichedEvent @event, string text)
{
return default;
}

(bool Match, ValueTask<string?>) Format(EnrichedEvent @event, object value, string[] path)
{
return default;
}
}
}
Loading

0 comments on commit 80bb064

Please sign in to comment.