Skip to main content

chalice-spec

Python package Code style: black

Chalice × APISpec × Pydantic plug-ins

Combines the power of Chalice, APISpec, and Pydantic to make AWS Chalice apps easily documented

Installation

First, add chalice-spec:

poetry add chalice_spec

We consider Chalice, APISpec, and Pydantic "peer dependencies." We only include them as dev dependencies in our codebase, and you may need to install them in yours if you haven't already.

poetry add chalice apispec pydantic

Setup

chalice-spec provides a subclass of the main Chalice class, called ChaliceWithSpec. Here is an example of how to get started:

Before:

from chalice import Chalice

app = Chalice(app_name="hello_world")

After:

from chalice_spec import ChaliceWithSpec, PydanticPlugin
from apispec import APISpec

spec = APISpec(...,
               plugins=[PydanticPlugin()])

app = ChaliceWithSpec(app_name="hello_world", spec=spec)

If you use

ChaliceWithSpec(..., generate_default_docs=True)

the plugin will generate empty docs (with empty request and response schemas) for every endpoint that you've defined in your app. This can be useful as a starting point / overview while developing.

Usage

To document your API, use your existing Pydantic models and add kwargs to Chalice decorators.

Before:

@app.route('/', methods=["POST"])
def example():
    body = MySchema.parse_obj(app.current_request.json_body)

After:

@app.route('/', methods=["POST"], docs=Docs(
    post=Operation(request=MySchema)
))
def example():
    body = MySchema.parse_obj(app.current_request.json_body)

If you have multiple methods supported, you may have something like:

@app.route('/', methods=["POST", "PUT"],
           docs=Docs(
               post=Operation(request=MyCreateSchema, response=MyReadSchema),
               put=Operation(request=MyEditSchema, response=MyReadSchema)
           )
def example():
    # code goes here
    pass

Auto-Generation

Default Empty Docs

If you use:

ChalicePlugin(generate_default_docs=True)

the plugin will generate empty docs (with empty request and response schemas) for every endpoint that you've defined in your app. This can be useful as a starting point / overview while developing.

Path Parameters

These are inferred from the path itself. Any identifiers inside curly braces in a path is added as a string path parameter for that path. e.g. for the path /users/{id}/friends/{f_id}, the path parameters id and f_id will be added to the spec.

To disable this behaviour, define your own parameters or set them to an empty list:

Operation(request=MySchema, response=MyOtherSchema, parameters=[])

Tags

Tags are used in things like Swagger to group endpoints into logical sets. If you don't supply any tags, chalice-spec will add a tag for each endpoint that is the first segment of the path. e.g. /users, /users/{id}/friends, and /users/{id}/posts will all be tagged with users.

To disable this behaviour, define tags in your operation (either with the tags you want, or an empty list):

Operation(request=MySchema, response=MyOtherSchema, tags=[])

Summary and Description

Endpoint summaries and descriptions are inferred from the route docstring. The first line of the docstring is used as the summary, and all other lines become the description:

@app.route('/users/{id}', methods=['GET'], docs=Docs(response=UserSchema))
def get_user(id):
    """
    Retrieve a user object.
    User's can't retrieve other users using this endpoint - only themselves.
    """

To disable this behaviour, you can define your own summary/description or set them to empty strings:

Operation(request=MySchema, response=MyOtherSchema, summary='', description='')

API

  • TODO: this section coming soon!

Metadata

Release files for chalice-spec 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chalice-spec 0.7.0
File Size Uploaded
chalice_spec-0.7.0.tar.gz 10.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chalice-spec 0.7.0
File Interpreter ABI Platform
chalice_spec-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.5 kB

Release files / chalice_spec-0.7.0.tar.gz

Download URL chalice_spec-0.7.0.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0601850b6fa10f1834e3036115d920d14d31c4871431b0bc11971a810fb459c6
BLAKE2b-256 checksum
How to use checksums
9f9e9b59a6d8547150461e926d862f6db60d7322c002cbe76d8827cd795aae5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.11.0 Darwin/22.5.0

Release files / chalice_spec-0.7.0-py3-none-any.whl

Download URL chalice_spec-0.7.0-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9cebb7686e7555d348568f72e7a0ca2b3c400857226d7cfe003342e1e867cf2
BLAKE2b-256 checksum
How to use checksums
110bd756d40277a1eebd69ee2760ea7948c48932d875d0ef9d37c3dc1bb745f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.11.0 Darwin/22.5.0

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

1 release file

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page