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', 'POST']) @use_kwargs({'apikey': fields.String(required=True)}, location='query') def webhook(apikey): reg: Registration = Registration.query.filter_by(token=apikey).first() if reg is None: raise NotFoundException(Registration.__name__) 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