Skip to main content

Pyramid plugin for openapi spec generation

Project description

pyramid_apispec

pyramid_apispec allows you to create an OpenAPI specification file using apispec and an online OpenAPI explorer using the Swagger UI project for your Pyramid application and its marshmallow schemas.

Installation

pip install pyramid_apispec

Basic usage

Check out the demo folder and minimal application example by running:

pip install -e '.[demo]'
python demo/app.py

You can then visit your API explorer page at http://0.0.0.0:6543/api-explorer.

Examples

Visit generated documentation here (please note that actual REST API is not working in github pages)

Hinting a route and its view:

@view_config(route_name='foo_route', renderer='json')
def foo_view():
    """A greeting endpoint.

    ---
    x-extension: value
    get:
        description: some description
        responses:
            200:
                description: response for 200 code
                schema:
                    $ref: #/definitions/BarBodySchema
    """
    return 'hi'

Rendering the spec as JSON response:

from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from pyramid_apispec.helpers import add_pyramid_paths

@view_config(route_name='openapi_spec', renderer='json')
def api_spec(request):
    spec = APISpec(
        title='Some API',
        version='1.0.0',
        plugins=[MarshmallowPlugin()],
    )
    # using marshmallow plugin here
    spec.definition('SomeFooBody', schema=MarshmallowSomeFooBodySchema)

    # inspect the `foo_route` and generate operations from docstring
    add_pyramid_paths(spec, 'foo_route', request=request)

    # inspection supports filtering via pyramid add_view predicate arguments
    add_pyramid_paths(
        spec, 'bar_route', request=request, request_method='post')
    return spec.to_dict()

Adding the API explorer view

To complement the specification file generation, this package can also provide an API explorer for your application's API via the Swagger UI project:

config.include('pyramid_apispec.views')
config.add_route("openapi_spec", "/openapi.json")
config.pyramid_apispec_add_explorer(
    spec_route_name='openapi_spec')

By default you need to pass the route name of the view that serves the OpenAPI specification in your application. If needed you can specify a Pyramid permission or custom callable (script_generator argument) to override the default JavaScript configuration of Swagger UI.

The default URL for the explorer is /api-explorer. This setting is controlled via the explorer_route_path argument - the route is registerd as pyramid_apispec.api_explorer_path.

Running tests

pip install -e '.[dev]'
tox

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

pyramid_apispec-0.3.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

pyramid_apispec-0.3.1-py2.py3-none-any.whl (10.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pyramid_apispec-0.3.1.tar.gz.

File metadata

  • Download URL: pyramid_apispec-0.3.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.7

File hashes

Hashes for pyramid_apispec-0.3.1.tar.gz
Algorithm Hash digest
SHA256 98a1f06e244f6b8522002dedc864685f7f6654fd0d2a2ab365c3edd34f2ed46c
MD5 8953766cf484260949d2e98321de19b3
BLAKE2b-256 c2e06bd37cb757837369458614f5bb38a1966d8acdd1a045e102e2ef4c70a0b9

See more details on using hashes here.

Provenance

File details

Details for the file pyramid_apispec-0.3.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pyramid_apispec-0.3.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.7

File hashes

Hashes for pyramid_apispec-0.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f9a865ba51dddf95173748b30b9ec03abddcd702ab8fcbabedfe9d8e8811b81e
MD5 01e2ea68d2bb9ca5401e4d5c26e9f07f
BLAKE2b-256 dd17d8ff43b68565eb27baca24ca96483bcc6cf7438cf63ce2e32595db6dfcd0

See more details on using hashes here.

Provenance

Supported by

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