webhook endpoint

This commit is contained in:
Gardient
2021-09-30 23:01:16 +03:00
parent 04d70ba424
commit a0dbdc4ddf
7 changed files with 35 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
from flask import Flask, Blueprint
from . import commands, login, target_exchange, target, registration
from . import commands, login, target_exchange, target, registration, webhook
from .exceptions import ApiException
from .extensions import db, migrate, jwt, apispec, rabbit
from .settings import ProdConfig, Config
@@ -44,6 +44,7 @@ def register_blueprints(app: Flask):
api_blueprint.register_blueprint(registration.views.blueprint, url_prefix='/registration')
app.register_blueprint(api_blueprint)
app.register_blueprint(webhook.views.blueprint)
def register_apispecs(app: Flask):

View File

@@ -19,3 +19,6 @@ class Registration(SurrogatePK, Model):
def __init__(self, **kwargs):
super(Registration, self).__init__(**kwargs)
def __repr__(self):
return f'<{Registration.__name__}({self.id}):{self.name!r}->({",".join([repr(x) for x in self.targets])})>'

View File

@@ -16,4 +16,4 @@ class Target(SurrogatePK, Model):
super(Target, self).__init__(**kwargs)
def __repr__(self):
return '<%s(%d):%r->%r>' % (Target.__name__, self.id, self.routing_key, self.exchange.name)
return f'<{Target.__name__}({self.id}):{self.routing_key!r}->{self.exchange.name!r}>'

View File

@@ -1,5 +1,5 @@
from flask import Blueprint
from flask_apispec import use_kwargs, marshal_with, doc
from flask_apispec import use_kwargs, marshal_with
from flask_jwt_extended import jwt_required
from marshmallow import fields

View File

@@ -9,6 +9,9 @@ class TargetExchange(SurrogatePK, Model):
def __init__(self, **kwargs):
super(TargetExchange, self).__init__(**kwargs)
def __repr__(self):
return f'<{TargetExchange.__name__}({self.id}):{self.name!r}>'
@staticmethod
def ensure_created(name):
if TargetExchange.query.filter_by(name=name).first() is None:

3
api/webhook/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
"""the webhook endpoint"""
from . import views

22
api/webhook/views.py Normal file
View File

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