public void SendWebhook(WebhookSenderArgs webhookSenderArgs)
        {
            if (webhookSenderArgs.WebhookEventId == default)
            {
                throw new ArgumentNullException(nameof(webhookSenderArgs.WebhookEventId));
            }

            if (webhookSenderArgs.WebhookSubscriptionId == default)
            {
                throw new ArgumentNullException(nameof(webhookSenderArgs.WebhookSubscriptionId));
            }

            var webhookSendAttemptId = InsertAndGetIdWebhookSendAttempt(webhookSenderArgs);

            var request = CreateWebhookRequestMessage(webhookSenderArgs);

            var payload = GetWebhookPayload(webhookSenderArgs);

            var serializedBody = _webhooksConfiguration.JsonSerializerSettings != null
                ? payload.ToJsonString(_webhooksConfiguration.JsonSerializerSettings)
                : payload.ToJsonString();

            SignWebhookRequest(request, serializedBody, webhookSenderArgs.Secret);

            AddAdditionalHeaders(request, webhookSenderArgs);

            bool           isSucceed  = false;
            HttpStatusCode?statusCode = null;
            string         content    = FailedRequestDefaultContent;

            try
            {
                var response = AsyncHelper.RunSync(() => SendHttpRequest(request));
                isSucceed  = response.isSucceed;
                statusCode = response.statusCode;
                content    = response.content;
            }
            catch (TaskCanceledException)
            {
                statusCode = HttpStatusCode.RequestTimeout;
                content    = "Request Timeout";
            }
            catch (HttpRequestException e)
            {
                content = e.Message;
            }
            catch (Exception e)
            {
                Logger.Error("An error occured while sending a webhook request", e);
            }
            finally
            {
                StoreResponseOnWebhookSendAttempt(webhookSendAttemptId, webhookSenderArgs.TenantId, statusCode, content);
            }

            if (!isSucceed)
            {
                throw new Exception($"Webhook send attempt failed. WebhookSendAttempt id: {webhookSendAttemptId}");
            }
        }
Exemple #2
0
        public async Task <Guid> SendWebhookAsync(WebhookSenderArgs webhookSenderArgs)
        {
            if (webhookSenderArgs.WebhookEventId == default)
            {
                throw new ArgumentNullException(nameof(webhookSenderArgs.WebhookEventId));
            }

            if (webhookSenderArgs.WebhookSubscriptionId == default)
            {
                throw new ArgumentNullException(nameof(webhookSenderArgs.WebhookSubscriptionId));
            }

            var webhookSendAttemptId = await _webhookManager.InsertAndGetIdWebhookSendAttemptAsync(webhookSenderArgs);

            var request = CreateWebhookRequestMessage(webhookSenderArgs);

            var serializedBody = await _webhookManager.GetSerializedBodyAsync(webhookSenderArgs);

            _webhookManager.SignWebhookRequest(request, serializedBody, webhookSenderArgs.Secret);

            AddAdditionalHeaders(request, webhookSenderArgs);

            var            isSucceed  = false;
            HttpStatusCode?statusCode = null;
            var            content    = FailedRequestDefaultContent;

            try
            {
                var response = await SendHttpRequest(request);

                isSucceed  = response.isSucceed;
                statusCode = response.statusCode;
                content    = response.content;
            }
            catch (TaskCanceledException)
            {
                statusCode = HttpStatusCode.RequestTimeout;
                content    = "Request Timeout";
            }
            catch (HttpRequestException e)
            {
                content = e.Message;
            }
            catch (Exception e)
            {
                Logger.Error("An error occured while sending a webhook request", e);
            }
            finally
            {
                await _webhookManager.StoreResponseOnWebhookSendAttemptAsync(webhookSendAttemptId, webhookSenderArgs.TenantId, statusCode, content);
            }

            if (!isSucceed)
            {
                throw new Exception($"Webhook sending attempt failed. WebhookSendAttempt id: {webhookSendAttemptId}");
            }

            return(webhookSendAttemptId);
        }
        protected virtual WebhookPayload GetWebhookPayload(WebhookSenderArgs webhookSenderArgs)
        {
            dynamic data = _webhooksConfiguration.JsonSerializerSettings != null
                ? webhookSenderArgs.Data.FromJsonString <dynamic>(_webhooksConfiguration.JsonSerializerSettings)
                : webhookSenderArgs.Data.FromJsonString <dynamic>();

            var attemptNumber = WebhookSendAttemptStore.GetSendAttemptCount(webhookSenderArgs.TenantId,
                                                                            webhookSenderArgs.WebhookEventId, webhookSenderArgs.WebhookSubscriptionId) + 1;

            return(new WebhookPayload(webhookSenderArgs.WebhookEventId.ToString(), webhookSenderArgs.WebhookName, attemptNumber)
            {
                Data = data
            });
        }
        protected virtual Guid InsertAndGetIdWebhookSendAttempt(WebhookSenderArgs webhookSenderArgs)
        {
            var workItem = new WebhookSendAttempt
            {
                WebhookEventId        = webhookSenderArgs.WebhookEventId,
                WebhookSubscriptionId = webhookSenderArgs.WebhookSubscriptionId,
                TenantId = webhookSenderArgs.TenantId
            };

            WebhookSendAttemptStore.Insert(workItem);
            CurrentUnitOfWork.SaveChanges();

            return(workItem.Id);
        }
        protected virtual async Task <string> GetSerializedBodyAsync(WebhookSenderArgs webhookSenderArgs)
        {
            if (webhookSenderArgs.SendExactSameData)
            {
                return(webhookSenderArgs.Data);
            }

            var payload = await GetWebhookPayloadAsync(webhookSenderArgs);

            var serializedBody = _webhooksConfiguration.JsonSerializerSettings != null
                ? payload.ToJsonString(_webhooksConfiguration.JsonSerializerSettings)
                : payload.ToJsonString();

            return(serializedBody);
        }
        protected virtual async Task <Guid> InsertAndGetIdWebhookSendAttemptAsync(WebhookSenderArgs webhookSenderArgs)
        {
            var workItem = new WebhookSendAttempt
            {
                WebhookEventId        = webhookSenderArgs.WebhookEventId,
                WebhookSubscriptionId = webhookSenderArgs.WebhookSubscriptionId,
                TenantId = webhookSenderArgs.TenantId
            };

            await WebhookSendAttemptStore.InsertAsync(workItem);

            await CurrentUnitOfWork.SaveChangesAsync();

            return(workItem.Id);
        }
        public virtual string GetSerializedBody(WebhookSenderArgs webhookSenderArgs)
        {
            if (webhookSenderArgs.SendExactSameData)
            {
                return(webhookSenderArgs.Data);
            }

            var payload = GetWebhookPayload(webhookSenderArgs);

            var serializedBody = _webhooksConfiguration.JsonSerializerSettings != null
                ? payload.ToJsonString(_webhooksConfiguration.JsonSerializerSettings)
                : payload.ToJsonString();

            return(serializedBody);
        }
