Skip to main content

Generate a schema and validate user input from types

Project description

https://github.com/aio-libs/aiohttp-apischema/workflows/CI/badge.svg https://codecov.io/gh/aio-libs/aiohttp-apischema/branch/master/graph/badge.svg https://img.shields.io/pypi/v/aiohttp-apischema.svg https://readthedocs.org/projects/aiohttp-apischema/badge/?version=latest

API schema generation and input validation for aiohttp.web.

Installation

Install from PyPI:

pip install aiohttp-apischema

Developing

Install requirement and launch tests:

pip install -r requirements-dev.txt
pytest

Basic usage

First create a SchemaGenerator instance:

from aiohttp_apischema import SchemaGenerator
SCHEMA = SchemaGenerator()

Then decorate endpoint handlers which should be included in the schema:

from aiohttp_apischema import APIResponse

@SCHEMA.api()
async def foo(request: web.Request) -> APIResponse[list[str], Literal[200]]:
    return APIResponse(["foo"])

Or for Class Based Views:

@SCHEMA.api_view()
class Handler(web.View):
    async def get(self) -> APIResponse[int, Literal[200]] | APIResponse[None, Literal[404]]:
        bar_id = int(self.request.match_info["id"])
        if bar_id == 1:
            return APIResponse(42)
        return APIResponse[None, Literal[404]](None, status=404)

Then call the setup method when building your app:

app = web.Application()
app.router.add_get("/foo", foo)
app.router.add_view(r"/bar/{id:\d+}", Handler)
SCHEMA.setup(app)

You can now view the docs under the /swagger/ path.

Validation usage

Validation of the request body is achieved by adding a positional parameter:

async def handler(request: web.Request, body: dict[int, str]) -> APIResponse[int, Literal[200]]:
    # body has been validated, so we can be sure the keys are int now.
    return APIResponse(sum(body.keys()))

License

aiohttp_apischema is offered under the Apache 2 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

aiohttp_apischema-0.0.2.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

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

aiohttp_apischema-0.0.2-py3-none-any.whl (1.2 MB view details)

Uploaded Python 3

File details

Details for the file aiohttp_apischema-0.0.2.tar.gz.

File metadata

  • Download URL: aiohttp_apischema-0.0.2.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for aiohttp_apischema-0.0.2.tar.gz
Algorithm Hash digest
SHA256 983cd2a30217e71220caa112694f527afb817446bb8efaada0552216bb843c8f
MD5 c7432b157e6bb82f39aabec438287c71
BLAKE2b-256 63cf7633ce17f1cc4b0f9c1fd1ba4173709baf9821fcd14762660dfc37e0f398

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiohttp_apischema-0.0.2.tar.gz:

Publisher: ci-cd.yml on aio-libs/aiohttp-apischema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aiohttp_apischema-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_apischema-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 00835c79107c41ed019368c9f4cb1cf0b36f35ca36975a6b93dbddd7158b6c82
MD5 ba6a6aaa7afb6a3c11ed69107a6bfa81
BLAKE2b-256 e0f78251d389e6f26377b6c69f358e9de17983a97b934cd7e5d19d3d999e4ef2

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiohttp_apischema-0.0.2-py3-none-any.whl:

Publisher: ci-cd.yml on aio-libs/aiohttp-apischema

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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