Skip to content

Commit

Permalink
WebhookRelayClient initial implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
damianh committed Apr 1, 2023
1 parent c63cfd7 commit 59ef0ba
Show file tree
Hide file tree
Showing 24 changed files with 707 additions and 16 deletions.
8 changes: 0 additions & 8 deletions Platform.sln.DotSettings

This file was deleted.

6 changes: 0 additions & 6 deletions Platform.v3.ncrunchsolution

This file was deleted.

32 changes: 32 additions & 0 deletions PlatformLibs.sln
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,26 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Webhooks", "libs\webhooks\s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Webhooks.Tests", "libs\webhooks\test\Webhooks.Tests\Webhooks.Tests.csproj", "{2BC20FAA-FC20-43C1-8866-C2CBC151A6B0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "webhook-relay", "webhook-relay", "{82B15B62-34E5-43A0-91C4-F3E023B1B288}"
ProjectSection(SolutionItems) = preProject
libs\webhook-relay\readme.md = libs\webhook-relay\readme.md
statemachine.md = statemachine.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{5B959414-6395-4637-BF86-22EE95D98723}"
ProjectSection(SolutionItems) = preProject
libs\webhook-relay\src\Directory.Build.props = libs\webhook-relay\src\Directory.Build.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{FB10AA73-8A64-45F4-B285-8C069A361CE3}"
ProjectSection(SolutionItems) = preProject
libs\webhook-relay\test\Directory.Build.props = libs\webhook-relay\test\Directory.Build.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebhookRelay", "libs\webhook-relay\src\WebhookRelay\WebhookRelay.csproj", "{B78CC157-95BB-44F3-80D7-CA4B391715A1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebhookRelay.Tests", "libs\webhook-relay\test\WebhookRelay.Tests\WebhookRelay.Tests.csproj", "{5D8CC3D8-C413-49AD-BCFA-0AC2B5F8CA98}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -382,6 +402,14 @@ Global
{2BC20FAA-FC20-43C1-8866-C2CBC151A6B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BC20FAA-FC20-43C1-8866-C2CBC151A6B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2BC20FAA-FC20-43C1-8866-C2CBC151A6B0}.Release|Any CPU.Build.0 = Release|Any CPU
{B78CC157-95BB-44F3-80D7-CA4B391715A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B78CC157-95BB-44F3-80D7-CA4B391715A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B78CC157-95BB-44F3-80D7-CA4B391715A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B78CC157-95BB-44F3-80D7-CA4B391715A1}.Release|Any CPU.Build.0 = Release|Any CPU
{5D8CC3D8-C413-49AD-BCFA-0AC2B5F8CA98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D8CC3D8-C413-49AD-BCFA-0AC2B5F8CA98}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D8CC3D8-C413-49AD-BCFA-0AC2B5F8CA98}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D8CC3D8-C413-49AD-BCFA-0AC2B5F8CA98}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -444,6 +472,10 @@ Global
{3ADF05C1-3CCA-4A10-9BB1-3E0D29E61DB2} = {1E1AD526-78B4-48E3-A3D2-087A230E3757}
{C19E94E1-1B0E-4D41-A09D-EBA4BF304B15} = {C4282CA9-C4A1-46BD-99FB-B1A1000D8637}
{2BC20FAA-FC20-43C1-8866-C2CBC151A6B0} = {3ADF05C1-3CCA-4A10-9BB1-3E0D29E61DB2}
{5B959414-6395-4637-BF86-22EE95D98723} = {82B15B62-34E5-43A0-91C4-F3E023B1B288}
{FB10AA73-8A64-45F4-B285-8C069A361CE3} = {82B15B62-34E5-43A0-91C4-F3E023B1B288}
{B78CC157-95BB-44F3-80D7-CA4B391715A1} = {5B959414-6395-4637-BF86-22EE95D98723}
{5D8CC3D8-C413-49AD-BCFA-0AC2B5F8CA98} = {FB10AA73-8A64-45F4-B285-8C069A361CE3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4306F166-DCCF-4316-88FC-9E339F1ABEFE}
Expand Down
3 changes: 2 additions & 1 deletion PlatformLibs.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Localstack/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pulumi/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Passphrase/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DB/@EntryIndexedValue">DB</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DB/@EntryIndexedValue">DB</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Webhook/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ProjectConfiguration>
<Settings>
<HiddenComponentWarnings>
<Value>AspNetTestHostCompatibility</Value>
</HiddenComponentWarnings>
</Settings>
</ProjectConfiguration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ProjectConfiguration>
<Settings>
<HiddenComponentWarnings>
<Value>AspNetTestHostCompatibility</Value>
</HiddenComponentWarnings>
</Settings>
</ProjectConfiguration>
4 changes: 3 additions & 1 deletion libs/test.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<LangVersion>preview</LangVersion>
<ImplicitUsings>true</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0'">
Expand All @@ -18,7 +20,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.3.0" />
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.*" />
<PackageReference Include="Shouldly" Version="4.*" />
<PackageReference Include="xunit.core" Version="2.*" />
Expand Down
25 changes: 25 additions & 0 deletions libs/webhook-relay/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Webhook Relay Client

