fix: KingClient 添加日志2

This commit is contained in:
caoqianming 2024-06-19 18:13:48 +08:00
parent c42dafcb44
commit 98d10405e4
1 changed files with 14 additions and 10 deletions

View File

@ -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']))