Skip to content

add Blueprint class #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions examples/petstore_blueprint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import logging

import flask.views
import marshmallow as ma

from flask_apispec import FlaskApiSpec, MethodResource
from flask_apispec import doc, marshal_with, use_kwargs
from flask_apispec.blueprint import Blueprint

app = flask.Flask(__name__)
docs = FlaskApiSpec(app)

blueprint = Blueprint(docs, name='pet', import_name=__name__)

logging.basicConfig(level=logging.DEBUG)


class Pet:
def __init__(self, name, type):
self.name = name
self.type = type


class PetSchema(ma.Schema):
name = ma.fields.Str()
type = ma.fields.Str()


@blueprint.route('/pets/<pet_id>')
@doc(params={'pet_id': {'description': 'pet id'}})
@marshal_with(PetSchema)
@use_kwargs({'breed': ma.fields.Str()}, location='query')
def get_pet(pet_id,breed):
return Pet('calici', 'cat')


@blueprint.route('/cat/<pet_id>')
@doc(
tags=['pets'],
params={'pet_id': {'description': 'the pet name'}},
)
class CatResource(MethodResource):

@marshal_with(PetSchema)
def get(self, pet_id):
return Pet('calici', 'cat')

@marshal_with(PetSchema)
def put(self, pet_id):
return Pet('calici', 'cat')


if __name__ == '__main__':
app.register_blueprint(blueprint)
app.run(debug=True)
1 change: 0 additions & 1 deletion flask_apispec/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def wrapper(func):
return activate(func)
return wrapper


def marshal_with(schema, code='default', description='', inherit=None, apply=None):
"""Marshal the return value of the decorated view function using the
specified schema.
Expand Down
30 changes: 30 additions & 0 deletions flask_apispec/blueprint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from flask import Blueprint as FlaskBlueprint
from flask.blueprints import BlueprintSetupState
from flask_apispec import MethodResource, FlaskApiSpec
import logging
import inspect

log = logging.getLogger(__name__)


class DocsBlueprintSetupState(BlueprintSetupState):

def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
log.debug('add url rule:%s' % rule)
doc_view_func = view_func
if inspect.isclass(view_func) and issubclass(view_func, MethodResource):
view_func = view_func.as_view(endpoint or view_func.__name__)
super().add_url_rule(rule, endpoint, view_func, **options)
docs = self.blueprint.docs
log.debug('register %s' % doc_view_func.__name__)
docs.register(doc_view_func, endpoint=endpoint, blueprint=self.blueprint.name)


class Blueprint(FlaskBlueprint):

def __init__(self, docs: FlaskApiSpec, name: str, import_name: str, **kwargs):
super().__init__(name, import_name, **kwargs)
self.docs = docs

def make_setup_state(self, app, options, first_registration=False):
return DocsBlueprintSetupState(self, app, options, first_registration)