apidoc updates

This commit is contained in:
Gardient
2021-09-30 23:00:46 +03:00
parent 71c1e322aa
commit 04d70ba424
8 changed files with 42 additions and 24 deletions

View File

@@ -53,6 +53,9 @@ def register_apispecs(app: Flask):
that one doesn't handle the static routes or blueprints in blueprints well that one doesn't handle the static routes or blueprints in blueprints well
""" """
apispec.spec.components.security_scheme('api_key', {"type": "apiKey", "in": "query", "name": "apikey"})
apispec.spec.components.security_scheme('jwt', {"type": "http", "scheme": "bearer", "bearerFormat": "JWT"})
for name, rule in app.view_functions.items(): for name, rule in app.view_functions.items():
if name == 'static': if name == 'static':
continue continue

View File

@@ -1,11 +1,14 @@
from marshmallow import Schema, fields from marshmallow import Schema, fields
class LoginSchema(Schema): class LoginSchema(Schema):
username = fields.Str() username = fields.Str(required=True)
password = fields.Str(load_only=True) password = fields.Str(required=True, load_only=True)
class TokenResponseSchema(Schema): class TokenResponseSchema(Schema):
token = fields.Str() token = fields.Str()
login_schema = LoginSchema() login_schema = LoginSchema()
token_response_schema = TokenResponseSchema() token_response_schema = TokenResponseSchema()

View File

@@ -4,21 +4,19 @@ from flask_apispec import use_kwargs, marshal_with, doc
from marshmallow import fields from marshmallow import fields
from api.exceptions import BadRequestException from api.exceptions import BadRequestException
from api.utils import docwrap
import api.constants as constants import api.constants as constants
from .serializers import token_response_schema from .serializers import token_response_schema, login_schema
blueprint = Blueprint('login', __name__) blueprint = Blueprint('login', __name__)
@doc(tags=['login']) @docwrap('Login', None)
@blueprint.route('', methods=['POST']) @blueprint.route('', methods=['POST'])
@jwt_required(optional=True) @jwt_required(optional=True)
@use_kwargs({ @use_kwargs(login_schema)
'username': fields.Str(required=True),
'password': fields.Str(required=True)
})
@marshal_with(token_response_schema) @marshal_with(token_response_schema)
def login_user(username, password, **kwargs): def login_user(username, password):
if username == constants.API_USER and password == current_app.config[constants.API_PASS]: if username == constants.API_USER and password == current_app.config[constants.API_PASS]:
return {'token': create_access_token(identity=username, fresh=True, expires_delta=False)} return {'token': create_access_token(identity=username, fresh=True, expires_delta=False)}
else: else:

View File

@@ -5,18 +5,21 @@ from marshmallow import fields
from sqlalchemy import or_ from sqlalchemy import or_
from api.exceptions import NotFoundException, BadRequestException from api.exceptions import NotFoundException, BadRequestException
from api.utils import docwrap
from api.target.models import Target from api.target.models import Target
from api.target_exchange.models import TargetExchange from api.target_exchange.models import TargetExchange
from .models import Registration from .models import Registration
from .serializers import registration_schema, registrations_schema from .serializers import registration_schema, registrations_schema
blueprint = Blueprint('Registration', __name__) blueprint = Blueprint('Registration', __name__)
doc = docwrap('Registration')
@doc(tags=['Registration']) @doc
@blueprint.route('', methods=['GET']) @blueprint.route('', methods=['GET'])
@jwt_required() @jwt_required()
@use_kwargs({'exchange': fields.Str(), 'target': fields.Str()}) @use_kwargs({'exchange': fields.Str(), 'target': fields.Str()}, location='query')
@marshal_with(registrations_schema) @marshal_with(registrations_schema)
def get_list(exchange=None, target=None): def get_list(exchange=None, target=None):
res = Registration.query res = Registration.query
@@ -26,7 +29,7 @@ def get_list(exchange=None, target=None):
return res.all() return res.all()
@doc(tags=['Registration']) @doc
@blueprint.route('', methods=['POST']) @blueprint.route('', methods=['POST'])
@jwt_required() @jwt_required()
@use_kwargs(registration_schema) @use_kwargs(registration_schema)
@@ -43,7 +46,7 @@ def create(name, routing_key, targets):
return registration return registration
@doc(tags=['Registration']) @doc
@blueprint.route('/<registration_id>', methods=['GET']) @blueprint.route('/<registration_id>', methods=['GET'])
@jwt_required() @jwt_required()
@marshal_with(registration_schema) @marshal_with(registration_schema)
@@ -55,7 +58,7 @@ def get_by_id(registration_id: int):
return NotFoundException(Registration.__name__) return NotFoundException(Registration.__name__)
@doc(tags=['Registration']) @doc
@blueprint.route('/<registration_id>', methods=['PUT']) @blueprint.route('/<registration_id>', methods=['PUT'])
@jwt_required() @jwt_required()
@use_kwargs(registration_schema) @use_kwargs(registration_schema)

View File

