mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 21:15:13 +00:00
Simplify custom response status codes (#686)
This commit is contained in:
@@ -8,7 +8,7 @@ from starlette.responses import StreamingResponse
|
||||
|
||||
from backend.app.admin.service.plugin_service import plugin_service
|
||||
from backend.common.enums import PluginType
|
||||
from backend.common.response.response_code import CustomResponseCode
|
||||
from backend.common.response.response_code import CustomResponse
|
||||
from backend.common.response.response_schema import ResponseModel, ResponseSchemaModel, response_base
|
||||
from backend.common.security.jwt import DependsJwtAuth
|
||||
from backend.common.security.permission import RequestPermission
|
||||
@@ -44,7 +44,9 @@ async def install_plugin(
|
||||
repo_url: Annotated[str | None, Query(description='插件 git 仓库地址')] = None,
|
||||
) -> ResponseModel:
|
||||
await plugin_service.install(type=type, file=file, repo_url=repo_url)
|
||||
return response_base.success(res=CustomResponseCode.PLUGIN_INSTALL_SUCCESS)
|
||||
return response_base.success(
|
||||
res=CustomResponse(code=200, msg='插件安装成功,请根据插件说明(README.md)进行相关配置并重启服务')
|
||||
)
|
||||
|
||||
|
||||
@router.delete(
|
||||
@@ -58,7 +60,9 @@ async def install_plugin(
|
||||
)
|
||||
async def uninstall_plugin(plugin: Annotated[str, Path(description='插件名称')]) -> ResponseModel:
|
||||
await plugin_service.uninstall(plugin=plugin)
|
||||
return response_base.success(res=CustomResponseCode.PLUGIN_UNINSTALL_SUCCESS)
|
||||
return response_base.success(
|
||||
res=CustomResponse(code=200, msg='插件卸载成功,请根据插件说明(README.md)移除相关配置并重启服务')
|
||||
)
|
||||
|
||||
|
||||
@router.put(
|
||||
|
||||
@@ -76,7 +76,7 @@ async def _validation_exception_handler(request: Request, exc: RequestValidation
|
||||
}
|
||||
request.state.__request_validation_exception__ = content # 用于在中间件中获取异常信息
|
||||
content.update(trace_id=get_request_trace_id(request))
|
||||
return MsgSpecJSONResponse(status_code=422, content=content)
|
||||
return MsgSpecJSONResponse(status_code=StandardResponseCode.HTTP_422, content=content)
|
||||
|
||||
|
||||
def register_exception(app: FastAPI):
|
||||
|
||||
@@ -23,25 +23,8 @@ class CustomResponseCode(CustomCodeBase):
|
||||
"""自定义响应状态码"""
|
||||
|
||||
HTTP_200 = (200, '请求成功')
|
||||
HTTP_201 = (201, '新建请求成功')
|
||||
HTTP_202 = (202, '请求已接受,但处理尚未完成')
|
||||
HTTP_204 = (204, '请求成功,但没有返回内容')
|
||||
HTTP_400 = (400, '请求错误')
|
||||
HTTP_401 = (401, '未经授权')
|
||||
HTTP_403 = (403, '禁止访问')
|
||||
HTTP_404 = (404, '请求的资源不存在')
|
||||
HTTP_410 = (410, '请求的资源已永久删除')
|
||||
HTTP_422 = (422, '请求参数非法')
|
||||
HTTP_425 = (425, '无法执行请求,由于服务器无法满足要求')
|
||||
HTTP_429 = (429, '请求过多,服务器限制')
|
||||
HTTP_500 = (500, '服务器内部错误')
|
||||
HTTP_502 = (502, '网关错误')
|
||||
HTTP_503 = (503, '服务器暂时无法处理请求')
|
||||
HTTP_504 = (504, '网关超时')
|
||||
|
||||
# Plugin
|
||||
PLUGIN_INSTALL_SUCCESS = (200, '插件安装成功,请根据插件说明(README.md)进行相关配置并重启服务')
|
||||
PLUGIN_UNINSTALL_SUCCESS = (200, '插件卸载成功,请根据插件说明(README.md)移除相关配置并重启服务')
|
||||
|
||||
|
||||
class CustomErrorCode(CustomCodeBase):
|
||||
|
||||
@@ -12,6 +12,7 @@ from fastapi.routing import APIRoute
|
||||
|
||||
from backend.common.exception import errors
|
||||
from backend.common.log import log
|
||||
from backend.common.response.response_code import StandardResponseCode
|
||||
|
||||
|
||||
def ensure_unique_route_names(app: FastAPI) -> None:
|
||||
@@ -39,7 +40,9 @@ async def http_limit_callback(request: Request, response: Response, expire: int)
|
||||
:return:
|
||||
"""
|
||||
expires = ceil(expire / 1000)
|
||||
raise errors.HTTPError(code=429, msg='请求过于频繁,请稍后重试', headers={'Retry-After': str(expires)})
|
||||
raise errors.HTTPError(
|
||||
code=StandardResponseCode.HTTP_429, msg='请求过于频繁,请稍后重试', headers={'Retry-After': str(expires)}
|
||||
)
|
||||
|
||||
|
||||
def timer(func) -> Callable:
|
||||
|
||||
Reference in New Issue
Block a user