fix: kingClient get_token request 3

This commit is contained in:
caoqianming 2024-04-25 18:35:33 +08:00
parent 41c79c1fdd
commit 1a2baa82d9
1 changed files with 13 additions and 12 deletions

View File

@ -42,21 +42,22 @@ class KingClient(HandleLogMixin):
}
cache.set('king_token_request', 'requesting', timeout=None)
_, res = self.request(
**kapis['login'], json=json, timeout=10)
**kapis['login'], json=json, timeout=10, check_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):
def request(self, url: str, method: str = 'post', params=dict(), json=dict(), timeout=20, raise_exception=True, check_auth=True):
if not self.king_enabled:
raise ParseError('亚控对接未启用')
count = 6
while cache.get('king_token_request') == 'requesting':
time.sleep(0.5)
if cache.get('king_token_request') == 'done':
break
count = count - 1
if count < 0:
break
if check_auth:
count = 6
while cache.get('king_token_request') == 'requesting':
time.sleep(0.5)
if cache.get('king_token_request') == 'done':
break
count = count - 1
if count < 0:
break
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 +70,9 @@ class KingClient(HandleLogMixin):
ret = r.json()
if 'code' in ret:
if ret['code'] != 0:
if ret['code'] in [-1, -2]: # 认证失败
if ret['code'] in [-1, -2] and check_auth: # 认证失败
self._get_token()
self.request(url, method, params, json, timeout, raise_exception)
self.request(url, method, params, json, timeout, raise_exception, check_auth=False)
return
err_detail = dict(detail=f"亚控错误: {ret['message']}",
code='king_' + str(ret['code']))