ezKit 3.0.45__tar.gz → 3.0.47__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezKit
3
- Version: 3.0.45
3
+ Version: 3.0.47
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -8,10 +8,10 @@ from os import environ
8
8
  from pathlib import Path
9
9
  from typing import Any, cast
10
10
 
11
- from fastapi import FastAPI, Request
11
+ from fastapi import FastAPI, Request, Response as FastAPIResponse, status
12
12
  from fastapi.exceptions import RequestValidationError
13
13
  from fastapi.middleware.cors import CORSMiddleware
14
- from fastapi.responses import JSONResponse
14
+ from fastapi.responses import FileResponse, JSONResponse
15
15
  from loguru import logger
16
16
  from pydantic import ValidationError
17
17
  from sqlalchemy import and_, case, delete, insert, or_, select, true, update
@@ -142,10 +142,12 @@ class InterceptHandler(logging.Handler):
142
142
 
143
143
 
144
144
  async def generic_api_execute(
145
- operation: Callable[[Any], Awaitable[Any]],
145
+ operation: Callable[[Any], Any | Awaitable[Any]],
146
146
  params_object: Any,
147
147
  params_type: str = "text",
148
- ) -> JSONResponse:
148
+ download_file: bool = False,
149
+ response: FastAPIResponse | None = None,
150
+ ) -> JSONResponse | FileResponse | None:
149
151
  """Generic API Execution"""
150
152
 
151
153
  # param: operation 函数
@@ -161,15 +163,21 @@ async def generic_api_execute(
161
163
  # 即: 直接将 params_object 传递给 operation 函数
162
164
  #
163
165
  # params_type: text(默认), json, form
166
+ #
167
+ # download_file 和 response 必须同时存在
164
168
 
165
169
  try:
166
170
 
167
- if params_type not in {"text", "json", "form"}:
168
- return Response(code=1001, message=f"Invalid parameter type: {params_type}")
169
-
170
171
  if not isinstance(params_object, (str, Request)):
171
172
  return Response(code=1001, message="Invalid parameter type")
172
173
 
174
+ if params_type not in {"text", "json", "form"}:
175
+ return Response(code=1001, message="Invalid parameter type")
176
+
177
+ # download_file 和 response 必须同时存在
178
+ if download_file and response is None:
179
+ return Response(code=1002, message="Missing response parameter")
180
+
173
181
  if isinstance(params_object, Request):
174
182
  if params_type == "json":
175
183
  data = await params_object.json()
@@ -181,14 +189,28 @@ async def generic_api_execute(
181
189
  data = params_object
182
190
 
183
191
  if data in ("", {}, []) or (isinstance(data, FormData) and not data):
184
- return Response(code=1002, message="Parameters cannot be empty")
185
192
 
186
- result = await operation(data)
193
+ # 处理下载文件错误
194
+ if download_file and response is not None:
195
+ response.status_code = status.HTTP_400_BAD_REQUEST
196
+ return None
197
+
198
+ # 处理常规数据错误
199
+ return Response(code=1003, message="Parameters cannot be empty")
200
+
201
+ result = operation(data) if download_file else await operation(data)
187
202
 
188
203
  if result is None:
189
- return Response(code=1003, message="Execution failed. Check the parameters or service.")
190
204
 
191
- return Response(data=result)
205
+ # 处理下载文件错误
206
+ if download_file and response is not None:
207
+ response.status_code = status.HTTP_400_BAD_REQUEST
208
+ return None
209
+
210
+ # 处理常规数据错误
211
+ return Response(code=1004, message="Execution failed. Check the parameters or service.")
212
+
213
+ return cast(FileResponse, result) if download_file else cast(JSONResponse, Response(data=result))
192
214
 
193
215
  except Exception as e:
194
216
 
@@ -197,6 +219,12 @@ async def generic_api_execute(
197
219
  else:
198
220
  logger.error(e)
199
221
 
222
+ # 处理下载文件错误
223
+ if download_file and response is not None:
224
+ response.status_code = status.HTTP_400_BAD_REQUEST
225
+ return None
226
+
227
+ # 处理常规数据错误
200
228
  return Response(code=400, message=str(e))
201
229
 
202
230
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezKit
3
- Version: 3.0.45
3
+ Version: 3.0.47
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -4,7 +4,7 @@ from setuptools import find_packages, setup
4
4
 
5
5
  setup(
6
6
  name="ezKit",
7
- version="3.0.45",
7
+ version="3.0.47",
8
8
  author="septvean",
9
9
  author_email="septvean@gmail.com",
10
10
  description="Easy Kit",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes