Skip to content

Commit 2a8c927

Browse files
committed
Testing the LINQ filter evaluator
1 parent fb1369f commit 2a8c927

File tree

13 files changed

+238
-16
lines changed

13 files changed

+238
-16
lines changed

Deveel.Webhooks.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Deveel.Webhooks.Signers", "
3737
EndProject
3838
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Deveel.Webhooks.MongoDb.XUnit", "test\Deveel.Webhooks.MongoDb.XUnit\Deveel.Webhooks.MongoDb.XUnit.csproj", "{D61BAD7A-7B8A-4413-A0D0-8F52EE81A0B0}"
3939
EndProject
40+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Deveel.Webhooks.DynamicLinq.XUnit", "test\Deveel.Webhooks.DynamicLinq.XUnit\Deveel.Webhooks.DynamicLinq.XUnit.csproj", "{A220DFA9-28DA-434F-AF49-7E237B1DEF30}"
41+
EndProject
4042
Global
4143
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4244
Debug|Any CPU = Debug|Any CPU
@@ -103,6 +105,10 @@ Global
103105
{D61BAD7A-7B8A-4413-A0D0-8F52EE81A0B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
104106
{D61BAD7A-7B8A-4413-A0D0-8F52EE81A0B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
105107
{D61BAD7A-7B8A-4413-A0D0-8F52EE81A0B0}.Release|Any CPU.Build.0 = Release|Any CPU
108+
{A220DFA9-28DA-434F-AF49-7E237B1DEF30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
109+
{A220DFA9-28DA-434F-AF49-7E237B1DEF30}.Debug|Any CPU.Build.0 = Debug|Any CPU
110+
{A220DFA9-28DA-434F-AF49-7E237B1DEF30}.Release|Any CPU.ActiveCfg = Release|Any CPU
111+
{A220DFA9-28DA-434F-AF49-7E237B1DEF30}.Release|Any CPU.Build.0 = Release|Any CPU
106112
EndGlobalSection
107113
GlobalSection(SolutionProperties) = preSolution
108114
HideSolutionNode = FALSE
@@ -123,6 +129,7 @@ Global
123129
{6A04D831-545F-4EFA-B1F7-2BD0B0A58DF6} = {57F6404B-1FCC-473F-A189-ABC9D640CC0E}
124130
{6D6057E9-117F-4B13-80E7-4C18B3905AEB} = {57F6404B-1FCC-473F-A189-ABC9D640CC0E}
125131
{D61BAD7A-7B8A-4413-A0D0-8F52EE81A0B0} = {07F23FF6-2FE1-4072-BF37-9238E3750AA1}
132+
{A220DFA9-28DA-434F-AF49-7E237B1DEF30} = {07F23FF6-2FE1-4072-BF37-9238E3750AA1}
126133
EndGlobalSection
127134
GlobalSection(ExtensibilityGlobals) = postSolution
128135
SolutionGuid = {E682A9F5-43D7-4D4C-82EA-953545B8F4DE}

README.md

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ The libraries currently provided by the framework are the following:
2121

2222
| Library | Description | NuGet | GitHub (prerelease) |
2323
| ----------------------------------------| ---------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------- |---------------------|
24-
| **Deveel.Webhooks** | Provides the capabilities to handle webhook subscriptions and notifications | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks?label=Deveel.Webhooks&logo=nuget) | [![GitHub](https://img.shields.io/static/v1?label=Deveel.Webhooks&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks) |
25-
| **Deveel.Webhooks.Sender** | Provides services and functions to send webhooks to remote endpoints | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks.Sender?label=Deveel.Webhooks.Sender&logo=nuget) | [![GitHub](https://img.shields.io/static/v1?label=Deveel.Webhooks.Sender&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks.Service) |
26-
| **Deveel.Webhooks.Service** | Implements the functions to manage and resolve webhook subscriptions | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks.Service?label=Deveel.Webhooks.Service&logo=nuget)| [![GitHub](https://img.shields.io/static/v1?label=Deveel.Webhooks.Service&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks.Service) |
27-
| **Deveel.Webhooks.MongoDb** | An implementation of the webhoom management data layer that is backed by [MongoDB](https://mongodb.com) databases | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks.MongoDb?label=Deveel.Webhooks.MongoDb&logo=nuget) | [![GitHub](https://img.shields.io/static/v1?label=Deveel.Webhooks.MongoDb&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks.MongoDb) |
28-
| **Deveel.Webhooks.DynamicLinq** | The webhook subscription filtering engine that uses the [Dynamic LINQ](https://dynamic-linq.net/) expressions | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks.DynamicLinq?label=Deveel.Webhooks.DynamicLinq&logo=nuget) | [![GitHub](https://img.shields.io/static/v1?label=Deveel.Webhooks.DynamicLinq&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks.DynamicLinq) |
29-
| **Deveel.Webhooks.Receiver.AspNetCore** | An implementation of the webhook receiver that is backed by [ASP.NET Core](https://dotnet.microsoft.com/apps/aspnet) | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks?label=Deveel.Webhooks.Receiver.AspNetCore&logo=nuget) | [![GitHub](https://img.shields.io/static/v1?label=Deveel.Webhooks.Receiver.AspNetCore&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks.Receiver.AspNetCore) |
24+
| **Deveel.Webhooks** | Provides the capabilities to handle webhook subscriptions and notifications | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks?label=NuGet&logo=nuget) | [![GitHub](https://img.shields.io/static/v1?label=GitHub&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks) |
25+
| **Deveel.Webhooks.Sender** | Provides services and functions to send webhooks to remote endpoints | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks.Sender?label=NuGet&logo=nuget) | [![GitHub](https://img.shields.io/static/v1?label=GitHub&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks.Sender) |
26+
| **Deveel.Webhooks.Service** | Implements the functions to manage and resolve webhook subscriptions | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks.Service?label=NuGet&logo=nuget)| [![GitHub](https://img.shields.io/static/v1?label=NuGet&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks.Service) |
27+
| **Deveel.Webhooks.MongoDb** | An implementation of the webhoom management data layer that is backed by [MongoDB](https://mongodb.com) databases | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks.MongoDb?label=NuGet&logo=nuget) | [![GitHub](https://img.shields.io/static/v1?label=GitHub&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks.MongoDb) |
28+
| **Deveel.Webhooks.DynamicLinq** | The webhook subscription filtering engine that uses the [Dynamic LINQ](https://dynamic-linq.net/) expressions | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks.DynamicLinq?label=NuGet&logo=nuget) | [![GitHub](https://img.shields.io/static/v1?label=GitHub&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks.DynamicLinq) |
29+
| **Deveel.Webhooks.Receiver.AspNetCore** | An implementation of the webhook receiver that is backed by [ASP.NET Core](https://dotnet.microsoft.com/apps/aspnet) | ![Nuget](https://img.shields.io/nuget/dt/Deveel.Webhooks?label=NuGet&logo=nuget) | [![GitHub](https://img.shields.io/static/v1?label=GitHub&message=Pre-Release&color=yellow&logo=github)](https://github.com/deveel/deveel.webhooks/pkgs/nuget/Deveel.Webhooks.Receiver.AspNetCore) |
3030

3131
You can obtain the stable versions of these libraries from the [NuGet Official](https://nuget.org) channel.
3232

@@ -46,6 +46,74 @@ While working on a .NET Core 3.1/.NET 5 *aaS (_as-a-Service_) project that funct
4646

4747
We would like to help you getting started with this framework and to eventually extend it: please refer to the [Documentation](docs/README.md) section that we have produced for you.
4848

49+
The easiest way to get started is to follow the [Quick Start](docs/QUICKSTART.md) guide, but you can also refer to the [Frequently Asked Questions](docs/FAQS.md) section to get answers to the most common questions.
50+
51+
## A Simple Example
52+
53+
The following example shows how to create a webhook subscription, and how to send a notification to the subscribed endpoint:
54+
55+
```csharp
56+
using Microsoft.AspNetCore.Builder;
57+
58+
using Deveel.Webhooks;
59+
60+
namespace Example {
61+
public class Program {
62+
public static void Main(string[] args) {
63+
var builder = WebApplication.CreateBuilder(args);
64+
65+
// configure your other services ...
66+
67+
// ... and then configure the webhooks
68+
builder.Services.AddWebhooks(webhooks => {
69+
webhooks.AddSubscriptions<MongoWebhookSubscription>(subs => {
70+
subs.UseMongoDb("mongodb://localhost:27017")
71+
.UseSubscriptionResolver();
72+
});
73+
74+
webhooks.AddNotifier<MyWebhook>(notifier => {
75+
notifier.UseSender(sender => {
76+
sender.Configure(options => {
77+
options.Timeout = TimeSpan.FromSeconds(30);
78+
});
79+
});
80+
});
81+
});
82+
83+
var app = builder.Build();
84+
85+
// configure your other middlewares ...
86+
87+
// ... and then configure the webhooks
88+
// to manually send the webhooks ...
89+
app.MapPost("/webhooks", async (HttpContext context,
90+
[FromServices] IWebhookSender<MyWebhook> sender, [FromBody] MyWebhookModel webhook) => {
91+
var destination = webhook.Destination.ToWebhookDestination();
92+
var result = await sender.SendAsync(destination webhook, context.HttpContext.RequestAborted);
93+
94+
// you can log the result of the delivery ...
95+
96+
return Results.Ok();
97+
});
98+
99+
// ... or notify the webhooks automatically from subscriptions
100+
app.MapPost("/webhooks/notify", async (HttpContext context,
101+
[FromServices] IWebhookNotifier<MyWebhook> notifier, [FromBody] MyEventModel eventModel) => {
102+
var eventInfo = eventModel.AsEventInfo();
103+
var result = await notifier.NotifyAsync(eventInfo, context.HttpContext.RequestAborted);
104+
105+
// you can log the result of the notification to all receivers ...
106+
return Results.Ok();
107+
});
108+
109+
app.Run();
110+
}
111+
}
112+
}
113+
```
114+
115+
More examples are available in the [Examples](examples/README.md) section.
116+
49117
## Contribute
50118

51119
Contributions to open-source projects, like **Deveel Webhooks**, is generally driven by interest in using the product and services, if they would respect some of the expectations we have to its functions.

examples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Examples
2+
3+
_Coming Soon_

src/Deveel.Webhooks.DynamicLinq/Webhooks/LinqWebhookFilterEvaluator.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ public LinqWebhookFilterEvaluator(IWebhookJsonSerializer<TWebhook> jsonSerialize
2828
string IWebhookFilterEvaluator<TWebhook>.Format => "linq";
2929

3030
private Func<object, bool> Compile(Type objType, string filter) {
31-
if (filter == null)
32-
throw new ArgumentNullException(nameof(filter));
33-
3431
if (!filterCache.TryGetValue(filter, out var compiled)) {
3532
var config = ParsingConfig.Default;
3633

@@ -61,7 +58,7 @@ public async Task<bool> MatchesAsync(WebhookSubscriptionFilter filter, TWebhook
6158
throw new ArgumentNullException(nameof(webhook));
6259

6360
if (filter.FilterFormat != "linq")
64-
throw new ArgumentException($"Filter format '{filter.FilterFormat}' not supported by the DLINQ evaluator");
61+
throw new ArgumentException($"Filter format '{filter.FilterFormat}' not supported by the LINQ evaluator");
6562

6663
if (filter.IsWildcard)
6764
return true;
@@ -74,9 +71,6 @@ public async Task<bool> MatchesAsync(WebhookSubscriptionFilter filter, TWebhook
7471

7572
var evalFilter = Compile(obj.GetType(), filter.Filters);
7673

77-
if (evalFilter == null)
78-
return false;
79-
8074
var result = evalFilter(obj);
8175

8276
return result;

src/Deveel.Webhooks.Sender/Webhooks/TypeCreator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace Deveel.Webhooks {
1919
static class TypeCreator {
20-
private static int anonymousCounter = -1;
20+
private static int anonymousCounter = 0;
2121

2222
private static void CreateProperty(TypeBuilder typeBuilder, string propertyName, Type propertyType) {
2323
var fieldBuilder = typeBuilder.DefineField($"_{propertyName}", propertyType, FieldAttributes.Private);
@@ -60,7 +60,7 @@ private static void CreateProperty(TypeBuilder typeBuilder, string propertyName,
6060
TypeAttributes.AutoLayout |
6161
TypeAttributes.BeforeFieldInit;
6262

63-
var typeBuilder = moduleBuilder.DefineType($"{Namespace}.__Anonynous_{anonymousCounter++}", typeAtts);
63+
var typeBuilder = moduleBuilder.DefineType($"{Namespace}._Anonynous_{anonymousCounter++}", typeAtts);
6464

6565
foreach (var propertyType in propertyTypes) {
6666
string propertyName = propertyType.Key;

src/Deveel.Webhooks/Deveel.Webhooks.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Deveel.Webhooks/Webhooks/IWebhookFilterEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ public interface IWebhookFilterEvaluator<TWebhook> where TWebhook : class {
4040
/// match the conditions given against the provided webhook instance,
4141
/// <strong>false</strong> otherwise.
4242
/// </returns>
43-
Task<bool> MatchesAsync(WebhookSubscriptionFilter filterRequest, TWebhook webhook, CancellationToken cancellationToken);
43+
Task<bool> MatchesAsync(WebhookSubscriptionFilter filterRequest, TWebhook webhook, CancellationToken cancellationToken = default);
4444
}
4545
}

src/Deveel.Webhooks/Webhooks/WebhookSubscriptionFilter.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,21 @@ public void AddFilter(string expression) {
107107
filters.Add(expression);
108108
}
109109
}
110+
111+
/// <summary>
112+
/// Creates a new instance of the <see cref="WebhookSubscriptionFilter"/> class
113+
/// </summary>
114+
/// <param name="format">
115+
/// The format of the expression.
116+
/// </param>
117+
/// <param name="filters">
118+
/// The list of filter expressions.
119+
/// </param>
120+
/// <returns>
121+
/// Returns a new instance of the <see cref="WebhookSubscriptionFilter"/> class.
122+
/// </returns>
123+
public static WebhookSubscriptionFilter Create(string format, params string[] filters) {
124+
return new WebhookSubscriptionFilter(format, filters);
125+
}
110126
}
111127
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<RootNamespace>Deveel</RootNamespace>
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
14+
<PackageReference Include="xunit" Version="2.4.2" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
<PackageReference Include="coverlet.collector" Version="3.2.0">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\..\src\Deveel.Webhooks.DynamicLinq\Deveel.Webhooks.DynamicLinq.csproj" />
27+
</ItemGroup>
28+
29+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
[assembly: ExcludeFromCodeCoverage]

0 commit comments

Comments
 (0)