huace-aigc-auth-client 1.1.19__tar.gz → 1.1.20__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.
- {huace_aigc_auth_client-1.1.19/huace_aigc_auth_client.egg-info → huace_aigc_auth_client-1.1.20}/PKG-INFO +1 -1
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client/__init__.py +1 -1
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client/api_stats_collector.py +9 -10
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client/sdk.py +108 -11
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20/huace_aigc_auth_client.egg-info}/PKG-INFO +1 -1
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/pyproject.toml +1 -1
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/LICENSE +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/MANIFEST.in +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/QUICK_START.txt +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/README.md +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client/legacy_adapter.py +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client/user_context.py +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client/webhook.py +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client/webhook_flask.py +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client.egg-info/SOURCES.txt +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client.egg-info/dependency_links.txt +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client.egg-info/requires.txt +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client.egg-info/top_level.txt +0 -0
- {huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/setup.cfg +0 -0
|
@@ -66,8 +66,8 @@ class ApiStatsCollector:
|
|
|
66
66
|
api_method: str,
|
|
67
67
|
status_code: int,
|
|
68
68
|
response_time: float,
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
error_message: Optional[str] = None,
|
|
70
|
+
request_params: Optional[Dict[str, Any]] = None
|
|
71
71
|
):
|
|
72
72
|
"""
|
|
73
73
|
收集接口统计数据
|
|
@@ -77,8 +77,8 @@ class ApiStatsCollector:
|
|
|
77
77
|
api_method: 请求方法
|
|
78
78
|
status_code: 状态码
|
|
79
79
|
response_time: 响应时间(秒)
|
|
80
|
-
query_string: 查询字符串
|
|
81
80
|
error_message: 错误信息
|
|
81
|
+
request_params: 请求参数(包含 headers, query_params, view_params, request_body, form_params)
|
|
82
82
|
"""
|
|
83
83
|
if not self.enabled:
|
|
84
84
|
return
|
|
@@ -89,8 +89,8 @@ class ApiStatsCollector:
|
|
|
89
89
|
'api_method': api_method,
|
|
90
90
|
'status_code': status_code,
|
|
91
91
|
'response_time': response_time,
|
|
92
|
-
'query_string': query_string,
|
|
93
92
|
'error_message': error_message,
|
|
93
|
+
'request_params': request_params,
|
|
94
94
|
'timestamp': datetime.utcnow().isoformat()
|
|
95
95
|
}
|
|
96
96
|
self.queue.put_nowait(stat_data)
|
|
@@ -217,8 +217,8 @@ def collect_api_stat(
|
|
|
217
217
|
api_method: str,
|
|
218
218
|
status_code: int,
|
|
219
219
|
response_time: float,
|
|
220
|
-
|
|
221
|
-
|
|
220
|
+
error_message: Optional[str] = None,
|
|
221
|
+
request_params: Optional[Dict[str, Any]] = None
|
|
222
222
|
):
|
|
223
223
|
"""
|
|
224
224
|
快捷方法:收集接口统计数据
|
|
@@ -232,8 +232,8 @@ def collect_api_stat(
|
|
|
232
232
|
api_method=api_method,
|
|
233
233
|
status_code=status_code,
|
|
234
234
|
response_time=response_time,
|
|
235
|
-
|
|
236
|
-
|
|
235
|
+
error_message=error_message,
|
|
236
|
+
request_params=request_params
|
|
237
237
|
)
|
|
238
238
|
|
|
239
239
|
|
|
@@ -274,8 +274,7 @@ def collect_api_stat(
|
|
|
274
274
|
api_path=request.url.path,
|
|
275
275
|
api_method=request.method,
|
|
276
276
|
status_code=response.status_code,
|
|
277
|
-
response_time=response_time
|
|
278
|
-
query_string=str(request.url.query)
|
|
277
|
+
response_time=response_time
|
|
279
278
|
)
|
|
280
279
|
|
|
281
280
|
return response
|
{huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client/sdk.py
RENAMED
|
@@ -594,14 +594,103 @@ class AuthMiddleware:
|
|
|
594
594
|
except Exception as e:
|
|
595
595
|
logger.warning(f"初始化统计收集器失败: {e}")
|
|
596
596
|
|
|
597
|
+
@staticmethod
|
|
598
|
+
def _collect_flask_request_params(request) -> Dict[str, Any]:
|
|
599
|
+
"""
|
|
600
|
+
收集 Flask 请求的所有参数
|
|
601
|
+
|
|
602
|
+
Args:
|
|
603
|
+
request: Flask request 对象
|
|
604
|
+
|
|
605
|
+
Returns:
|
|
606
|
+
包含 headers, query_params, view_params, request_body, form_params 的字典
|
|
607
|
+
"""
|
|
608
|
+
try:
|
|
609
|
+
params = {
|
|
610
|
+
"headers": dict(request.headers),
|
|
611
|
+
"query_params": request.args.to_dict(flat=False),
|
|
612
|
+
"view_params": request.view_args or {},
|
|
613
|
+
"request_body": None,
|
|
614
|
+
"form_params": None
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
# 获取请求体(JSON 或文本)
|
|
618
|
+
if request.is_json:
|
|
619
|
+
try:
|
|
620
|
+
params["request_body"] = request.get_json(silent=True)
|
|
621
|
+
except Exception:
|
|
622
|
+
pass
|
|
623
|
+
elif request.data:
|
|
624
|
+
try:
|
|
625
|
+
params["request_body"] = request.data.decode('utf-8')
|
|
626
|
+
except Exception:
|
|
627
|
+
params["request_body"] = str(request.data)
|
|
628
|
+
|
|
629
|
+
# 获取表单数据
|
|
630
|
+
if request.form:
|
|
631
|
+
params["form_params"] = request.form.to_dict(flat=False)
|
|
632
|
+
|
|
633
|
+
return params
|
|
634
|
+
except Exception as e:
|
|
635
|
+
logger.warning(f"收集Flask请求参数失败: {e}")
|
|
636
|
+
return {}
|
|
637
|
+
|
|
638
|
+
@staticmethod
|
|
639
|
+
async def _collect_fastapi_request_params(request) -> Dict[str, Any]:
|
|
640
|
+
"""
|
|
641
|
+
收集 FastAPI 请求的所有参数
|
|
642
|
+
|
|
643
|
+
Args:
|
|
644
|
+
request: FastAPI Request 对象
|
|
645
|
+
|
|
646
|
+
Returns:
|
|
647
|
+
包含 headers, query_params, view_params, request_body, form_params 的字典
|
|
648
|
+
"""
|
|
649
|
+
try:
|
|
650
|
+
params = {
|
|
651
|
+
"headers": dict(request.headers),
|
|
652
|
+
"query_params": dict(request.query_params),
|
|
653
|
+
"view_params": dict(request.path_params),
|
|
654
|
+
"request_body": None,
|
|
655
|
+
"form_params": None
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
# 获取请求体
|
|
659
|
+
content_type = request.headers.get("content-type", "")
|
|
660
|
+
|
|
661
|
+
if "application/json" in content_type:
|
|
662
|
+
try:
|
|
663
|
+
params["request_body"] = await request.json()
|
|
664
|
+
except Exception:
|
|
665
|
+
pass
|
|
666
|
+
elif "application/x-www-form-urlencoded" in content_type or "multipart/form-data" in content_type:
|
|
667
|
+
try:
|
|
668
|
+
form = await request.form()
|
|
669
|
+
params["form_params"] = {k: v for k, v in form.items()}
|
|
670
|
+
except Exception:
|
|
671
|
+
pass
|
|
672
|
+
else:
|
|
673
|
+
# 尝试读取原始body
|
|
674
|
+
try:
|
|
675
|
+
body = await request.body()
|
|
676
|
+
if body:
|
|
677
|
+
params["request_body"] = body.decode('utf-8')
|
|
678
|
+
except Exception:
|
|
679
|
+
pass
|
|
680
|
+
|
|
681
|
+
return params
|
|
682
|
+
except Exception as e:
|
|
683
|
+
logger.warning(f"收集FastAPI请求参数失败: {e}")
|
|
684
|
+
return {}
|
|
685
|
+
|
|
597
686
|
def _collect_stats(
|
|
598
687
|
self,
|
|
599
688
|
api_path: str,
|
|
600
689
|
api_method: str,
|
|
601
690
|
status_code: int,
|
|
602
691
|
response_time: float,
|
|
603
|
-
|
|
604
|
-
|
|
692
|
+
error_message: Optional[str] = None,
|
|
693
|
+
request_params: Optional[Dict[str, Any]] = None
|
|
605
694
|
):
|
|
606
695
|
"""收集接口统计"""
|
|
607
696
|
if not self.enable_stats or not self.stats_collector:
|
|
@@ -613,8 +702,8 @@ class AuthMiddleware:
|
|
|
613
702
|
api_method=api_method,
|
|
614
703
|
status_code=status_code,
|
|
615
704
|
response_time=response_time,
|
|
616
|
-
|
|
617
|
-
|
|
705
|
+
error_message=error_message,
|
|
706
|
+
request_params=request_params
|
|
618
707
|
)
|
|
619
708
|
except Exception:
|
|
620
709
|
pass # 静默失败
|
|
@@ -649,6 +738,9 @@ class AuthMiddleware:
|
|
|
649
738
|
|
|
650
739
|
path = request.url.path
|
|
651
740
|
start_time = time.time()
|
|
741
|
+
|
|
742
|
+
# 收集请求参数
|
|
743
|
+
request_params = await self._collect_fastapi_request_params(request) if self.enable_stats else None
|
|
652
744
|
|
|
653
745
|
# 检查是否跳过
|
|
654
746
|
if self._should_skip(path):
|
|
@@ -661,7 +753,7 @@ class AuthMiddleware:
|
|
|
661
753
|
if not token:
|
|
662
754
|
logger.warning("AuthMiddleware未提供认证信息")
|
|
663
755
|
response_time = time.time() - start_time
|
|
664
|
-
self._collect_stats(path, request.method, 401, response_time,
|
|
756
|
+
self._collect_stats(path, request.method, 401, response_time, "未提供认证信息", request_params)
|
|
665
757
|
return JSONResponse(
|
|
666
758
|
status_code=401,
|
|
667
759
|
content={"code": 401, "message": "未提供认证信息", "data": None}
|
|
@@ -687,7 +779,7 @@ class AuthMiddleware:
|
|
|
687
779
|
except AigcAuthError as e:
|
|
688
780
|
logger.error(f"AuthMiddleware认证失败: {e.message}")
|
|
689
781
|
response_time = time.time() - start_time
|
|
690
|
-
self._collect_stats(path, request.method, 401, response_time,
|
|
782
|
+
self._collect_stats(path, request.method, 401, response_time, e.message, request_params)
|
|
691
783
|
return JSONResponse(
|
|
692
784
|
status_code=401,
|
|
693
785
|
content={"code": e.code, "message": e.message, "data": None}
|
|
@@ -697,11 +789,11 @@ class AuthMiddleware:
|
|
|
697
789
|
try:
|
|
698
790
|
response = await call_next(request)
|
|
699
791
|
response_time = time.time() - start_time
|
|
700
|
-
self._collect_stats(path, request.method, response.status_code, response_time,
|
|
792
|
+
self._collect_stats(path, request.method, response.status_code, response_time, None, request_params)
|
|
701
793
|
return response
|
|
702
794
|
except Exception as e:
|
|
703
795
|
response_time = time.time() - start_time
|
|
704
|
-
self._collect_stats(path, request.method, 500, response_time, str(
|
|
796
|
+
self._collect_stats(path, request.method, 500, response_time, str(e), request_params)
|
|
705
797
|
raise
|
|
706
798
|
finally:
|
|
707
799
|
clear_current_user()
|
|
@@ -720,6 +812,9 @@ class AuthMiddleware:
|
|
|
720
812
|
path = request.path
|
|
721
813
|
# 记录开始时间到 g 对象
|
|
722
814
|
g.start_time = time.time()
|
|
815
|
+
|
|
816
|
+
# 收集请求参数
|
|
817
|
+
g.request_params = self._collect_flask_request_params(request) if self.enable_stats else None
|
|
723
818
|
|
|
724
819
|
# 检查是否跳过
|
|
725
820
|
if self._should_skip(path):
|
|
@@ -732,7 +827,7 @@ class AuthMiddleware:
|
|
|
732
827
|
if not token:
|
|
733
828
|
logger.warning("AuthMiddleware未提供认证信息")
|
|
734
829
|
response_time = time.time() - g.start_time
|
|
735
|
-
self._collect_stats(path, request.method, 401, response_time,
|
|
830
|
+
self._collect_stats(path, request.method, 401, response_time, "未提供认证信息", g.request_params)
|
|
736
831
|
return jsonify({
|
|
737
832
|
"code": 401,
|
|
738
833
|
"message": "未提供认证信息",
|
|
@@ -754,7 +849,7 @@ class AuthMiddleware:
|
|
|
754
849
|
except AigcAuthError as e:
|
|
755
850
|
logger.error(f"AuthMiddleware认证失败: {e.message}")
|
|
756
851
|
response_time = time.time() - g.start_time
|
|
757
|
-
self._collect_stats(path, request.method, 401, response_time,
|
|
852
|
+
self._collect_stats(path, request.method, 401, response_time, e.message, g.request_params)
|
|
758
853
|
return jsonify({
|
|
759
854
|
"code": e.code,
|
|
760
855
|
"message": e.message,
|
|
@@ -779,12 +874,14 @@ class AuthMiddleware:
|
|
|
779
874
|
|
|
780
875
|
if hasattr(g, 'start_time'):
|
|
781
876
|
response_time = time.time() - g.start_time
|
|
877
|
+
request_params = getattr(g, 'request_params', None)
|
|
782
878
|
self._collect_stats(
|
|
783
879
|
request.path,
|
|
784
880
|
request.method,
|
|
785
881
|
response.status_code,
|
|
786
882
|
response_time,
|
|
787
|
-
|
|
883
|
+
None,
|
|
884
|
+
request_params
|
|
788
885
|
)
|
|
789
886
|
|
|
790
887
|
return response
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{huace_aigc_auth_client-1.1.19 → huace_aigc_auth_client-1.1.20}/huace_aigc_auth_client/webhook.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|