Implementing External listner to test Pub/Sub
This commit is contained in:
parent
5da4dff0da
commit
44a081b1b4
@ -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]
|
[ApiController]
|
||||||
[Route("webhook-received")]
|
[Route("webhook-received")]
|
||||||
@ -8,11 +14,17 @@ public class WebhooksReceivedController : Controller
|
|||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly IHooksRepository _hooksRepository;
|
private readonly IHooksRepository _hooksRepository;
|
||||||
|
|
||||||
public WebhooksReceivedController(IOptions<WebhookClientOptions> options, ILogger<WebhooksReceivedController> logger, IHooksRepository hooksRepository)
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
|
public WebhooksReceivedController(IOptions<WebhookClientOptions> options,
|
||||||
|
ILogger<WebhooksReceivedController> logger,
|
||||||
|
IHooksRepository hooksRepository,
|
||||||
|
IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_options = options.Value;
|
_options = options.Value;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_hooksRepository = hooksRepository;
|
_hooksRepository = hooksRepository;
|
||||||
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
@ -31,6 +43,9 @@ public class WebhooksReceivedController : Controller
|
|||||||
When = hook.When,
|
When = hook.When,
|
||||||
Token = token
|
Token = token
|
||||||
};
|
};
|
||||||
|
|
||||||
|
sendMessageToServiceBus(hook.Payload);
|
||||||
|
|
||||||
await _hooksRepository.AddNew(newHook);
|
await _hooksRepository.AddNew(newHook);
|
||||||
_logger.LogInformation("Received hook was processed.");
|
_logger.LogInformation("Received hook was processed.");
|
||||||
return Ok(newHook);
|
return Ok(newHook);
|
||||||
@ -39,4 +54,49 @@ public class WebhooksReceivedController : Controller
|
|||||||
_logger.LogInformation("Received hook is NOT processed - Bad Request returned.");
|
_logger.LogInformation("Received hook is NOT processed - Bad Request returned.");
|
||||||
return BadRequest();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user