Build and document REST APIs with Flask and apispec
Project description
flask-apispec is a lightweight tool for building REST APIs in Flask. flask-apispec uses webargs for request parsing, marshmallow for response formatting, and apispec to automatically generate Swagger markup. You can use flask-apispec with vanilla Flask or a fuller-featured framework like Flask-RESTful.
Install
pip install flask-apispec
Quickstart
from flask import Flask
from flask_apispec import use_kwargs, marshal_with
from marshmallow import fields, Schema
from .models import Pet
app = Flask(__name__)
class PetSchema(Schema):
class Meta:
fields = ('name', 'category', 'size')
@app.route('/pets')
@use_kwargs({'category': fields.Str(), 'size': fields.Str()})
@marshal_with(PetSchema(many=True))
def get_pets(**kwargs):
return Pet.query.filter_by(**kwargs)
flask-apispec works with function- and class-based views:
from flask import make_response
from flask_apispec.views import MethodResource
class PetResource(MethodResource):
@marshal_with(PetSchema)
def get(self, pet_id):
return Pet.query.filter(Pet.id == pet_id).one()
@use_kwargs(PetSchema)
@marshal_with(PetSchema, code=201)
def post(self, **kwargs):
return Pet(**kwargs)
@use_kwargs(PetSchema)
@marshal_with(PetSchema)
def put(self, pet_id, **kwargs):
pet = Pet.query.filter(Pet.id == pet_id).one()
pet.__dict__.update(**kwargs)
return pet
@marshal_with(None, code=204)
def delete(self, pet_id):
pet = Pet.query.filter(Pet.id == pet_id).one()
pet.delete()
return make_response('', 204)
flask-apispec generates Swagger markup for your view functions and classes. By default, Swagger JSON is served at /swagger/, and Swagger-UI at /swagger-ui/.
from apispec import APISpec
from flask_apispec.extension import FlaskApiSpec
app.config.update({
'APISPEC_SPEC': APISpec(
title='pets',
version='v1',
plugins=['apispec.ext.marshmallow'],
),
'APISPEC_SWAGGER_URL': '/swagger/',
})
docs = FlaskApiSpec(app)
docs.register(get_pets)
docs.register(PetResource)
Documentation
Notes
flask-apispec is strongly inspired by Flask-RESTful and Flask-RESTplus, but attempts to provide similar functionality with greater flexibility and less code.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flask-apispec-0.4.1.tar.gz
.
File metadata
- Download URL: flask-apispec-0.4.1.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ccb133afb9cfed6b2bab2abf8c0ebc6278b74e7ac4962cc1340250c5e2d03b3 |
|
MD5 | 16dfc065b0d2618a6ef35f15e1c756f0 |
|
BLAKE2b-256 | d59125d8a22d98a6465b03d8f97c400eb95c846bd8a7eeb5a39b2517735d5784 |
Provenance
File details
Details for the file flask_apispec-0.4.1-py2.py3-none-any.whl
.
File metadata
- Download URL: flask_apispec-0.4.1-py2.py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddc9f3a13049a65db68cbdc4663ea951c9d052bb6b172399116f2747687d45b3 |
|
MD5 | 5515e62947a5b7639c142912344f885c |
|
BLAKE2b-256 | d8c66a0fdd3d836a7abdaaaf90252cde56f71a4187d49a43c5923569c93d3192 |