diff --git a/apps/third/king/k.py b/apps/third/king/k.py index 323ac1e3..0cb5a2d6 100644 --- a/apps/third/king/k.py +++ b/apps/third/king/k.py @@ -35,17 +35,21 @@ class KingClient(HandleLogMixin): # is_ok, _ = self.request(**kapis['heartbeat'], raise_exception=False, timeout=10) # if is_ok == 'success': # return - if cache.get('king_token') != 'requesting': + current_val = cache.get('king_token') + if current_val != 'requesting': json = { 'username': settings.KING_USERNAME, 'password': settings.KING_PASSWORD } - cache.set('king_token', 'requesting', timeout=None) - _, res = self.request( - **kapis['login'], json=json, timeout=10, need_auth=False) + 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', current_val, timeout=None) cache.set('king_token', res['Authorization'], timeout=None) - 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: raise ParseError('亚控对接未启用') @@ -55,6 +59,7 @@ class KingClient(HandleLogMixin): token = cache.get('king_token') if token != 'requesting': self.headers['Authorization'] = token + break time.sleep(0.5) count = count -1 @@ -68,12 +73,11 @@ class KingClient(HandleLogMixin): ret = r.text if 300 > r.status_code >= 200: ret = r.json() - myLogger.info(url, ret) if 'code' in ret: if ret['code'] != 0: if need_auth and ret['code'] in [-1, -2]: # 认证失败 self._get_token() - self.request(url, method, params, json, timeout, raise_exception, check_auth=False) + self.request(url, method, params, json, timeout, raise_exception, need_auth=False) return err_detail = dict(detail=f"亚控错误: {ret['message']}", code='king_' + str(ret['code']))