diff --git a/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs b/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs index da051a2bf..df4e8e41e 100644 --- a/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs +++ b/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs @@ -1,4 +1,10 @@ -namespace WebhookClient.Controllers; +using System; +using System.Security.Policy; +using System.Text; +using Azure; +using Microsoft.CodeAnalysis.VisualBasic.Syntax; + +namespace WebhookClient.Controllers; [ApiController] [Route("webhook-received")] @@ -8,11 +14,17 @@ public class WebhooksReceivedController : Controller private readonly ILogger _logger; private readonly IHooksRepository _hooksRepository; - public WebhooksReceivedController(IOptions options, ILogger logger, IHooksRepository hooksRepository) + private readonly IConfiguration _configuration; + + public WebhooksReceivedController(IOptions options, + ILogger logger, + IHooksRepository hooksRepository, + IConfiguration configuration) { _options = options.Value; _logger = logger; _hooksRepository = hooksRepository; + _configuration = configuration; } [HttpPost] @@ -31,6 +43,9 @@ public class WebhooksReceivedController : Controller When = hook.When, Token = token }; + + sendMessageToServiceBus(hook.Payload); + await _hooksRepository.AddNew(newHook); _logger.LogInformation("Received hook was processed."); return Ok(newHook); @@ -39,4 +54,49 @@ public class WebhooksReceivedController : Controller _logger.LogInformation("Received hook is NOT processed - Bad Request returned."); return BadRequest(); } + + //function to send the message to Service Bus + public void sendMessageToServiceBus(string data) + { + //get the sas key from app config + var saskey = _configuration["ApimWAF:SASKey"]; + var url = _configuration["ApimWAF:Url"]; + var apimKey = _configuration["ApimWAF:apimKey"]; + + if (saskey == null || url == null || apimKey == null) + { + _logger.LogInformation("Skiping sending APIM Request - missing configuration"); + return; + } + + try + { + + var handler = new HttpClientHandler(); + + //ignore the certificate errors + handler.ServerCertificateCustomValidationCallback = + HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; + + System.Net.ServicePointManager.ServerCertificateValidationCallback += + (s, cert, chain, sslPolicyErrors) => true; + + using (var client = new HttpClient(handler)) + { + var msg = new HttpRequestMessage(HttpMethod.Post, url); + msg.Headers.Add("Authorization", saskey); + msg.Headers.Add("Ocp-Apim-Subscription-Key", apimKey); + + msg.Content = new StringContent(data, Encoding.UTF8, "application/json"); + + var response = client.Send(msg); + + _logger.LogInformation("Sending the paid message to {Url} with \"{Data}\" result: \"{response}\"", url, data ?? string.Empty, response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); + } + } + catch (Exception ex) + { + _logger.LogInformation("Error sending APIM Request {}", ex); + } + } }