diff --git a/apps/third/king/k.py b/apps/third/king/k.py index 7c69ca8e..323ac1e3 100644 --- a/apps/third/king/k.py +++ b/apps/third/king/k.py @@ -35,30 +35,29 @@ class KingClient(HandleLogMixin): # is_ok, _ = self.request(**kapis['heartbeat'], raise_exception=False, timeout=10) # if is_ok == 'success': # return - if cache.get('king_token_request') != 'requesting': + if cache.get('king_token') != 'requesting': json = { 'username': settings.KING_USERNAME, 'password': settings.KING_PASSWORD } - cache.set('king_token_request', 'requesting', timeout=None) + cache.set('king_token', 'requesting', timeout=None) _, res = self.request( - **kapis['login'], json=json, timeout=10, check_auth=False) + **kapis['login'], json=json, timeout=10, need_auth=False) cache.set('king_token', res['Authorization'], timeout=None) - cache.set('king_token_request', 'done', timeout=None) - def request(self, url: str, method: str = 'post', params=dict(), json=dict(), timeout=20, raise_exception=True, check_auth=True): + + 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('亚控对接未启用') - if check_auth: + if need_auth: count = 6 - while cache.get('king_token_request') == 'requesting': + while count > 0: + token = cache.get('king_token') + if token != 'requesting': + self.headers['Authorization'] = token time.sleep(0.5) - if cache.get('king_token_request') == 'done': - break - count = count - 1 - if count < 0: - break - self.headers['Authorization'] = cache.get('king_token') + count = count -1 + self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method, "params": params, "body": json, "target": "king", "result": 10, "headers": self.headers} try: @@ -69,9 +68,10 @@ 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 ret['code'] in [-1, -2] and check_auth: # 认证失败 + if need_auth and ret['code'] in [-1, -2]: # 认证失败 self._get_token() self.request(url, method, params, json, timeout, raise_exception, check_auth=False) return