Class WebhookManager
Webhook manager. Queues webhooks and processes them on a background queue using the ISystemWebhookPlugin and ICustomWebhookPlugin.
Implements
Inherited Members
Namespace: SuperOffice.CRM.Webhooks
Assembly: SoDataBase.BusinessLogic.dll
Syntax
public class WebhookManager : IWebhookManager
Constructors
WebhookManager(IAsyncQueue<WebhookTaskInfo>, IAsyncQueue<DelayedItem<RetryWebhook>>, IAsyncQueue<(ICustomWebhookPlugin, Webhook, WebhookPayload)>, ISoRequestItemsAccessor, IServiceScopeFactory, IServiceProvider)
Creates the webhook dispatcher - instantiates the systam and custom webhook plugins.
Declaration
public WebhookManager(IAsyncQueue<WebhookTaskInfo> queue, IAsyncQueue<DelayedItem<RetryWebhook>> retryQueue, IAsyncQueue<(ICustomWebhookPlugin, Webhook, WebhookPayload)> slowQueue, ISoRequestItemsAccessor soRequestItemsAccessor, IServiceScopeFactory serviceScopeFactory, IServiceProvider serviceProvider)
Parameters
| Type | Name | Description |
|---|---|---|
| IAsyncQueue<WebhookTaskInfo> | queue | |
| IAsyncQueue<DelayedItem<RetryWebhook>> | retryQueue | |
| IAsyncQueue<(ICustomWebhookPlugin, Webhook, WebhookPayload)> | slowQueue | |
| ISoRequestItemsAccessor | soRequestItemsAccessor | |
| IServiceScopeFactory | serviceScopeFactory | |
| IServiceProvider | serviceProvider |
Methods
DeleteWebhookAsync(int, CancellationToken)
Remove a given webhook from storage.
Declaration
public Task DeleteWebhookAsync(int id, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| int | id | |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
GetAllWebhooksAsync(string, string, string, WebhookState?, CancellationToken)
Return all the stored webhooks.
Declaration
public Task<IEnumerable<Webhook>> GetAllWebhooksAsync(string name = null, string eventName = null, string appToken = null, WebhookState? state = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Optional name of webhook to filter on. |
| string | eventName | Optional name of event to filter list of webhooks on. Default NULL for no filter. |
| string | appToken | Optional appToken name to filter on. |
| WebhookState? | state | Optional state to filter list of webhooks on. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<IEnumerable<Webhook>> | List of webhooks (that are listening to eventName, in state, with given name) |
GetCurrent()
Return the current webhook manager instance. It uses a SuperOffice.CRM.Webhooks.WebhookCache and a WebhookDispatcher to process webhooks on a background thread.
Declaration
public static IWebhookManager GetCurrent()
Returns
| Type | Description |
|---|---|
| IWebhookManager | The one and only webhook manager. |
GetCurrentQueueStats()
Webhook manager. Queues webhooks and processes them on a background queue using the ISystemWebhookPlugin and ICustomWebhookPlugin.
Declaration
public WebhookTaskInfo[] GetCurrentQueueStats()
Returns
| Type | Description |
|---|---|
| WebhookTaskInfo[] |
GetLastErrorAsync(int, CancellationToken)
Return the most recent error message received when calling this webhook.
Declaration
public Task<string> GetLastErrorAsync(int webhookId, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| int | webhookId | Webhook to get error message for |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<string> | Error message, or NULL if no errors received |
GetRetryQueueStats()
Webhook manager. Queues webhooks and processes them on a background queue using the ISystemWebhookPlugin and ICustomWebhookPlugin.
Declaration
public DelayedItem<RetryWebhook>[] GetRetryQueueStats()
Returns
| Type | Description |
|---|---|
| DelayedItem<RetryWebhook>[] |
GetSlowQueueLength()
Webhook manager. Queues webhooks and processes them on a background queue using the ISystemWebhookPlugin and ICustomWebhookPlugin.
Declaration
public int GetSlowQueueLength()
Returns
| Type | Description |
|---|---|
| int |
GetWebhookAsync(int, CancellationToken)
Get a single webhook by id
Declaration
public Task<Webhook> GetWebhookAsync(int id, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| int | id | primary key for webhook |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<Webhook> | The given webhook, or NULL if no found |
Notify(ChangeType, int, string, IDictionary<string, object>, int, IDictionary<string, object>)
Asynchronously inform all interested plugins that an event has occurred. In other words, we don't block on calls here. This method will return before the webhooks finish processing.
Declaration
public void Notify(ChangeType changeType, int primaryKey, string entityName, IDictionary<string, object> changes, int associateId, IDictionary<string, object> fieldValues)
Parameters
| Type | Name | Description |
|---|---|---|
| ChangeType | changeType | Created/Changed/Deleted/softDeleted |
| int | primaryKey | id |
| string | entityName | "contact", "sale", etc |
| IDictionary<string, object> | changes | Dictionary of changed fields "name", "country_id" mapped to the new values. |
| int | associateId | User id who triggered event. |
| IDictionary<string, object> | fieldValues |
NotifyWebhooksAsync(IEnumerable<Webhook>, WebhookPayload, CancellationToken)
Broadcast the event to all subscribers, and mark failures/success status on the passed webhook objects.
Declaration
public Task NotifyWebhooksAsync(IEnumerable<Webhook> webhooks, WebhookPayload eventInfo, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Webhook> | webhooks | |
| WebhookPayload | eventInfo | Event name, unique id and details |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
RecordResult(Webhook, WebhookResponse, CancellationToken)
Record the result of a webhook call in memoory or on database. May change state of webhook if too many errors or permanent failure.
Declaration
public Task RecordResult(Webhook hook, WebhookResponse result, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| Webhook | hook | |
| WebhookResponse | result | |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
SaveWebhookAsync(Webhook)
Store/Create a webhook.
Declaration
public Task<Webhook> SaveWebhookAsync(Webhook webhook)
Parameters
| Type | Name | Description |
|---|---|---|
| Webhook | webhook | webhook to store. Create new hook if id=0 |
Returns
| Type | Description |
|---|---|
| Task<Webhook> | Updated webhook, with primary key set. |
SignalEvent(string, int, string, IDictionary<string, object>, int)
Asynchronously inform all interested webhooks that an event has occurred. In other words, we don't block on calls here. This method will return before the webhooks finish processing.
Declaration
public void SignalEvent(string eventName, int primaryKey, string entityName, IDictionary<string, object> changes, int associateId)
Parameters
| Type | Name | Description |
|---|---|---|
| string | eventName | |
| int | primaryKey | id |
| string | entityName | "contact", "sale", etc |
| IDictionary<string, object> | changes | Dictionary of changed fields "name", "country_id" mapped to the new values. |
| int | associateId | User id who triggered event. |
TestWebhookAsync(Webhook, CancellationToken)
Test invoke a webhook with a PING event to see if it works.
Declaration
public Task<WebhookResponse> TestWebhookAsync(Webhook webhook, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| Webhook | webhook | webhook definition |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<WebhookResponse> | True if the webhook invocation worked, False if we did not get 200 OK. NULL is treated as failure. |
WaitForWebhooksToCompleteAsync(CancellationToken)
Wait for all webhooks currently being processed or in the queue to be completed. Primarely used by tests verifying that webhooks are called.
Declaration
public Task WaitForWebhooksToCompleteAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |