Skip to main content

MIT License Python Sanic

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

文档

正在编写中,敬请期待

Metadata

Release files for sanic-api 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-api 0.5.2
File Size Uploaded Provenance Actions
sanic_api-0.5.2.tar.gz 13.2 kB Without provenance Details for sanic_api-0.5.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sanic-api 0.5.2
File Interpreter ABI Platform Actions
sanic_api-0.5.2-py3-none-any.whl Python 3 none any Details for sanic_api-0.5.2-py3-none-any.whl

Total release size: 29.3 kB

Release files / sanic_api-0.5.2.tar.gz

Download URL sanic_api-0.5.2.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f68866b59b3555bcc7edfcca7e365eab44cf762d2b5f10e06283c05216d0a234
BLAKE2b-256 checksum
How to use checksums
89cc40b1a859fcdacd15d3c9045cfa64deca85809a6526b6d87fd064ba54a3cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / sanic_api-0.5.2-py3-none-any.whl

Download URL sanic_api-0.5.2-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2b3ff2152ae1330c395171bfc8398ede730f4c0b0c746583c934bf2696aa1f4
BLAKE2b-256 checksum
How to use checksums
54be46e0f2987a72ef00d0277f34def9e5dcbae87263ba15207d6ae5d51da563
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Security for sanic-api 0.5.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page