ezKit 3.0.45__tar.gz → 3.0.46__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.46
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 函数
@@ -181,14 +183,28 @@ async def generic_api_execute(
181
183
  data = params_object
182
184
 
183
185
  if data in ("", {}, []) or (isinstance(data, FormData) and not data):
186
+
187
+ # 处理下载文件错误
188
+ if download_file and response is not None:
189
+ response.status_code = status.HTTP_400_BAD_REQUEST
190
+ return None
191
+
192
+ # 处理常规数据错误
184
193
  return Response(code=1002, message="Parameters cannot be empty")
185
194
 
186
- result = await operation(data)
195
+ result = operation(data) if download_file else await operation(data)
187
196
 
188
197
  if result is None:
198
+
199
+ # 处理下载文件错误
200
+ if download_file and response is not None:
201
+ response.status_code = status.HTTP_400_BAD_REQUEST
202
+ return None
203
+
204
+ # 处理常规数据错误
189
205
  return Response(code=1003, message="Execution failed. Check the parameters or service.")
190
206
 
191
- return Response(data=result)
207
+ return cast(FileResponse, result) if download_file else cast(JSONResponse, Response(data=result))
192
208
 
193
209
  except Exception as e:
194
210
 
@@ -197,6 +213,12 @@ async def generic_api_execute(
197
213
  else:
198
214
  logger.error(e)
199
215
 
216
+ # 处理下载文件错误
217
+ if download_file and response is not None:
218
+ response.status_code = status.HTTP_400_BAD_REQUEST
219
+ return None
220
+
221
+ # 处理常规数据错误
200
222
  return Response(code=400, message=str(e))
201
223
 
202
224
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezKit
3
- Version: 3.0.45
3
+ Version: 3.0.46
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.46",
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