Files
fastapi-best-architecture/backend/common/response/response_schema.py
T
Wu Clan 5e438c685d Refactor the backend architecture (#299)
* define the basic architecture

* Update script and deployment file locations

* Update the route registration

* Fix CI download dependencies

* Updated ruff to 0.3.3

* Update app subdirectory naming

* Update the model import

* fix pre-commit pdm lock

* Update the service directory naming

* Add CRUD method documents

* Fix the issue of circular import

* Update the README document

* Update the SQL statement for create tables

* Update docker scripts and documentation

* Fix docker scripts

* Update the backend README.md

* Add the security folder and move the redis client

* Update the configuration item

* Fix environment configuration reads

* Update the default configuration

* Updated README description

* Updated the user registration API

* Fix test cases

* Update the celery configuration

* Update and fix celery configuration

* Updated the celery structure

* Update celery tasks and api

* Add celery flower

* Update the import style

* Update contributors
2024-03-22 18:16:15 +08:00

90 lines
2.4 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from datetime import datetime
from typing import Any
from pydantic import BaseModel, ConfigDict
from backend.common.response.response_code import CustomResponse, CustomResponseCode
from backend.core.conf import settings
_ExcludeData = set[int | str] | dict[int | str, Any]
__all__ = ['ResponseModel', 'response_base']
class ResponseModel(BaseModel):
"""
统一返回模型
E.g. ::
@router.get('/test', response_model=ResponseModel)
def test():
return ResponseModel(data={'test': 'test'})
@router.get('/test')
def test() -> ResponseModel:
return ResponseModel(data={'test': 'test'})
@router.get('/test')
def test() -> ResponseModel:
res = CustomResponseCode.HTTP_200
return ResponseModel(code=res.code, msg=res.msg, data={'test': 'test'})
"""
# TODO: json_encoders 配置失效: https://github.com/tiangolo/fastapi/discussions/10252
model_config = ConfigDict(json_encoders={datetime: lambda x: x.strftime(settings.DATETIME_FORMAT)})
code: int = CustomResponseCode.HTTP_200.code
msg: str = CustomResponseCode.HTTP_200.msg
data: Any | None = None
class ResponseBase:
"""
统一返回方法
.. tip::
此类中的方法将返回 ResponseModel 模型,作为一种编码风格而存在;
E.g. ::
@router.get('/test')
def test() -> ResponseModel:
return await response_base.success(data={'test': 'test'})
"""
@staticmethod
async def __response(*, res: CustomResponseCode | CustomResponse = None, data: Any | None = None) -> ResponseModel:
"""
请求成功返回通用方法
:param res: 返回信息
:param data: 返回数据
:return:
"""
return ResponseModel(code=res.code, msg=res.msg, data=data)
async def success(
self,
*,
res: CustomResponseCode | CustomResponse = CustomResponseCode.HTTP_200,
data: Any | None = None,
) -> ResponseModel:
return await self.__response(res=res, data=data)
async def fail(
self,
*,
res: CustomResponseCode | CustomResponse = CustomResponseCode.HTTP_400,
data: Any = None,
) -> ResponseModel:
return await self.__response(res=res, data=data)
response_base = ResponseBase()