Generate a schema and validate user input from types
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aiohttp_apischema-0.0.1.tar.gz
.
File metadata
- Download URL: aiohttp_apischema-0.0.1.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad7f86395a7136ad11c7422de304ef0d309d0b551dd512fe3379afb179c490a6 |
|
MD5 | 30d4e184dfc71c2c0ffd6ca9fe4b638a |
|
BLAKE2b-256 | b725d20cc5121417c0e2e3b4b5513d689f5e5ee03b2649c2b3776c70a3fd5815 |
File details
Details for the file aiohttp_apischema-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: aiohttp_apischema-0.0.1-py3-none-any.whl
- Upload date:
- Size: 3.1 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24b3b3bc0ef53023f1b8b17cd4e994009d27ad858e9d96a082d0ce0af776e55c |
|
MD5 | b710ee0718900371ce236aeb34e38bbd |
|
BLAKE2b-256 | 309c3ed5d77bc444ba56873372d23c1e1f641e1bdbcfca0a4973ea5b86da396d |