Ejemplo n.º 1
0
        /// <inheritdoc />
        /// <exception cref="ArgumentException">Id, callback, secret or filters is <see langword="null" /></exception>
        public async Task <WebHookRegistrationStoreResult> InsertWebHookAsync(IPrincipal user, IWebHook webHook, CancellationToken cancellationToken = default)
        {
            if (webHook == null)
            {
                throw new ArgumentNullException(nameof(webHook));
            }

            if (webHook.Id == default)
            {
                throw new ArgumentException("WebHook id needs to be set by client.");
            }

            if (webHook.Callback == null)
            {
                throw new ArgumentException("WebHook callback needs to be set.");
            }

            if (webHook.Secret == null)
            {
                throw new ArgumentException("WebHook secret needs to be set.");
            }

            if (webHook.Filters == null)
            {
                throw new ArgumentException("WebHook filters needs to be set.");
            }

            var key = await _idGetter.GetPrincipalIdAsync(user, cancellationToken);

            var dbWebHook = new WebHook
            {
                PrincipalId     = key,
                Callback        = webHook.Callback.ToString(),
                ProtectedSecret = _secretProtector.Protect(webHook.Secret),
                Filters         = webHook.Filters.Select(f => new WebHookFilter {
                    Trigger = f.Trigger
                }).ToList()
            };

            try
            {
                await _context.SaveAsync(dbWebHook);

                return(WebHookRegistrationStoreResult.Success);
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"WebHook {dbWebHook.Id} insertion failed : {e.Message}");
                return(WebHookRegistrationStoreResult.InternalError);
            }
        }
Ejemplo n.º 2
0
        private async Task AddLogAsync(IWebHookWorkItem workItem, string error = null)
        {
            var log = new WebHookLog
            {
                Error                 = error,
                WebHookId             = workItem.WebHook.Id,
                WebHookNotificationId = workItem.Id
            };

            try
            {
                await _logContext.SaveAsync(log);

                if (!string.IsNullOrEmpty(error))
                {
                    Logger.LogInformation(error);
                }
            }
            catch (Exception e)
            {
                if (!string.IsNullOrEmpty(error))
                {
                    Logger.LogError(error);
                }

                Logger.LogError($"Log failed for WebHook {workItem.WebHook.Id}. [{workItem.WebHook.Callback}]: {e.Message}");
            }
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        protected override async Task <Guid> LogAsync(IWebHookNotification notification, IReadOnlyList <IWebHook> webHooks, CancellationToken cancellationToken)
        {
            var notif = new WebHookNotification
            {
                Payload   = notification.Payload,
                TriggerId = notification.TriggerId,
                Count     = webHooks.Count
            };
            await _context.SaveAsync(notif);

            return(notif.Id);
        }