sycommon-python-lib 0.1.6__tar.gz → 0.1.8__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.

Potentially problematic release.


This version of sycommon-python-lib might be problematic. Click here for more details.

Files changed (50) hide show
  1. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/PKG-INFO +1 -2
  2. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/pyproject.toml +1 -2
  3. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/middleware/traceid.py +1 -1
  4. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/synacos/feign.py +76 -86
  5. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon_python_lib.egg-info/PKG-INFO +1 -2
  6. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon_python_lib.egg-info/requires.txt +0 -1
  7. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/README.md +0 -0
  8. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/setup.cfg +0 -0
  9. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/__init__.py +0 -0
  10. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/config/Config.py +0 -0
  11. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/config/DatabaseConfig.py +0 -0
  12. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/config/EmbeddingConfig.py +0 -0
  13. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/config/LLMConfig.py +0 -0
  14. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/config/MQConfig.py +0 -0
  15. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/config/RerankerConfig.py +0 -0
  16. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/config/__init__.py +0 -0
  17. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/database/base_db_service.py +0 -0
  18. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/database/database_service.py +0 -0
  19. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/health/__init__.py +0 -0
  20. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/health/health_check.py +0 -0
  21. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/health/ping.py +0 -0
  22. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/logging/__init__.py +0 -0
  23. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/logging/kafka_log.py +0 -0
  24. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/logging/logger_wrapper.py +0 -0
  25. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/middleware/__init__.py +0 -0
  26. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/middleware/context.py +0 -0
  27. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/middleware/cors.py +0 -0
  28. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/middleware/exception.py +0 -0
  29. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/middleware/middleware.py +0 -0
  30. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/middleware/monitor_memory.py +0 -0
  31. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/middleware/mq.py +0 -0
  32. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/middleware/timeout.py +0 -0
  33. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/models/__init__.py +0 -0
  34. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/models/base_http.py +0 -0
  35. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/models/log.py +0 -0
  36. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/models/mqlistener_config.py +0 -0
  37. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/models/mqmsg_model.py +0 -0
  38. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/models/mqsend_config.py +0 -0
  39. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/models/sso_user.py +0 -0
  40. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/rabbitmq/rabbitmq_client.py +0 -0
  41. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/rabbitmq/rabbitmq_service.py +0 -0
  42. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/services.py +0 -0
  43. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/synacos/__init__.py +0 -0
  44. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/synacos/nacos_service.py +0 -0
  45. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/tools/__init__.py +0 -0
  46. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/tools/snowflake.py +0 -0
  47. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon/tools/timing.py +0 -0
  48. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon_python_lib.egg-info/SOURCES.txt +0 -0
  49. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon_python_lib.egg-info/dependency_links.txt +0 -0
  50. {sycommon_python_lib-0.1.6 → sycommon_python_lib-0.1.8}/src/sycommon_python_lib.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sycommon-python-lib
3
- Version: 0.1.6
3
+ Version: 0.1.8
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -8,7 +8,6 @@ Requires-Dist: aio-pika>=9.5.7
8
8
  Requires-Dist: aiohttp>=3.12.15
9
9
  Requires-Dist: decorator>=5.2.1
10
10
  Requires-Dist: fastapi>=0.116.1
11
- Requires-Dist: kafka>=1.3.5
12
11
  Requires-Dist: kafka-python>=2.2.15
13
12
  Requires-Dist: loguru>=0.7.3
14
13
  Requires-Dist: mysql-connector-python>=9.4.0
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sycommon-python-lib"
3
- version = "0.1.6"
3
+ version = "0.1.8"
4
4
  description = "Add your description here"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
