aspnet / WebHooks

[Archived] Libraries to create and consume web hooks on ASP.NET Core. Project moved to https://github.com/aspnet/AspLabs
Apache License 2.0
627 stars 439 forks source link

Question: How do you filter to events by dynamic id? #97

Closed whozDougie closed 6 years ago

whozDougie commented 7 years ago

Hi,

I'm trying to filter events to a specific unique Id. For example, I want a user to subscribe to a Webhook event for a specific job ID (without creating "N" numbers of web hooks). In other words, I want to only be notified if my job of id 321 is complete.

I'm wonder what the best approach would be to achieve this?

EDIT: For example, I want to fire event2, but only tell subscribers of event2 who are concerned of jobId 321. I can get the subscription to filter on event2, but I can't seem to get it to narrow down another level without introducing a unique event for each jobId.

I also saw another post of a similar question here, so I'm sure I'm not alone with this challenge: https://blogs.msdn.microsoft.com/webdev/2015/09/15/sending-webhooks-with-asp-net-webhooks-preview/

I followed this guide to implement webhooks for a job scheduling application. Users now get webhooks when a job is finished. Problem is, everybody who subscribes to the “job_FinishedEvent” gets a webhook for every job that is finished. I would like to allow users of my application to subscribe to Webhook event for specific entities/jobs e.g. “I want want to receive a webhook when job with id 123 is done”.

Can this be done out of the box with the ASP.NET Webhook packages? Or is it up to the subscribers to ignore events for jobs they are not interested in?

I was thinking of making the IWebHookFilterProvider pull the list of supported filters from the jobs in the database that are in progress (e.g. “job123_finishedEvent”). That would mean the list of supported filter changes a lot over time. But I noticed in the Microsoft.AspNet.WebHooks.Custom source code that the filters get fetched only once: code sample from Microsoft.AspNet.WebHooks.FilterManager:

public async Task GetAllWebHookFiltersAsync() { if (_filters == null) { //code to fetch the filters …

So I suppose the list of supported events/filters should be somewhat static? Is there a proper way to do this?

===================================================================

Thanks in Advance!

HenrikFrystykNielsen commented 7 years ago

You can filter who you will send notifications to based on arbitrary data fields such as whether a job ID is 321 using the NotifyAsync methods that take a Func as part of their parameters. The core API is defined here [1] but there are helpers for calling them from both Web API controllers [2] and MVC controllers [3].

Hope this helps,

Henrik

[1] https://github.com/aspnet/WebHooks/blob/master/src/Microsoft.AspNet.WebHooks.Custom/WebHooks/IWebHookManager.cs [2] https://github.com/aspnet/WebHooks/blob/master/src/Microsoft.AspNet.WebHooks.Custom/Extensions/ApiControllerExtensions.cs [3] https://github.com/aspnet/WebHooks/blob/master/src/Microsoft.AspNet.WebHooks.Custom.Mvc/Extensions/ControllerExtensions.cs

last-Programmer commented 7 years ago

Hi, We also would like to know this. How do we pass the arbitrary data fields while doing the registration. data: JSON.stringify({ WebHookUri: "http://localhost:59927/api/webhooks/incoming/custom", Secret: "12345678901234567890123456789012", Description: "My first WebHook!" }), in the above mentioned webhook registration payload how to we specify the id or any other arbitrary data. because we are not finding any documentation for this payload fields or sending arbitrary data

Thanks

last-Programmer commented 7 years ago

Never mind. we have found.

We need to pass a property called Properties : { Id: "123123123"} in the payload.

Thanks

HenrikFrystykNielsen commented 7 years ago

Right on -- glad you found it!

dougbu commented 6 years ago

Question has been answered.