从类型生成模式并验证用户输入
项目描述
为aiohttp.web生成API模式并验证输入。
安装
从PyPI安装
pip install aiohttp-apischema
开发
安装要求和启动测试
pip install -r requirements-dev.txt pytest
基本用法
首先创建一个SchemaGenerator实例
from aiohttp_apischema import SchemaGenerator
SCHEMA = SchemaGenerator()
然后装饰应包含在模式中的端点处理程序
from aiohttp_apischema import APIResponse
@SCHEMA.api()
async def foo(request: web.Request) -> APIResponse[list[str], Literal[200]]:
return APIResponse(["foo"])
或者对于基于类的视图
@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)
然后构建您的应用程序时调用设置方法
app = web.Application()
app.router.add_get("/foo", foo)
app.router.add_view(r"/bar/{id:\d+}", Handler)
SCHEMA.setup(app)
您现在可以在/swagger/路径下查看文档。
验证用法
通过添加位置参数实现请求体的验证
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()))
许可证
aiohttp_apischema在Apache 2许可证下提供。
项目详情
下载文件
下载适合您平台文件的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源代码发行版
aiohttp_apischema-0.0.1.tar.gz (3.1 MB 查看哈希值)
构建发行版
关闭
aiohttp_apischema-0.0.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ad7f86395a7136ad11c7422de304ef0d309d0b551dd512fe3379afb179c490a6 |
|
MD5 | 30d4e184dfc71c2c0ffd6ca9fe4b638a |
|
BLAKE2b-256 | b725d20cc5121417c0e2e3b4b5513d689f5e5ee03b2649c2b3776c70a3fd5815 |
关闭
aiohttp_apischema-0.0.1-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 24b3b3bc0ef53023f1b8b17cd4e994009d27ad858e9d96a082d0ce0af776e55c |
|
MD5 | b710ee0718900371ce236aeb34e38bbd |
|
BLAKE2b-256 | 309c3ed5d77bc444ba56873372d23c1e1f641e1bdbcfca0a4973ea5b86da396d |