Ejemplo n.º 1
0
        public void SendWebhookSecretMessage(WebhookSecretMessageDto secretMessageDto)
        {
            var factory = new ConnectionFactory
            {
                HostName = "localhost",
                Port     = 5672
            };

            using var connection = factory.CreateConnection();
            using var channel    = connection.CreateModel();
            channel.ExchangeDeclare("webhook_secret", ExchangeType.Direct);

            var message = JsonSerializer.Serialize(secretMessageDto);
            var body    = Encoding.UTF8.GetBytes(message);

            channel.BasicPublish("webhook_secret", "webhook", null, body);

            Console.WriteLine("---> Message Published in Message Bus");
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <WebhookSubscriptionReadDto> > CreateSubscription(
            WebhookSubscriptionCreateDto webhookSubscriptionCreateDto)
        {
            var subscription =
                await _context.WebhookSubscriptions.FirstOrDefaultAsync(s =>
                                                                        s.WebhookURI == webhookSubscriptionCreateDto.WebhookURI);

            if (subscription is not null)
            {
                return(await Task.FromResult(NoContent()));
            }
            subscription                  = _mapper.Map <WebhookSubscription>(webhookSubscriptionCreateDto);
            subscription.Secret           = Guid.NewGuid().ToString();
            subscription.WebhookPublisher = "AmTech";
            try
            {
                await _context.AddAsync(subscription);

                await _context.SaveChangesAsync();

                var webhookSecret = new WebhookSecretMessageDto
                {
                    Publisher = subscription.WebhookPublisher,
                    Secret    = subscription.Secret
                };

                _messageBus.SendWebhookSecretMessage(webhookSecret);
            }
            catch (Exception ex)
            {
                return(await Task.FromResult(BadRequest(ex.Message)));
            }

            var response = _mapper.Map <WebhookSubscriptionReadDto>(subscription);

            return(CreatedAtRoute(
                       routeName: "GetSubscriptionBySecret",
                       routeValues: new { secret = response.Secret },
                       value: response));
        }