Exemplo n.º 1
0
        public async Task <HttpResponseMessage> SendWebhook(TenantInformation tenant, WebhookMessage message)
        {
            try
            {
                var webhookUri         = new Uri(message.Url);
                var httpClientProvider = new HttpClientProvider();
                var httpClient         = httpClientProvider.CreateWithCustomCertificateValidation(webhookUri, message.IgnoreInvalidSSLCertificate, ConfigHelper.WebhookConnectionTimeout);

                var request = new HttpRequestMessage
                {
                    RequestUri = webhookUri,
                    Method     = HttpMethod.Post,
                    Content    = new StringContent(message.WebhookJsonPayload, Encoding.UTF8, "application/json")
                };

                AddHttpHeaders(request, message, tenant);

                AddBasicAuthentication(request, message, tenant);

                AddSignatureAuthentication(request, message, tenant);

                AddNServiceBusHeaders(request, message, tenant);

                return(await httpClient.SendAsync(request));
            }
            catch (HttpRequestException e)
            {
                if (e.InnerException is WebException)
                {
                    var webException = (WebException)e.InnerException;
                    if (webException.Status == WebExceptionStatus.TrustFailure)
                    {
                        throw new WebhookExceptionRetryPerPolicy($"Failed to send webhook due to invalid SSL Certificate. {webException.Message}.");
                    }
                    else
                    {
                        throw new WebhookExceptionRetryPerPolicy($"Failed to send webhook due {webException.Status}. {webException.Message} {webException.Response}");
                    }
                }
                else
                {
                    throw new WebhookExceptionRetryPerPolicy($"Failed to send webhook due to {e.InnerException}.");
                }
            }
        }