@@ -18,6 +18,7 @@ class Config(object):
JWT_AUTH_HEADER_PREFIX = 'Token' JWT_AUTH_HEADER_PREFIX = 'Token'
APISPEC_TITLE = 'MahssageBus API' APISPEC_TITLE = 'MahssageBus API'
APISPEC_VERSION = 'v0.1' APISPEC_VERSION = 'v0.1'
APISPEC_OAS_VERSION = '3.0.0'
RABBITMQ_HOST = os.environ.get("RABBITMQ_HOST", "localhost") RABBITMQ_HOST = os.environ.get("RABBITMQ_HOST", "localhost")

View File

@@ -5,16 +5,19 @@ from marshmallow import fields
from api.exceptions import NotFoundException, BadRequestException from api.exceptions import NotFoundException, BadRequestException
from api.target_exchange.models import TargetExchange from api.target_exchange.models import TargetExchange
from api.utils import docwrap
from .models import Target from .models import Target
from .serializers import target_schema, targets_schema from .serializers import target_schema, targets_schema
blueprint = Blueprint('target', __name__) blueprint = Blueprint('target', __name__)
doc = docwrap('Target')
@doc(tags=['Target']) @doc
@blueprint.route('', methods=['GET']) @blueprint.route('', methods=['GET'])
@jwt_required() @jwt_required()
@use_kwargs({'exchange': fields.Str()}) @use_kwargs({'exchange': fields.Str()}, location='query')
@marshal_with(targets_schema) @marshal_with(targets_schema)
def get_list(exchange=None): def get_list(exchange=None):
res = Target.query res = Target.query
@@ -23,7 +26,7 @@ def get_list(exchange=None):
return res.all() return res.all()
@doc(tags=['Target']) @doc
@blueprint.route('', methods=['POST']) @blueprint.route('', methods=['POST'])
@jwt_required() @jwt_required()
@use_kwargs(target_schema) @use_kwargs(target_schema)
@@ -37,7 +40,7 @@ def create(name, routing_key, exchange):
return target return target
@doc(tags=['Target']) @doc
@blueprint.route('/<target_id>', methods=['GET']) @blueprint.route('/<target_id>', methods=['GET'])
@jwt_required() @jwt_required()
@marshal_with(target_schema) @marshal_with(target_schema)
@@ -49,7 +52,7 @@ def get_by_id(target_id: int):
return NotFoundException(Target.__name__) return NotFoundException(Target.__name__)
@doc(tags=['Target']) @doc
@blueprint.route('/<target_id>', methods=['PUT']) @blueprint.route('/<target_id>', methods=['PUT'])
@jwt_required() @jwt_required()
@use_kwargs(target_schema) @use_kwargs(target_schema)

View File

@@ -1,16 +1,18 @@
from flask import Blueprint from flask import Blueprint
from flask_jwt_extended import jwt_required from flask_jwt_extended import jwt_required
from flask_apispec import use_kwargs, marshal_with, doc from flask_apispec import use_kwargs, marshal_with
from api.exceptions import NotFoundException from api.exceptions import NotFoundException
from api.utils import docwrap
from .models import TargetExchange from .models import TargetExchange
from .serializers import target_exchange_schema, target_exchanges_schema from .serializers import target_exchange_schema, target_exchanges_schema
blueprint = Blueprint('target_exchange', __name__) blueprint = Blueprint('target_exchange', __name__)
doc = docwrap('TargetExchange')
@doc(tags=['TargetExchange']) @doc
@blueprint.route('', methods=['GET']) @blueprint.route('', methods=['GET'])
@jwt_required() @jwt_required()
@marshal_with(target_exchanges_schema) @marshal_with(target_exchanges_schema)
@@ -18,7 +20,7 @@ def get_list():
return TargetExchange.query.all() return TargetExchange.query.all()
@doc(tags=['TargetExchange']) @doc
@blueprint.route('', methods=['POST']) @blueprint.route('', methods=['POST'])
@jwt_required() @jwt_required()
@use_kwargs(target_exchange_schema) @use_kwargs(target_exchange_schema)
@@ -29,7 +31,7 @@ def create(name):
return target_exchange return target_exchange
@doc(tags=['TargetExchange']) @doc
@blueprint.route('/<exchange_id>', methods=['GET']) @blueprint.route('/<exchange_id>', methods=['GET'])
@jwt_required() @jwt_required()
@marshal_with(target_exchange_schema) @marshal_with(target_exchange_schema)
@@ -41,7 +43,7 @@ def get_by_id(exchange_id):
return NotFoundException(__name__) return NotFoundException(__name__)
@doc(tags=['TargetExchange']) @doc
@blueprint.route('/<exchange_id>', methods=['PUT']) @blueprint.route('/<exchange_id>', methods=['PUT'])
@jwt_required() @jwt_required()
@use_kwargs(target_exchange_schema) @use_kwargs(target_exchange_schema)

5
api/utils.py Normal file
View File

@@ -0,0 +1,5 @@
from flask_apispec import doc
def docwrap(tag, security='jwt', **kwargs):
return doc(tags=[tag], security=([{security: []}] if security is not None else None), **kwargs)