Skip to main content

Plugins for apispec

Project description

apispec-plugins

PyPI version ci codecov Code style: black License: MIT

apispec plugins for integrating with different components (web frameworks, packages, etc).

Currently supported plugins:

  • apispec_plugins.webframeworks.flask

Installation

Install the package directly from PyPI (recommended):

pip install apispec-plugins

Plugin dependencies like Flask are not installed with the package by default. To have Flask installed, do like so:

pip install apispec-plugins[flask]

Migration from apispec<1.0.0

The location from where plugins, like FlaskPlugin imports, are imported is different. Therefore, the imports need to be performed this way:

# apispec<1.0.0
from apispec.ext.flask import FlaskPlugin

# apispec>=1.0.0
from apispec_plugins.webframeworks.flask import FlaskPlugin

Example Usage

from apispec import APISpec
from apispec_plugins.webframeworks.flask import FlaskPlugin
from flask import Flask

spec = APISpec(
    title="Pet Store",
    version="1.0.0",
    openapi_version="2.0",
    info=dict(description="A minimal pet store API"),
    plugins=(FlaskPlugin(),),
)

app = Flask(__name__)


@app.route("/pet/<petId>")
def pet(petId):
    """Find pet by ID.
    ---
    get:
        parameters:
            - in: path
              name: petId
        responses:
            200:
                description: display pet data
    """
    return f"Display pet with ID {petId}"


# Since `path` inspects the view and its route,
# we need to be in a Flask request context
with app.test_request_context():
    spec.path(view=pet)

Alternatively, a Flask MethodView can be used:

from flask.views import MethodView


class PetAPI(MethodView):
    def get(self, petId):
        # get pet by ID
        pass


app.add_url_rule("/pet/<petId>", view_func=PetAPI.as_view("pet_view"))

There is also easy integration with other packages like Flask-RESTful:

from flask_restful import Api, Resource


class PetAPI(Resource):
    def get(self, petId):
        # get pet by ID
        pass


api = Api(app)
api.add_resource(PetAPI, "/pet/<petId>", endpoint="pet")

Dynamic specs

As seen so far, specs are specified in the docstring of the view or class. However, with the spec_from decorator, one can dynamically set specs:

from apispec_plugins import spec_from


@spec_from(
    {
        "parameters": {"in": "path", "name": "petId"},
        "responses": {200: {"description": "display pet data"}},
    }
)
def pet(petID):
    """Find pet by ID."""
    pass

Why not apispec-webframeworks?

The conceiving of this project was based on apispec-webframeworks. While that project is focused on integrating web frameworks with APISpec, this repository goes a step further in providing the best integration possible with the APISpec standards. Some limitations on that project were also addressed, like:

  • a path cannot register no more than 1 single rule per endpoint;
  • support for additional libraries like Flask-RESTful;
  • limited docstring spec processing;

Tests & linting

Run tests with tox:

# ensure tox is installed
$ tox

Run linter only:

$ tox -e lint

Optionally, run coverage as well with:

$ tox -e coverage

License

MIT licensed. See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

apispec-plugins-0.1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

apispec_plugins-0.1.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file apispec-plugins-0.1.1.tar.gz.

File metadata

  • Download URL: apispec-plugins-0.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.12 Darwin/20.5.0

File hashes

Hashes for apispec-plugins-0.1.1.tar.gz
Algorithm Hash digest
SHA256 de12b348fac7baf2bf98b3dcd63f0ffc4d2af8555d9b1184125495c9773a7d27
MD5 94ddde880a01093f0e010ba7e17bea7b
BLAKE2b-256 0dd1222f5d281a9595b2c971b932c35aded2690a9d82a461add3d796571ed38c

See more details on using hashes here.

File details

Details for the file apispec_plugins-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: apispec_plugins-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.12 Darwin/20.5.0

File hashes

Hashes for apispec_plugins-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c8bca3ba46b55b474586ce1e3a466cfc1e7a69e0b21de956ab611cf26f2fdc42
MD5 57d332002367f3ab179b0817b068537f
BLAKE2b-256 0696ce629c478349cb56445f81e46a6e4f822e88fc14590fdd43b3896c52fa27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page