webhook endpoint queue message in rabbitMQ
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user