Exemple #8
0
        protected virtual void AddAdditionalHeaders(HttpRequestMessage request, WebhookSenderArgs webhookSenderArgs)
        {
            foreach (var header in webhookSenderArgs.Headers)
            {
                if (request.Headers.TryAddWithoutValidation(header.Key, header.Value))
                {
                    continue;
                }

                if (request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value))
                {
                    continue;
                }

                throw new Exception($"Invalid Header. SubscriptionId:{webhookSenderArgs.WebhookSubscriptionId},Header: {header.Key}:{header.Value}");
            }
        }
        public virtual async Task <WebhookPayload> GetWebhookPayloadAsync(WebhookSenderArgs webhookSenderArgs)
        {
            var data = _webhooksConfiguration.JsonSerializerSettings != null
                ? webhookSenderArgs.Data.FromJsonString <dynamic>(_webhooksConfiguration.JsonSerializerSettings)
                : webhookSenderArgs.Data.FromJsonString <dynamic>();

            var attemptNumber = await _webhookSendAttemptStore.GetSendAttemptCountAsync(
                webhookSenderArgs.TenantId,
                webhookSenderArgs.WebhookEventId,
                webhookSenderArgs.WebhookSubscriptionId);

            return(new WebhookPayload(
                       webhookSenderArgs.WebhookEventId.ToString(),
                       webhookSenderArgs.WebhookName,
                       attemptNumber)
            {
                Data = data
            });
        }
        public virtual async Task <Guid> InsertAndGetIdWebhookSendAttemptAsync(WebhookSenderArgs webhookSenderArgs)
        {
            using (var uow = UnitOfWorkManager.Begin())
            {
                var workItem = new WebhookSendAttempt
                {
                    WebhookEventId        = webhookSenderArgs.WebhookEventId,
                    WebhookSubscriptionId = webhookSenderArgs.WebhookSubscriptionId,
                    TenantId = webhookSenderArgs.TenantId
                };

                await _webhookSendAttemptStore.InsertAsync(workItem);

                await CurrentUnitOfWork.SaveChangesAsync();

                await uow.CompleteAsync();

                return(workItem.Id);
            }
        }
Exemple #11
0
 /// <summary>
 /// You can override this to change request message
 /// </summary>
 /// <returns></returns>
 protected virtual HttpRequestMessage CreateWebhookRequestMessage(WebhookSenderArgs webhookSenderArgs)
 {
     return(new HttpRequestMessage(HttpMethod.Post, webhookSenderArgs.WebhookUri));
 }