Esempio n. 1
0
        public async Task SendWebhookNotification(FlightDetailChangedPayloadDto flightDetailChangedPayloadDto)
        {
            var serializedPayload = JsonSerializer.Serialize(flightDetailChangedPayloadDto);

            var httpClient = _httpClientFactory.CreateClient();

            var request = new HttpRequestMessage(HttpMethod.Post, flightDetailChangedPayloadDto.WebhookURI);

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            request.Content = new StringContent(serializedPayload);

            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            try
            {
                using (var response = await httpClient.SendAsync(request))
                {
                    Console.WriteLine("Success");
                    response.EnsureSuccessStatusCode();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unsuccessful {ex.Message}");
            }
        }
Esempio n. 2
0
        public void Run()
        {
            var factory = new ConnectionFactory()
            {
                HostName = "localhost", Port = 5672
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(exchange: "trigger", type: ExchangeType.Fanout);

                    var queueName = channel.QueueDeclare().QueueName;

                    channel.QueueBind(queue: queueName,
                                      exchange: "trigger",
                                      routingKey: "");

                    var consumer = new EventingBasicConsumer(channel);
                    Console.WriteLine("Listening on the message bus...");

                    consumer.Received += async(ModuleHandle, ea) =>
                    {
                        Console.WriteLine("Event is triggered!");

                        var body = ea.Body;
                        var notificationMessage = Encoding.UTF8.GetString(body.ToArray());
                        var message             = JsonSerializer.Deserialize <NotificationMessageDto>(notificationMessage);

                        var webhookToSend = new FlightDetailChangedPayloadDto
                        {
                            WebhookType = message.WebhookType,
                            WebhookURI  = string.Empty,
                            Secret      = string.Empty,
                            Publisher   = string.Empty,
                            OldPrice    = message.OldPrice,
                            NewPrice    = message.NewPrice,
                            FlightCode  = message.FlightCode
                        };

                        foreach (var whs in _context.WebhookSubscriptions.Where(s => s.WebhookType.Equals(message.WebhookType)))
                        {
                            webhookToSend.WebhookURI = whs.WebhookURI;
                            webhookToSend.Secret     = whs.Secret;
                            webhookToSend.Publisher  = whs.WebhhokPublisher;

                            await _webhookClient.SendWebhookNotification(webhookToSend);
                        }
                    };

                    channel.BasicConsume(queue: queueName,
                                         autoAck: true,
                                         consumer: consumer);
                    Console.ReadLine();
                }
        }