fix: king request 5

This commit is contained in:
caoqianming 2024-04-25 20:54:35 +08:00
parent 4004278f31
commit dd61b1ba01
1 changed files with 14 additions and 14 deletions

View File

@ -35,30 +35,29 @@ class KingClient(HandleLogMixin):
# is_ok, _ = self.request(**kapis['heartbeat'], raise_exception=False, timeout=10) # is_ok, _ = self.request(**kapis['heartbeat'], raise_exception=False, timeout=10)
# if is_ok == 'success': # if is_ok == 'success':
# return # return
if cache.get('king_token_request') != 'requesting': if cache.get('king_token') != 'requesting':
json = { json = {
'username': settings.KING_USERNAME, 'username': settings.KING_USERNAME,
'password': settings.KING_PASSWORD 'password': settings.KING_PASSWORD
} }
cache.set('king_token_request', 'requesting', timeout=None) cache.set('king_token', 'requesting', timeout=None)
_, res = self.request( _, 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', 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: if not self.king_enabled:
raise ParseError('亚控对接未启用') raise ParseError('亚控对接未启用')
if check_auth: if need_auth:
count = 6 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) time.sleep(0.5)
if cache.get('king_token_request') == 'done': count = count -1
break
count = count - 1
if count < 0:
break
self.headers['Authorization'] = cache.get('king_token')
self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method, self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method,
"params": params, "body": json, "target": "king", "result": 10, "headers": self.headers} "params": params, "body": json, "target": "king", "result": 10, "headers": self.headers}
try: try:
@ -69,9 +68,10 @@ class KingClient(HandleLogMixin):
ret = r.text ret = r.text
if 300 > r.status_code >= 200: if 300 > r.status_code >= 200:
ret = r.json() ret = r.json()
myLogger.info(url, ret)
if 'code' in ret: if 'code' in ret:
if ret['code'] != 0: 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._get_token()
self.request(url, method, params, json, timeout, raise_exception, check_auth=False) self.request(url, method, params, json, timeout, raise_exception, check_auth=False)
return return