A .NET client for the [Webhook Relay](https://webhookrelay.com) service.

It creates a websocket connection, authenticates and subscribes to the specified buckets.

Once the connection is established, it will start receiving messages and writing them to
a channel.

You can then read messages from the channel and process them however you like.

If the connection is lost, the client will attempt to reconnect and resubscribe.

When the client is disposed, it will close the connection and unsubscribe from the buckets.

It is your responsibility to handle any remaining messages in the channel.

## Installation

`dotnet add package Logicality.WebhookRelay.Client`

## Usage
```csharp

```
11 changes: 11 additions & 0 deletions libs/webhook-relay/src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>

<Import Project="../../src.props" />

<PropertyGroup>
<MinVerTagPrefix>webhook-relay-</MinVerTagPrefix>
<MinVerMinimumMajorMinor>0.1</MinVerMinimumMajorMinor>
</PropertyGroup>

</Project>
14 changes: 14 additions & 0 deletions libs/webhook-relay/src/WebhookRelay/ClientState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Logicality.WebhookRelay;

public enum ClientState
{
Disconnected,
Connecting,
Connected,
Authenticating,
Authenticated,
Subscribing,
Subscribed,
HandlingMessages,
Disposed,
}
16 changes: 16 additions & 0 deletions libs/webhook-relay/src/WebhookRelay/ClientTrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Logicality.WebhookRelay;

internal enum ClientTrigger
{
Connect,
ConnectCompleted,
ConnectFailed,
Authenticate,
AuthenticateSucceeded,
AuthenticationFailed,
Subscribe,
SubscribeCompleted,
HandleMessages,
ConnectionLost,
Dispose
}
26 changes: 26 additions & 0 deletions libs/webhook-relay/src/WebhookRelay/Metadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Runtime.Serialization;

namespace Logicality.WebhookRelay;

public class Metadata
{
public string Id { get; set; }

[DataMember(Name = "bucket_id")]
public string BucketId { get; set; }

[DataMember(Name = "bucket_name")]
public string BucketName { get; set; }

[DataMember(Name = "input_id")]
public string InputId { get; set; }

[DataMember(Name = "input_name")]
public string InputName { get; set; }

[DataMember(Name = "output_name")]
public string OutputName { get; set; }

[DataMember(Name = "output_destination")]
public string OutputDestination { get; set; }
}
13 changes: 13 additions & 0 deletions libs/webhook-relay/src/WebhookRelay/SnakeCaseNamingPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Text.Json;

namespace Logicality.WebhookRelay;

internal class SnakeCaseNamingPolicy : JsonNamingPolicy
{
public static SnakeCaseNamingPolicy Instance { get; } = new SnakeCaseNamingPolicy();

public override string ConvertName(string name)
{
return name.ToSnakeCase();
}
}
9 changes: 9 additions & 0 deletions libs/webhook-relay/src/WebhookRelay/StringUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Logicality.WebhookRelay;

internal static class StringUtils
{
public static string ToSnakeCase(this string str)
{
return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x : x.ToString())).ToLower();
}
}
14 changes: 14 additions & 0 deletions libs/webhook-relay/src/WebhookRelay/WebhookMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Logicality.WebhookRelay;

public class WebhookMessage
{
public Metadata Meta { get; set; }

public Dictionary<string, string[]> Headers { get; set; }

public string Query { get; set; }

public string Body { get; set; }

public string Method { get; set; }
}
15 changes: 15 additions & 0 deletions libs/webhook-relay/src/WebhookRelay/WebhookRelay.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="Stateless" Version="5.13.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit 59ef0ba

Please sign in to comment.