From b5a66b5d2d4d8564afe69087317e93f9fbeec316 Mon Sep 17 00:00:00 2001 From: Gardient Date: Fri, 1 Oct 2021 20:17:19 +0300 Subject: [PATCH] webhook endpoint queue message in rabbitMQ --- api/commands.py | 2 ++ api/webhook/views.py | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/api/commands.py b/api/commands.py index 3427efb..4cc202b 100644 --- a/api/commands.py +++ b/api/commands.py @@ -100,9 +100,11 @@ def seed(): def setup_rabmq(): """Set up rabbitMQ""" for exchange in TargetExchange.query.filter(TargetExchange.name != "").all(): + print(f"making sure {exchange.name} exchange exists") rabbit.ensure_exchange_exists(exchange.name) for target in Target.query.join(Target.exchange).filter(TargetExchange.name == "").all(): + print(f"making sure {target.name} queue exists") rabbit.ensure_queue_exists(target.routing_key) pass diff --git a/api/webhook/views.py b/api/webhook/views.py index 5b33db7..e3dd952 100644 --- a/api/webhook/views.py +++ b/api/webhook/views.py @@ -1,22 +1,34 @@ -from flask import Blueprint, jsonify +from flask import Blueprint, request, jsonify from flask_apispec import use_kwargs from marshmallow import fields from api.exceptions import NotFoundException from api.registration.models import Registration from api.utils import docwrap +from api.extensions import rabbit blueprint = Blueprint('webhook', __name__) @docwrap('webhook', 'api_key') -@blueprint.route('/webhook', methods=['GET']) +@blueprint.route('/webhook', methods=['GET', 'POST']) @use_kwargs({'apikey': fields.String(required=True)}, location='query') def webhook(apikey): - reg = Registration.query.filter_by(token=apikey).first() + reg: Registration = Registration.query.filter_by(token=apikey).first() if reg is None: raise NotFoundException(Registration.__name__) - return jsonify({'response': repr(reg)}) - pass + if request.method == 'GET': + message = request.args.to_dict() + message.pop('apikey', None) + else: + message = request.get_json() + + for target in reg.targets: + if target.exchange.name == '': + rabbit.queue_publish(target.name, message) + else: + rabbit.exchange_publish(target.exchange.name, target.name, message) + + return None, 204