webhook endpoint queue message in rabbitMQ

This commit is contained in:
Gardient
2021-10-01 20:17:19 +03:00
parent a0dbdc4ddf
commit b5a66b5d2d
2 changed files with 19 additions and 5 deletions

View File

@@ -100,9 +100,11 @@ def seed():
def setup_rabmq(): def setup_rabmq():
"""Set up rabbitMQ""" """Set up rabbitMQ"""
for exchange in TargetExchange.query.filter(TargetExchange.name != "").all(): for exchange in TargetExchange.query.filter(TargetExchange.name != "").all():
print(f"making sure {exchange.name} exchange exists")
rabbit.ensure_exchange_exists(exchange.name) rabbit.ensure_exchange_exists(exchange.name)
for target in Target.query.join(Target.exchange).filter(TargetExchange.name == "").all(): 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) rabbit.ensure_queue_exists(target.routing_key)
pass pass

View File

@@ -1,22 +1,34 @@
from flask import Blueprint, jsonify from flask import Blueprint, request, jsonify
from flask_apispec import use_kwargs from flask_apispec import use_kwargs
from marshmallow import fields from marshmallow import fields
from api.exceptions import NotFoundException from api.exceptions import NotFoundException
from api.registration.models import Registration from api.registration.models import Registration
from api.utils import docwrap from api.utils import docwrap
from api.extensions import rabbit
blueprint = Blueprint('webhook', __name__) blueprint = Blueprint('webhook', __name__)
@docwrap('webhook', 'api_key') @docwrap('webhook', 'api_key')
@blueprint.route('/webhook', methods=['GET']) @blueprint.route('/webhook', methods=['GET', 'POST'])
@use_kwargs({'apikey': fields.String(required=True)}, location='query') @use_kwargs({'apikey': fields.String(required=True)}, location='query')
def webhook(apikey): def webhook(apikey):
reg = Registration.query.filter_by(token=apikey).first() reg: Registration = Registration.query.filter_by(token=apikey).first()
if reg is None: if reg is None:
raise NotFoundException(Registration.__name__) raise NotFoundException(Registration.__name__)
return jsonify({'response': repr(reg)}) if request.method == 'GET':
pass 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