add apispec swagger generation

This commit is contained in:
Gardient
2021-09-21 19:51:23 +03:00
parent d48f36df60
commit 2cab5310c9
3 changed files with 31 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
from flask import Flask, Blueprint from flask import Flask, Blueprint
from . import commands, login, target_exchange from . import commands, login, target_exchange
from .settings import ProdConfig, Config from .settings import ProdConfig, Config
from .extensions import db, migrate, jwt from .extensions import db, migrate, jwt, apispec
from .exceptions import ApiException from .exceptions import ApiException
@@ -16,6 +16,7 @@ def create_app(config: Config = ProdConfig) -> Flask:
app.config.from_object(config) app.config.from_object(config)
register_extensions(app) register_extensions(app)
register_blueprints(app) register_blueprints(app)
register_apispecs(app)
register_errorhandlers(app) register_errorhandlers(app)
register_shellcontext(app) register_shellcontext(app)
register_commands(app) register_commands(app)
@@ -28,18 +29,43 @@ def register_extensions(app: Flask):
db.init_app(app) db.init_app(app)
migrate.init_app(app, db) migrate.init_app(app, db)
jwt.init_app(app) jwt.init_app(app)
apispec.init_app(app)
def register_blueprints(app: Flask): def register_blueprints(app: Flask):
"""Register Flask blueprints.""" """Register Flask blueprints."""
api_blueprint = Blueprint('api', __name__, url_prefix='/api') api_blueprint = Blueprint('api', __name__, url_prefix='/api')
api_blueprint.register_blueprint(login.views.blueprint, url_prefix='/login') api_blueprint.register_blueprint(
api_blueprint.register_blueprint(target_exchange.views.blueprint, url_prefix='/target-exchange') login.views.blueprint, url_prefix='/login')
api_blueprint.register_blueprint(
target_exchange.views.blueprint, url_prefix='/target-exchange')
app.register_blueprint(api_blueprint) app.register_blueprint(api_blueprint)
def register_apispecs(app: Flask):
"""Register routes for apispec
Extended from FlaskApiSpec::register_existing_resources
that one doesn't handle the static routes or blueprints in blueprints well
"""
for name, rule in app.view_functions.items():
if name == 'static':
continue
try:
blueprint_name, _ = name.rsplit('.', 1)
except ValueError:
blueprint_name = None
try:
apispec.register(rule, blueprint=blueprint_name)
except TypeError:
pass
def register_errorhandlers(app: Flask): def register_errorhandlers(app: Flask):
def errorHandler(error: ApiException): def errorHandler(error: ApiException):
return error.to_response() return error.to_response()

View File

@@ -1,6 +1,7 @@
from flask_jwt_extended import JWTManager from flask_jwt_extended import JWTManager
from flask_migrate import Migrate from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy, Model from flask_sqlalchemy import SQLAlchemy, Model
from flask_apispec import FlaskApiSpec
class CRUDMixin(Model): class CRUDMixin(Model):
"""Mixin that adds convenience methods for CRUD (create, read, update, delete) operations.""" """Mixin that adds convenience methods for CRUD (create, read, update, delete) operations."""
@@ -32,3 +33,4 @@ class CRUDMixin(Model):
db = SQLAlchemy(model_class=CRUDMixin) db = SQLAlchemy(model_class=CRUDMixin)
migrate = Migrate() migrate = Migrate()
jwt = JWTManager() jwt = JWTManager()
apispec = FlaskApiSpec()

View File

@@ -16,7 +16,6 @@ blueprint = Blueprint('login', __name__)
@use_kwargs(login_schema) @use_kwargs(login_schema)
@marshal_with(token_response_schema) @marshal_with(token_response_schema)
def login_user(username, password, **kwargs): def login_user(username, password, **kwargs):
print(f'user: {username}; pass: {password}; expected:{current_app.config[constants.API_PASS]}')
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 TokenResponse(create_access_token(identity=username, fresh=True)) return TokenResponse(create_access_token(identity=username, fresh=True))
else: else: