response.py 685 B

123456789101112131415161718192021
  1. from __future__ import annotations
  2. from typing import Any, Optional
  3. from quart import jsonify
  4. class R:
  5. """统一 JSON 响应构建器。"""
  6. @staticmethod
  7. def ok(data: Any = None, message: str = "success"):
  8. return jsonify({"code": 0, "message": message, "data": data})
  9. @staticmethod
  10. def fail(code: int = -1, message: str = "error", data: Any = None, http_status: int = 200):
  11. return jsonify({"code": code, "message": message, "data": data}), http_status
  12. @staticmethod
  13. def error(message: str = "Internal Server Error", http_status: int = 500):
  14. return jsonify({"code": http_status, "message": message, "data": None}), http_status