internal 1.1.5__tar.gz → 1.1.6__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 internal might be problematic. Click here for more details.
- {internal-1.1.5 → internal-1.1.6}/PKG-INFO +1 -1
- {internal-1.1.5 → internal-1.1.6}/pyproject.toml +1 -1
- {internal-1.1.5 → internal-1.1.6}/src/internal/base_config.py +2 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/http/requests.py +59 -3
- {internal-1.1.5 → internal-1.1.6}/README.md +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/__init__.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/base_factory.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/cache_redis.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/__init__.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/contact_type.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/description_type.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/device_code.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/event_code.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/lpr_direction.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/notify_type.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/operator_type.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/order_type.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/common_enum/websocket_channel.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/const.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/database.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/exception/__init__.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/exception/app_exception.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/exception/base_exception.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/exception/internal_exception.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/ext/__init__.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/ext/amazon/__init__.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/ext/amazon/aws/__init__.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/ext/amazon/aws/const.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/http/__init__.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/http/responses.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/interface/__init__.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/interface/base_interface.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/middleware/__init__.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/middleware/log_request.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/model/__init__.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/model/base_model.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/model/operate.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/utils.py +0 -0
- {internal-1.1.5 → internal-1.1.6}/src/internal/validator_utils.py +0 -0
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import random
|
|
3
|
+
|
|
1
4
|
import httpx
|
|
2
5
|
|
|
3
6
|
from fastapi import FastAPI
|
|
@@ -59,10 +62,63 @@ async def async_request(app: FastAPI, method, url, current_user: dict = None,
|
|
|
59
62
|
raise BadGatewayException(str(exc))
|
|
60
63
|
|
|
61
64
|
|
|
65
|
+
async def invoke_webhook_message_api(app: FastAPI, message: str):
|
|
66
|
+
payload = {"text": message}
|
|
67
|
+
response = await async_request(app, "POST", app.state.config.WEBHOOK_BASE_URL, json=payload)
|
|
68
|
+
response.raise_for_status()
|
|
69
|
+
return response
|
|
70
|
+
|
|
71
|
+
|
|
62
72
|
async def send_webhook_message(app: FastAPI, message: str):
|
|
63
|
-
if app.state.config.WEBHOOK_BASE_URL:
|
|
64
|
-
|
|
73
|
+
if not app.state.config.WEBHOOK_BASE_URL:
|
|
74
|
+
app.state.logger.warn(f"Skip notify webhook url is null")
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
retry_count = app.state.config.WEBHOOK_RETRY_COUNT
|
|
78
|
+
if retry_count <= 0:
|
|
65
79
|
try:
|
|
66
|
-
await
|
|
80
|
+
response = await invoke_webhook_message_api(app, message)
|
|
81
|
+
return response
|
|
67
82
|
except Exception as e:
|
|
68
83
|
app.state.logger.warn(f"Notify failure, Exception:{e}")
|
|
84
|
+
else:
|
|
85
|
+
retries = 0
|
|
86
|
+
current_delay = float(app.state.config.WEBHOOK_RETRY_DELAY_INITIAL_SECONDS)
|
|
87
|
+
|
|
88
|
+
while retries <= retry_count:
|
|
89
|
+
app.state.logger.warn(f"嘗試發送訊息 (第 {retries + 1} 次嘗試)...")
|
|
90
|
+
try:
|
|
91
|
+
# 使用 await 關鍵字等待異步請求完成
|
|
92
|
+
response = await invoke_webhook_message_api(app, message)
|
|
93
|
+
return response
|
|
94
|
+
except httpx.HTTPStatusError as e:
|
|
95
|
+
if e.response.status_code == 429:
|
|
96
|
+
app.state.logger.warn(f"收到 429 錯誤:{e.response.status_code} - {e.response.text}")
|
|
97
|
+
if retries < retry_count:
|
|
98
|
+
# 計算下一次的延遲時間:current_delay * 2^retries + 隨機抖動
|
|
99
|
+
sleep_time = current_delay * (2 ** retries) + random.uniform(0, 0.5)
|
|
100
|
+
app.state.logger.warn(f"等待 {sleep_time:.2f} 秒後重試...")
|
|
101
|
+
# 使用 asyncio.sleep 進行異步等待,不會阻塞主執行緒
|
|
102
|
+
await asyncio.sleep(sleep_time)
|
|
103
|
+
retries += 1
|
|
104
|
+
else:
|
|
105
|
+
app.state.logger.warn(
|
|
106
|
+
f"已達到最大重試次數 ({app.state.config.WEBHOOK_RETRY_COUNT}),放棄發送訊息。")
|
|
107
|
+
raise # 重新拋出最後一個異常
|
|
108
|
+
else:
|
|
109
|
+
app.state.logger.warn(f"發生其他 HTTP 錯誤:{e.response.status_code} - {e.response.text}")
|
|
110
|
+
raise # 對於非 429 錯誤,直接拋出
|
|
111
|
+
|
|
112
|
+
except httpx.RequestError as e:
|
|
113
|
+
# 處理網絡錯誤,例如 DNS 查找失敗、連接超時等
|
|
114
|
+
app.state.logger.warn(f"發生網絡錯誤:{e}")
|
|
115
|
+
if retries < retry_count:
|
|
116
|
+
sleep_time = current_delay * (2 ** retries) + random.uniform(0, 0.5)
|
|
117
|
+
app.state.logger.warn(f"等待 {sleep_time:.2f} 秒後重試...")
|
|
118
|
+
await asyncio.sleep(sleep_time)
|
|
119
|
+
retries += 1
|
|
120
|
+
else:
|
|
121
|
+
app.state.logger.warn(f"已達到最大重試次數 ({retry_count}),放棄發送訊息。")
|
|
122
|
+
raise
|
|
123
|
+
|
|
124
|
+
return None # 如果重試次數用盡仍未成功
|
|
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
|
|
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
|
|
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
|