Skip to main content

Sanic 框架实用API工具集,拥有自动生成文档、参数校验、配置的导入、日志功能的优化等功能,更好的助力接口的开发

Project description

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

文档

正在编写中,敬请期待

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

sanic_api-0.5.1.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sanic_api-0.5.1-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file sanic_api-0.5.1.tar.gz.

File metadata

  • Download URL: sanic_api-0.5.1.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

Hashes for sanic_api-0.5.1.tar.gz
Algorithm Hash digest
SHA256 b7e5c9a3c44ae6dfbfa2e7f9c5dc34b468a34630dd152f319146e07a9a973cd8
MD5 e14d59bd35f344c83772d312fd8bd53e
BLAKE2b-256 abf1735e192ab06717d55be0ca660361209dff2fa27dae2d6b7b21f9356c341b

See more details on using hashes here.

File details

Details for the file sanic_api-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: sanic_api-0.5.1-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

Hashes for sanic_api-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 02b20b612fb4715265d46d98787ca7d39b930f8b31ef017a7cd38adf65ac9205
MD5 0b4271596624dcab78a0d121b0c25300
BLAKE2b-256 bdeef59cc1515353c7b93fe2a6ed183a0eaa66d92466692c95bff793cab21861

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page