sycommon-python-lib 0.1.1__py3-none-any.whl → 0.1.2__py3-none-any.whl

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.
sycommon/synacos/feign.py CHANGED
@@ -46,7 +46,7 @@ from sycommon.synacos.nacos_service import NacosService
46
46
  # return None
47
47
 
48
48
 
49
- def feign_client(service_name: str, path_prefix: str = ""):
49
+ def feign_client(service_name: str, path_prefix: str = "", default_timeout: float | None = None):
50
50
  # Feign风格客户端装饰器
51
51
  """声明式HTTP客户端装饰器"""
52
52
  def decorator(cls):
@@ -56,6 +56,7 @@ def feign_client(service_name: str, path_prefix: str = ""):
56
56
  self.nacos_manager = NacosService(None)
57
57
  self.path_prefix = path_prefix
58
58
  self.session = aiohttp.ClientSession()
59
+ self.default_timeout = default_timeout
59
60
 
60
61
  def __getattr__(self, name):
61
62
  func = getattr(cls, name)
@@ -67,6 +68,8 @@ def feign_client(service_name: str, path_prefix: str = ""):
67
68
  path = request_meta.get("path", "")
68
69
  headers = request_meta.get("headers", {})
69
70
 
71
+ timeout = kwargs.pop('timeout', self.default_timeout)
72
+
70
73
  # 获取版本信息
71
74
  version = headers.get('s-y-version')
72
75
 
@@ -120,7 +123,7 @@ def feign_client(service_name: str, path_prefix: str = ""):
120
123
  headers=headers,
121
124
  params=params,
122
125
  data=data,
123
- timeout=10
126
+ timeout=timeout
124
127
  ) as response:
125
128
  return await self._handle_response(response)
126
129
  else:
@@ -131,7 +134,7 @@ def feign_client(service_name: str, path_prefix: str = ""):
131
134
  headers=headers,
132
135
  params=params,
133
136
  json=body,
134
- timeout=10
137
+ timeout=timeout
135
138
  ) as response:
136
139
  return await self._handle_response(response)
137
140
  except Exception as e:
@@ -188,15 +191,15 @@ def feign_upload(field_name: str = "file"):
188
191
 
189
192
 
190
193
  async def feign(service_name, api_path, method='GET', params=None, headers=None, file_path=None,
191
- path_params=None, request=None, body=None, files=None, form_data=None):
194
+ path_params=None, body=None, files=None, form_data=None, timeout=None):
192
195
  """
193
- feign 函数,支持 form-data 表单上传文件和其他字段
196
+ feign 函数,支持 form-data 表单上传文件和其他字段,支持自定义超时时间
194
197
 
195
198
  参数:
196
199
  files: 字典,用于上传文件,格式: {'field_name': (filename, file_content)}
197
200
  form_data: 字典,用于上传表单字段
201
+ timeout: 超时时间(秒),None 表示不设置超时
198
202
  """
199
- file_stream = None
200
203
  session = aiohttp.ClientSession()
201
204
  try:
202
205
  # 获取健康的服务实例
@@ -259,7 +262,7 @@ async def feign(service_name, api_path, method='GET', params=None, headers=None,
259
262
  headers=headers,
260
263
  params=params,
261
264
  data=data,
262
- timeout=10
265
+ timeout=timeout
263
266
  ) as response:
264
267
  return await _handle_feign_response(response)
265
268
  else:
@@ -272,7 +275,7 @@ async def feign(service_name, api_path, method='GET', params=None, headers=None,
272
275
  headers=headers,
273
276
  params=params,
274
277
  json=body,
275
- timeout=10
278
+ timeout=timeout
276
279
  ) as response:
277
280
  return await _handle_feign_response(response)
278
281
  except aiohttp.ClientError as e:
@@ -281,8 +284,9 @@ async def feign(service_name, api_path, method='GET', params=None, headers=None,
281
284
  print(f"请求服务接口时出错: {e}")
282
285
  return None
283
286
  except Exception as e:
284
- SYLogger.error(f"nacos:请求服务接口时出错: {e}")
285
- print(f"nacos:请求服务接口时出错: {e}")
287
+ import traceback
288
+ SYLogger.error(f"nacos:请求服务接口时出错: {traceback.format_exc()}")
289
+ print(f"nacos:请求服务接口时出错: {traceback.format_exc()}")
286
290
  return None
287
291
  finally:
288
292
  SYLogger.info(f"nacos:结束调用服务: {service_name}, {api_path}")