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.1a1.tar.gz (3.1 MB view details)

Uploaded Source

Built Distribution

aiohttp_apischema-0.0.1a1-py3-none-any.whl (3.1 MB view details)

Uploaded Python 3

File details

Details for the file aiohttp_apischema-0.0.1a1.tar.gz.

File metadata

  • Download URL: aiohttp_apischema-0.0.1a1.tar.gz
  • Upload date:
  • Size: 3.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for aiohttp_apischema-0.0.1a1.tar.gz
Algorithm Hash digest
SHA256 db69b321189466983c47636705506adcb108ec01e5d87b70a28cc9a4625d954c
MD5 64b4b787409c09595ac6df9d0c27b5e9
BLAKE2b-256 8777392f7ecfbcbaf6f93ea0694730b705fc0a808cbe4e8b27156b13f8cfebd4

See more details on using hashes here.

File details

Details for the file aiohttp_apischema-0.0.1a1-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_apischema-0.0.1a1-py3-none-any.whl
Algorithm Hash digest
SHA256 55d41aa0befbcf2ef0632a5661b7c7a1d3cd8a3fee712e1599abbf3f9a95b91e
MD5 8c9e32f1ceac58d430ce5c2d3c7ab6cb
BLAKE2b-256 933444c03fce094bf18564554d763b6f32f777aaeab559045c27c11f8ae8adba

See more details on using hashes here.

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