a
This commit is contained in:
parent
fad8a1d6fd
commit
211ef38ee9
|
|
@ -30,6 +30,10 @@ class BinanceClient:
|
|||
api_secret: API密钥(如果为None,从config读取)
|
||||
testnet: 是否使用测试网(如果为None,从config读取)
|
||||
"""
|
||||
# 记录是否明确传入了 api_key 和 api_secret(用于后续判断是否应该从 config 刷新)
|
||||
self._explicit_api_key = api_key is not None
|
||||
self._explicit_api_secret = api_secret is not None
|
||||
|
||||
# 如果未提供参数,从config读取(确保使用最新值)
|
||||
# 注意:如果明确传递了 api_key 和 api_secret,应该使用传递的值,而不是从 config 读取
|
||||
if api_key is None:
|
||||
|
|
@ -64,7 +68,7 @@ class BinanceClient:
|
|||
# 记录使用的 API Key(用于调试,只显示前后4位)
|
||||
if api_key:
|
||||
key_display = f"{api_key[:4]}...{api_key[-4:]}" if len(api_key) > 8 else api_key
|
||||
logger.info(f"BinanceClient.__init__: 使用 API Key {key_display}, testnet={testnet}")
|
||||
logger.info(f"BinanceClient.__init__: 使用 API Key {key_display}, testnet={testnet}, explicit_key={self._explicit_api_key}, explicit_secret={self._explicit_api_secret}")
|
||||
else:
|
||||
logger.warning("BinanceClient.__init__: API Key 为空!")
|
||||
self.client: Optional[AsyncClient] = None
|
||||
|
|
@ -90,6 +94,11 @@ class BinanceClient:
|
|||
|
||||
def _refresh_api_credentials(self):
|
||||
"""刷新API密钥(从配置管理器重新读取,确保使用最新值)"""
|
||||
# 如果明确传入了 api_key 和 api_secret,不应该从 config 刷新(避免覆盖多账号场景下的正确密钥)
|
||||
if self._explicit_api_key and self._explicit_api_secret:
|
||||
logger.debug("BinanceClient: 使用了明确的 API 密钥,跳过从 config 刷新(避免覆盖多账号场景)")
|
||||
return
|
||||
|
||||
# 优先从配置管理器读取(会从Redis获取最新值)
|
||||
if config._config_manager:
|
||||
try:
|
||||
|
|
@ -99,7 +108,8 @@ class BinanceClient:
|
|||
new_api_secret = config._config_manager.get('BINANCE_API_SECRET')
|
||||
|
||||
# 如果获取到新值且与当前值不同,更新
|
||||
if new_api_key and new_api_key != self.api_key:
|
||||
# 注意:即使传入了明确的密钥,如果只传入了其中一个,另一个仍可以从 config 刷新
|
||||
if not self._explicit_api_key and new_api_key and new_api_key != self.api_key:
|
||||
old_key_display = f"{self.api_key[:4]}...{self.api_key[-4:]}" if self.api_key and len(self.api_key) > 8 else self.api_key
|
||||
new_key_display = f"{new_api_key[:4]}...{new_api_key[-4:]}" if new_api_key and len(new_api_key) > 8 else new_api_key
|
||||
logger.info(f"检测到API密钥已更新: {old_key_display} -> {new_key_display}")
|
||||
|
|
@ -108,7 +118,7 @@ class BinanceClient:
|
|||
if self.client:
|
||||
logger.warning("API密钥已更新,但客户端已连接,需要重新连接才能使用新密钥")
|
||||
|
||||
if new_api_secret and new_api_secret != self.api_secret:
|
||||
if not self._explicit_api_secret and new_api_secret and new_api_secret != self.api_secret:
|
||||
logger.info("检测到API密钥Secret已更新")
|
||||
self.api_secret = new_api_secret
|
||||
# 如果客户端已连接,需要重新连接以使用新密钥
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user