Skip to content

Commit

Permalink
Merge pull request #230 from contentful/feature/add-async-consumer
Browse files Browse the repository at this point in the history
Add async version of webhook consumer
  • Loading branch information
Roblinde authored Mar 13, 2021
2 parents c4b7282 + cca419c commit ea83856
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
49 changes: 49 additions & 0 deletions Contentful.AspNetCore.Tests/MiddleWare/WebhookMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,55 @@ public async Task RegisteredConsumerShouldDeserializePayloadCorrectly()
Assert.Equal("Hello", hookEntry.Fields.Title["en-US"].ToString());
}

[Fact]
public async Task RegisteredConsumerShouldDeserializePayloadCorrectlyAsync()
{
//Arrange
var consumerBuilder = new ConsumerBuilder();
var called = false;
var hookEntry = new Entry<dynamic>();
consumerBuilder.AddConsumer<Entry<dynamic>>("Test", SystemWebhookTopics.Entry, SystemWebhookActions.Publish,
async (s) => { called = true; hookEntry = s; return await Task.FromResult(new { Result = "Ok" }); });
var middleware = new WebhookMiddleware(null, consumerBuilder.Build());
var context = new DefaultHttpContext();

var entry = new Entry<dynamic>()
{
SystemProperties = new SystemProperties
{
Id = "123"
},
Fields = new
{
Title = new Dictionary<string, string>
{
{ "en-US", "Hello" },
{ "sv-SE", "Hallå" }
}
}
};

string jsonObject = JsonConvert.SerializeObject(entry);

var memoryStream = new MemoryStream();
var writer = new StreamWriter(memoryStream);
writer.Write(jsonObject);
writer.Flush();
memoryStream.Position = 0;

context.Request.Body = memoryStream;
context.Request.Headers.Add("X-Contentful-Topic", new Microsoft.Extensions.Primitives.StringValues("ContentManagement.Entry.publish"));
context.Request.Headers.Add("X-Contentful-Webhook-Name", new Microsoft.Extensions.Primitives.StringValues("Test"));
context.Request.ContentType = "application/vnd.contentful.management.v1+json";
//Act
await middleware.Invoke(context);

//Assert
Assert.True(called);
Assert.Equal("123", hookEntry.SystemProperties.Id);
Assert.Equal("Hello", hookEntry.Fields.Title["en-US"].ToString());
}

[Fact]
public async Task RegisteredConsumerShouldDeserializePayloadCorrectlyForMultipleConsumers()
{
Expand Down
8 changes: 4 additions & 4 deletions Contentful.AspNetCore/Contentful.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<PackageProjectUrl>https://github.com/contentful/contentful.net</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<Version>6.0.3</Version>
<AssemblyVersion>6.0.3.0</AssemblyVersion>
<Version>6.0.4</Version>
<AssemblyVersion>6.0.4.0</AssemblyVersion>
<RepositoryUrl>https://github.com/contentful/contentful.net</RepositoryUrl>
<FileVersion>6.0.3.0</FileVersion>
<FileVersion>6.0.4.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>bin\Release\netstandard1.5\Contentful.AspNetCore.xml</DocumentationFile>
Expand All @@ -25,7 +25,7 @@
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="contentful.csharp" Version="6.0.3" />
<PackageReference Include="contentful.csharp" Version="6.0.4" />
<PackageReference Include="gitlink" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
Expand Down
29 changes: 28 additions & 1 deletion Contentful.AspNetCore/MiddleWare/WebhookMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ public interface IConsumerBuilder
/// <param name="consumer">The consumer that will be called if the name, action and topic matches.</param>
/// <param name="preRequestVerification">Consumer specific verification of the request.</param>
void AddConsumer<T>(string name, string topicType, string topicAction, Func<T, object> consumer, Func<HttpContext, bool> preRequestVerification = null);

/// <summary>
/// Adds a consumer to the middleware pipeline.
/// </summary>
/// <typeparam name="T">The type the consumer expects.</typeparam>
/// <param name="name">The name of the webhook. * can be used as a wildcard.</param>
/// <param name="topicType">The type of topics to trigger this consumer for. * can be used as a wildcard.</param>
/// <param name="topicAction">The actions to trigger this consumer for. * can be used as a wildcard.</param>
/// <param name="consumer">The consumer that will be called if the name, action and topic matches.</param>
/// <param name="preRequestVerification">Consumer specific verification of the request.</param>
void AddConsumer<T>(string name, string topicType, string topicAction, Func<T, Task<object>> consumer, Func<HttpContext, bool> preRequestVerification = null);
}

/// <summary>
Expand Down Expand Up @@ -203,7 +214,23 @@ public void AddConsumer<T>(string name, string topicType, string topicAction, Fu
Delegate consumerDelegate = consumer;
_consumers.Add(Tuple.Create(tuple, consumerDelegate, preRequestVerification));
}


/// <summary>
/// Adds a consumer to the middleware pipeline.
/// </summary>
/// <typeparam name="T">The type the consumer expects.</typeparam>
/// <param name="name">The name of the webhook. * can be used as a wildcard.</param>
/// <param name="topicType">The type of topics to trigger this consumer for. * can be used as a wildcard.</param>
/// <param name="topicAction">The actions to trigger this consumer for. * can be used as a wildcard.</param>
/// <param name="consumer">The consumer that will be called if the name, action and topic matches.</param>
/// <param name="preRequestVerification">Consumer specific verification of the request.</param>
public void AddConsumer<T>(string name, string topicType, string topicAction, Func<T, Task<object>> consumer, Func<HttpContext, bool> preRequestVerification = null)
{
var tuple = Tuple.Create(name, topicType, topicAction);
Delegate consumerDelegate = consumer;
_consumers.Add(Tuple.Create(tuple, consumerDelegate, preRequestVerification));
}

/// <summary>
/// Helper method to turn the list of consumers into a lookup.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions Contentful.Core/Contentful.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<AssemblyVersion>6.0.3.0</AssemblyVersion>
<FileVersion>6.0.3.0</FileVersion>
<Version>6.0.3</Version>
<AssemblyVersion>6.0.4.0</AssemblyVersion>
<FileVersion>6.0.4.0</FileVersion>
<Version>6.0.4</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="gitlink" Version="3.1.0">
Expand Down

0 comments on commit ea83856

Please sign in to comment.