Files
webhook/response.py

17 lines
580 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""规范化响应工具:统一返回 {"code": 0, "msg": "", "data": ...} 格式。"""
from aiohttp import web
def ok(data=None, *, msg: str = "ok", status: int = 200) -> web.Response:
"""成功响应code=0。"""
return web.json_response({"code": 0, "msg": msg, "data": data}, status=status)
def error(msg: str, *, code: int = 1, status: int | None = None) -> web.Response:
"""错误响应code 非 0。"""
return web.json_response(
{"code": code, "msg": msg, "data": None},
status=status or (code if 400 <= code < 600 else 400),
)