-
Notifications
You must be signed in to change notification settings - Fork 33
Target .NET 8, rewrite webhook controller #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Kontent.Ai.Delivery.Abstractions; | ||
|
|
@@ -10,55 +9,38 @@ | |
| namespace Kontent.Ai.Boilerplate.Areas.WebHooks.Controllers | ||
| { | ||
| [Area("WebHooks")] | ||
| public class WebhooksController : Controller | ||
| public class WebhooksController(IDeliveryCacheManager cacheManager) : Controller | ||
| { | ||
| private readonly IDeliveryCacheManager _cacheManager; | ||
|
|
||
| public WebhooksController(IDeliveryCacheManager cacheManager) | ||
| { | ||
| _cacheManager = cacheManager ?? throw new ArgumentNullException(nameof(cacheManager)); | ||
| } | ||
| private readonly IDeliveryCacheManager _cacheManager = cacheManager ?? throw new ArgumentNullException(nameof(cacheManager)); | ||
|
|
||
| [HttpPost] | ||
| public async Task<IActionResult> Index([FromBody] DeliveryWebhookModel model) | ||
| public async Task<IActionResult> Index([FromBody] WebhookNotification? webhook) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we support both until the 1.11.? or is it ok to cause this breaking now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the underlying package is https://github.com/kontent-ai/aspnetcore-extensions and it still supports legacy webhooks as well. the boilerplate should ideally use the most up-to-date approach and since we'll be removing legacy webhooks soon, I don't see a problem with adopting the new webhooks (wouldn't want users to scaffold a new project with webhooks that won't be a thing in a month). |
||
| { | ||
| if (model != null) | ||
| if (webhook is null) | ||
| { | ||
| var dependencies = new HashSet<string>(); | ||
| if (model.Data.Items?.Any() == true) | ||
| { | ||
| foreach (var item in model.Data.Items ?? Enumerable.Empty<DeliveryWebhookItem>()) | ||
| { | ||
| dependencies.Add(CacheHelpers.GetItemDependencyKey(item.Codename)); | ||
| } | ||
|
|
||
| dependencies.Add(CacheHelpers.GetItemsDependencyKey()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about items dependencyKey? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, this was way too simple. I added more complex logic with pattern matching to wipe the keys of (potentially) dependent entities |
||
| } | ||
|
|
||
| if (model.Data.Taxonomies?.Any() == true) | ||
| { | ||
| foreach (var taxonomy in model.Data.Taxonomies ?? Enumerable.Empty<Taxonomy>()) | ||
| { | ||
| dependencies.Add(CacheHelpers.GetTaxonomyDependencyKey(taxonomy.Codename)); | ||
| } | ||
|
|
||
| dependencies.Add(CacheHelpers.GetTaxonomiesDependencyKey()); | ||
| dependencies.Add(CacheHelpers.GetItemsDependencyKey()); | ||
| dependencies.Add(CacheHelpers.GetTypesDependencyKey()); | ||
| } | ||
| return Ok(); | ||
| } | ||
|
|
||
| if (model.Message.Type == "content_type") | ||
| { | ||
| dependencies.Add(CacheHelpers.GetTypesDependencyKey()); | ||
| } | ||
| var tasks = (webhook.Notifications ?? Enumerable.Empty<WebhookModel>()) | ||
| .Select(GetDependencyKey) | ||
| .Where(key => key is not null) | ||
| .Distinct(StringComparer.Ordinal) | ||
| .Select(key => _cacheManager.InvalidateDependencyAsync(key!)); | ||
|
|
||
| foreach (var dependency in dependencies) | ||
| { | ||
| await _cacheManager.InvalidateDependencyAsync(dependency); | ||
| } | ||
| } | ||
| await Task.WhenAll(tasks); | ||
|
|
||
| return Ok(); | ||
| } | ||
|
|
||
| private static string? GetDependencyKey(WebhookModel notification) => | ||
|
||
| notification switch | ||
| { | ||
| { Message.ObjectType: "content_item", Data.System.Codename: var code } => CacheHelpers.GetItemDependencyKey(code), | ||
| { Message.ObjectType: "taxonomy", Data.System.Codename: var code } => CacheHelpers.GetTaxonomyDependencyKey(code), | ||
| { Message.ObjectType: "language" } => CacheHelpers.GetLanguagesDependencyKey(), | ||
| { Message.ObjectType: "content_type" } => CacheHelpers.GetTypesDependencyKey(), | ||
| // there's no cache helper in the SDK for asset notifications yet | ||
| _ => null | ||
| }; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would keep the original constructor, rather then using this hybrid solution for field validation. but it is up to your decision.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you might be right, especially since rest of the project doesn't use primary constructors either. reverting.