From 98d10405e49889779222428630cd2d6f1924e648 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Wed, 19 Jun 2024 18:13:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20KingClient=20=E6=B7=BB=E5=8A=A0=E6=97=A5?= =?UTF-8?q?=E5=BF=972?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/third/king/k.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/apps/third/king/k.py b/apps/third/king/k.py index bc3dffe1..acc1f964 100644 --- a/apps/third/king/k.py +++ b/apps/third/king/k.py @@ -35,19 +35,22 @@ class KingClient(HandleLogMixin): # is_ok, _ = self.request(**kapis['heartbeat'], raise_exception=False, timeout=10) # if is_ok == 'success': # return - current_val = cache.get('king_token') + current_val:str = cache.get('king_token') + is_ok = 'success' if current_val != 'requesting': json = { 'username': settings.KING_USERNAME, 'password': settings.KING_PASSWORD } - try: - cache.set('king_token', 'requesting', timeout=None) - _, res = self.request( - **kapis['login'], json=json, timeout=10, need_auth=False) - cache.set('king_token', res['Authorization'], timeout=None) - except Exception: + cache.set('king_token', 'requesting', timeout=None) + is_ok, res = self.request( + **kapis['login'], json=json, timeout=10, need_auth=False, raise_exception=False) + if is_ok == "success": + current_val = res['Authorization'] cache.set('king_token', current_val, timeout=None) + return is_ok, current_val + + def request(self, url: str, method: str = 'post', params=dict(), json=dict(), timeout=20, raise_exception=True, need_auth=True): if not self.king_enabled: @@ -74,10 +77,11 @@ class KingClient(HandleLogMixin): ret = r.json() if 'code' in ret: if ret['code'] != 0: - if need_auth and ret['code'] in [-1, -2]: # 认证失败 + if need_auth and ret['code'] in [-1, -2] and ret['message'] != 'failed': # 认证失败 myLogger.error(f"亚控认证失败: {url}-{ret}") - self._get_token() - return self.request(url, method, params, json, timeout, raise_exception, need_auth) + is_ok, _ = self._get_token() + if is_ok: + return self.request(url, method, params, json, timeout, raise_exception, need_auth) err_detail = dict(detail=f"亚控错误: {ret['message']}", code='king_' + str(ret['code']))