Flask-APIBlueprint is a Flask micro-framework extension which adds support for route inheritance for Blueprints.
If you're building an API with Blueprints in Flask, you don't have to redeclare the routes if using the APIBlueprint
class. You can also override routes as you version your API and remap endpoints.
Flask. It will automatically be installed if you install the extension via pip:
$ pip install flask-apiblueprint
To run the tests use the test_apiblueprint.py
file:
$ python test_apiblueprint.py
Use the inherit_from
keyword argument in the APIBlueprint
constructor to copy routes over from another APIBlueprint
.
api_v2 = APIBlueprint(
'api_v2', __name__, subdomain='', url_prefix='/api/v2', inherit_from=api_v1
)
To override copied routes, just redeclare them.
@api_v1.route('/user/<user_id>/')
def username(user_id):
username = User.query.get(user_id).username
return jsonify(username=username)
@api_v2.route('/user/<user_id>/')
def user_info(user_id):
username = User.query.get(user_id).username
firstname = User.query.get(user_id).firstname
return jsonify(data=dict(username=username, firstname=firstname))
Use the remapping
keyword argument in the constructor to change the endpoints of inherited routes.
remapping = {'/users/list/': '/users/'}
api_v2 = APIBlueprint(
'api_v2', __name__, subdomain='', url_prefix='/api/v2', inherit_from=api_v1,
remapping=remapping
)
See the docs for more details or run the app.py
file to see how the sample_api is implemented.
$ python app.py