@@ -9,7 +9,6 @@ dependencies = [
9
9
  "aiohttp>=3.12.15",
10
10
  "decorator>=5.2.1",
11
11
  "fastapi>=0.116.1",
12
- "kafka>=1.3.5",
13
12
  "kafka-python>=2.2.15",
14
13
  "loguru>=0.7.3",
15
14
  "mysql-connector-python>=9.4.0",
@@ -95,7 +95,7 @@ def setup_trace_id_handler(app):
95
95
  if "application/json" in content_type and not content_disposition.startswith("attachment"):
96
96
  try:
97
97
  data = json.loads(response_body)
98
- data["trace_id"] = trace_id
98
+ data["traceId"] = trace_id
99
99
  new_body = json.dumps(
100
100
  data, ensure_ascii=False).encode()
101
101
  response = Response(
@@ -47,8 +47,6 @@ from sycommon.synacos.nacos_service import NacosService
47
47
 
48
48
 
49
49
  def feign_client(service_name: str, path_prefix: str = "", default_timeout: float | None = None):
50
- # Feign风格客户端装饰器
51
- """声明式HTTP客户端装饰器"""
52
50
  def decorator(cls):
53
51
  class FeignWrapper:
54
52
  def __init__(self):
@@ -66,10 +64,17 @@ def feign_client(service_name: str, path_prefix: str = "", default_timeout: floa
66
64
  request_meta = getattr(func, "_feign_meta", {})
67
65
  method = request_meta.get("method", "GET")
68
66
  path = request_meta.get("path", "")
69
- headers = request_meta.get("headers", {})
67
+ headers = request_meta.get(
68
+ "headers", {}).copy() # 复制 headers 避免修改原对象
70
69
 
71
70
  timeout = kwargs.pop('timeout', self.default_timeout)
72
71
 
72
+ # 处理JSON请求的Content-Type
73
+ is_json_request = method.upper() in [
74
+ "POST", "PUT", "PATCH"] and not request_meta.get("files")
75
+ if is_json_request and "Content-Type" not in headers:
76
+ headers["Content-Type"] = "application/json"
77
+
73
78
  # 获取版本信息
74
79
  version = headers.get('s-y-version')
75
80
 
@@ -94,6 +99,7 @@ def feign_client(service_name: str, path_prefix: str = "", default_timeout: floa
94
99
 
95
100
  SYLogger.info(
96
101
  f"nacos:调用服务: {self.service_name} -> {url}")
102
+ SYLogger.info(f"nacos:请求头: {headers}")
97
103
 
98
104
  # 构建请求
99
105
  params = request_meta.get("params", {})
@@ -127,7 +133,7 @@ def feign_client(service_name: str, path_prefix: str = "", default_timeout: floa
127
133
  ) as response:
128
134
  return await self._handle_response(response)
129
135
  else:
130
- # 普通请求
136
+ # 普通请求(JSON)
131
137
  async with self.session.request(
132
138
  method=method,
133
139
  url=url,
@@ -163,12 +169,12 @@ def feign_client(service_name: str, path_prefix: str = "", default_timeout: floa
163
169
 
164
170
 
165
171
  def feign_request(method: str, path: str, headers: dict = None):
166
- # Feign请求方法装饰器
167
172
  def decorator(func):
173
+ # 初始化请求元数据,确保headers是可修改的字典
168
174
  func._feign_meta = {
169
175
  "method": method.upper(),
170
176
  "path": path,
171
- "headers": headers or {}
177
+ "headers": headers.copy() if headers else {}
172
178
  }
173
179
  return func
174
180
  return decorator
@@ -193,23 +199,23 @@ def feign_upload(field_name: str = "file"):
193
199
  async def feign(service_name, api_path, method='GET', params=None, headers=None, file_path=None,
194
200
  path_params=None, body=None, files=None, form_data=None, timeout=None):
195
201
  """
196
- feign 函数,支持 form-data 表单上传文件和其他字段,支持自定义超时时间
197
-
198
- 参数:
199
- files: 字典,用于上传文件,格式: {'field_name': (filename, file_content)}
200
- form_data: 字典,用于上传表单字段
201
- timeout: 超时时间(秒),None 表示不设置超时
202
+ feign 函数,显式设置JSON请求的Content-Type头
202
203
  """
203
204
  session = aiohttp.ClientSession()
204
205
  try:
205
- # 获取健康的服务实例
206
- if not headers:
207
- headers = {}
206
+ # 初始化headers,确保是可修改的字典
207
+ headers = headers.copy() if headers else {}
208
+
209
+ # 处理JSON请求的Content-Type
210
+ is_json_request = method.upper() in ["POST", "PUT", "PATCH"] and not (
211
+ files or form_data or file_path)
212
+ if is_json_request and "Content-Type" not in headers:
213
+ headers["Content-Type"] = "application/json"
208
214
 
209
215
  nacos_service = NacosService(None)
210
216
  version = headers.get('s-y-version')
211
217
 
212
- # 使用 discover_services 方法获取服务实例列表
218
+ # 获取服务实例
213
219
  instances = nacos_service.get_service_instances(
214
220
  service_name, version=version)
215
221
  if not instances:
@@ -220,76 +226,63 @@ async def feign(service_name, api_path, method='GET', params=None, headers=None,
220
226
  instance = instances[int(time.time()) % len(instances)]
221
227
 
222
228
  SYLogger.info(f"nacos:开始调用服务: {service_name}")
223
- SYLogger.info(
224
- f"nacos:服务实例信息: IP={instance['ip']}, Port={instance['port']}")
225
-
226
- if instance:
227
- ip = instance.get('ip')
228
- port = instance.get('port')
229
-
230
- SYLogger.info(f"nacos:服务实例信息: IP={ip}, Port={port}")
231
-
232
- # 处理 path 参数
233
- if path_params:
234
- for key, value in path_params.items():
235
- api_path = api_path.replace(f"{{{key}}}", str(value))
236
-
237
- url = f"http://{ip}:{port}{api_path}"
238
- SYLogger.info(f"nacos:请求地址: {url}")
239
-
240
- try:
241
- # 处理文件上传
242
- if files or form_data or file_path:
243
- # 创建表单数据
244
- data = aiohttp.FormData()
245
- if form_data:
246
- for key, value in form_data.items():
247
- data.add_field(key, value)
248
- if files:
249
- for field_name, (filename, content) in files.items():
250
- data.add_field(field_name, content,
251
- filename=filename)
252
- if file_path:
253
- filename = os.path.basename(file_path)
254
- with open(file_path, 'rb') as f:
255
- data.add_field('file', f, filename=filename)
256
- # 移除 Content-Type 头,让 aiohttp 自动设置 boundary
257
- headers.pop('Content-Type', None)
258
- # 发送表单数据
259
- async with session.request(
260
- method=method.upper(),
261
- url=url,
262
- headers=headers,
263
- params=params,
264
- data=data,
265
- timeout=timeout
266
- ) as response:
267
- return await _handle_feign_response(response)
268
- else:
269
- # 普通请求
270
- if body and 'Content-Type' not in headers:
271
- headers['Content-Type'] = 'application/json'
272
- async with session.request(
273
- method=method.upper(),
274
- url=url,
275
- headers=headers,
276
- params=params,
277
- json=body,
278
- timeout=timeout
279
- ) as response:
280
- return await _handle_feign_response(response)
281
- except aiohttp.ClientError as e:
282
- SYLogger.error(
283
- f"nacos:请求服务接口时出错ClientError: {e}")
284
- print(f"请求服务接口时出错: {e}")
285
- return None
229
+ SYLogger.info(f"nacos:请求头: {headers}")
230
+
231
+ ip = instance.get('ip')
232
+ port = instance.get('port')
233
+
234
+ # 处理path参数
235
+ if path_params:
236
+ for key, value in path_params.items():
237
+ api_path = api_path.replace(f"{{{key}}}", str(value))
238
+
239
+ url = f"http://{ip}:{port}{api_path}"
240
+ SYLogger.info(f"nacos:请求地址: {url}")
241
+
242
+ try:
243
+ # 处理文件上传
244
+ if files or form_data or file_path:
245
+ data = aiohttp.FormData()
246
+ if form_data:
247
+ for key, value in form_data.items():
248
+ data.add_field(key, value)
249
+ if files:
250
+ for field_name, (filename, content) in files.items():
251
+ data.add_field(field_name, content, filename=filename)
252
+ if file_path:
253
+ filename = os.path.basename(file_path)
254
+ with open(file_path, 'rb') as f:
255
+ data.add_field('file', f, filename=filename)
256
+ # 移除Content-Type,让aiohttp自动处理
257
+ headers.pop('Content-Type', None)
258
+ async with session.request(
259
+ method=method.upper(),
260
+ url=url,
261
+ headers=headers,
262
+ params=params,
263
+ data=data,
264
+ timeout=timeout
265
+ ) as response:
266
+ return await _handle_feign_response(response)
267
+ else:
268
+ # 普通JSON请求
269
+ async with session.request(
270
+ method=method.upper(),
271
+ url=url,
272
+ headers=headers,
273
+ params=params,
274
+ json=body,
275
+ timeout=timeout
276
+ ) as response:
277
+ return await _handle_feign_response(response)
278
+ except aiohttp.ClientError as e:
279
+ SYLogger.error(f"nacos:请求服务接口时出错ClientError: {e}")
280
+ return None
286
281
  except Exception as e:
287
282
  import traceback
288
283
  SYLogger.error(f"nacos:请求服务接口时出错: {traceback.format_exc()}")
289
- print(f"nacos:请求服务接口时出错: {traceback.format_exc()}")
290
284
  return None
291
285
  finally:
292
- SYLogger.info(f"nacos:结束调用服务: {service_name}, {api_path}")
293
286
  await session.close()
294
287
 
295
288
 
@@ -300,12 +293,9 @@ async def _handle_feign_response(response):
300
293
  if 'application/json' in content_type:
301
294
  return await response.json()
302
295
  else:
303
- # 如果是文件流,将其封装成 io.BytesIO 对象返回
304
296
  content = await response.read()
305
297
  return io.BytesIO(content)
306
298
  else:
307
- print(
308
- f"nacos:请求失败,状态码: {response.status},响应内容: {await response.text()}")
309
- SYLogger.error(
310
- f"nacos:请求服务接口时出错: {response.status}, 响应内容: {await response.text()}")
299
+ error_msg = await response.text()
300
+ SYLogger.error(f"nacos:请求失败,状态码: {response.status},响应内容: {error_msg}")
311
301
  return None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sycommon-python-lib
3
- Version: 0.1.6
3
+ Version: 0.1.8
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -8,7 +8,6 @@ Requires-Dist: aio-pika>=9.5.7
8
8
  Requires-Dist: aiohttp>=3.12.15
9
9
  Requires-Dist: decorator>=5.2.1
10
10
  Requires-Dist: fastapi>=0.116.1
11
- Requires-Dist: kafka>=1.3.5
12
11
  Requires-Dist: kafka-python>=2.2.15
13
12
  Requires-Dist: loguru>=0.7.3
14
13
  Requires-Dist: mysql-connector-python>=9.4.0
@@ -2,7 +2,6 @@ aio-pika>=9.5.7
2
2
  aiohttp>=3.12.15
3
3
  decorator>=5.2.1
4
4
  fastapi>=0.116.1
5
- kafka>=1.3.5
6
5
  kafka-python>=2.2.15
7
6
  loguru>=0.7.3
8
7
  mysql-connector-python>=9.4.0