Sanic-API
让您的sanic服务程序更好的支持API文档、参数校验、日志打印、响应规范等
特性
-
无需任何多余改动,全自动生成openapi文档,使用更加方便 (新版方案正在积极优化开发中)
-
基于
pydantic的参数校验器,让接口的请求及响应更符合你的预期,使用更方便 -
使用
loguru库代替官方logging日志库,并对访问日志进行扩展,支持写入文件及推送loki -
使用了基于
pydantic-settings的项目配置方案,支持json、yml、ini、.env等多种格式 -
对sanic的启动进行了简单的封装,可快速启动项目
截图
路线图
-
全自动生成openapi文档
-
编写详细文档
安装
使用 pip 安装 sanic-api
pip install sanic-api
使用方法/示例
最小示例
from sanic_api.app import BaseApp
class App(BaseApp):
"""
最小的sanic服务示例
"""
if __name__ == "__main__":
App.run()
带参数校验的示例
from pydantic import BaseModel, Field
from sanic import Blueprint, Sanic, json
from sanic.log import logger
from sanic_api.api import BaseRespTml, Request
from sanic_api.app import BaseApp
user_blueprint = Blueprint("user", "/user")
class UserInfoModel(BaseModel):
user_id: int = Field(title="用户ID")
class UserInfoResponse(BaseRespTml):
user_name: str = Field(title="用户名")
class UseLoginRequest(Request):
form_data: UserInfoModel
@user_blueprint.post("info")
async def user_info(request: Request, json_data: UserInfoModel):
"""
获取用户信息
"""
logger.info(f"data: {json_data}")
info = UserInfoResponse(user_name="张三")
info.temp_data.code = "0000"
info.temp_data.msg = "查询成功"
return info.resp()
@user_blueprint.post("login")
async def user_login(request: UseLoginRequest):
"""
用户登录
"""
logger.info(f"user_id: {request.form_data.user_id}")
return json(request.form_data.model_dump())
class App(BaseApp):
"""
服务示例
"""
async def setup_route(self, app: Sanic):
api = Blueprint.group(url_prefix="api")
api.append(user_blueprint)
app.blueprint(api)
if __name__ == "__main__":
App.run()
开发
要部署这个项目,请先安装rye
rye sync
文档
正在编写中,敬请期待
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sanic_api-0.5.2.tar.gz
(13.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
sanic_api-0.5.2-py3-none-any.whl
(16.1 kB
view details)
File details
Details for the file sanic_api-0.5.2.tar.gz.
File metadata
- Download URL: sanic_api-0.5.2.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f68866b59b3555bcc7edfcca7e365eab44cf762d2b5f10e06283c05216d0a234
|
|
| MD5 |
e666d73a18b7625d3be0d2985d253fb1
|
|
| BLAKE2b-256 |
89cc40b1a859fcdacd15d3c9045cfa64deca85809a6526b6d87fd064ba54a3cb
|
File details
Details for the file sanic_api-0.5.2-py3-none-any.whl.
File metadata
- Download URL: sanic_api-0.5.2-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2b3ff2152ae1330c395171bfc8398ede730f4c0b0c746583c934bf2696aa1f4
|
|
| MD5 |
6fb10731481cc2c0b174afa5110d576d
|
|
| BLAKE2b-256 |
54be46e0f2987a72ef00d0277f34def9e5dcbae87263ba15207d6ae5d51da563
|