用celery更新token
This commit is contained in:
parent
aabb7aefea
commit
ba4a02b67b
|
@ -32,13 +32,11 @@ class DhClient:
|
|||
self.client_secret = client_secret
|
||||
self.headers = {"Connection": "close"}
|
||||
self.isGetingToken = False
|
||||
self.isRuning = True
|
||||
self.token = None
|
||||
self.log = {}
|
||||
self.setup()
|
||||
self._get_token()
|
||||
|
||||
def _get_token_loop(self):
|
||||
while self.isRuning:
|
||||
def _get_token(self):
|
||||
self.isGetingToken = True
|
||||
params = {
|
||||
'grant_type': 'client_credentials',
|
||||
|
@ -51,16 +49,6 @@ class DhClient:
|
|||
self.headers['Authorization'] = 'bearer ' + res['access_token']
|
||||
self.headers['User-Id'] = '1'
|
||||
self.isGetingToken = False
|
||||
time.sleep(600)
|
||||
|
||||
def setup(self):
|
||||
Thread(target=DhClient._get_token_loop, args=(self, ), daemon=True).start()
|
||||
|
||||
def __del__(self):
|
||||
"""
|
||||
自定义销毁
|
||||
"""
|
||||
self.isRuning = False
|
||||
|
||||
def request(self, url: str, method: str, params=dict(), json=dict(), timeout=120,
|
||||
file_path_rela=None, raise_exception=True):
|
||||
|
@ -74,11 +62,6 @@ class DhClient:
|
|||
if params:
|
||||
url = url.format(**params)
|
||||
self.log.update({"path": url})
|
||||
if self.isGetingToken:
|
||||
wait_num = 0
|
||||
while self.isGetingToken and wait_num < 3:
|
||||
time.sleep(0.5)
|
||||
wait_num = wait_num + 1
|
||||
r = getattr(requests, method)('{}{}'.format(settings.DAHUA_BASE_URL, url),
|
||||
headers=self.headers, params=params, json=json,
|
||||
timeout=timeout, files=files, verify=False)
|
||||
|
|
|
@ -31,12 +31,10 @@ class SpClient:
|
|||
self.password = password
|
||||
self.headers = {"Connection": "close"}
|
||||
self.isGetingToken = False
|
||||
self.isRuning = True
|
||||
self.log = {}
|
||||
self.setup()
|
||||
self._get_token()
|
||||
|
||||
def _get_token_loop(self):
|
||||
while self.isRuning:
|
||||
def _get_token(self):
|
||||
self.isGetingToken = True
|
||||
json_data = {
|
||||
"user": {
|
||||
|
@ -48,16 +46,6 @@ class SpClient:
|
|||
if is_ok == 'success':
|
||||
self.headers['Authorization'] = 'Bearer ' + res['user']['token']
|
||||
self.isGetingToken = False
|
||||
time.sleep(1200)
|
||||
|
||||
def setup(self):
|
||||
Thread(target=SpClient._get_token_loop, args=(self, ), daemon=True).start()
|
||||
|
||||
def __del__(self):
|
||||
"""
|
||||
自定义销毁
|
||||
"""
|
||||
self.isRuning = False
|
||||
|
||||
def request(self, url: str, method: str, params=dict(), json=dict(), timeout=120,
|
||||
file_path_rela=None, raise_exception=True):
|
||||
|
@ -70,11 +58,6 @@ class SpClient:
|
|||
files = {'file': open(settings.BASE_DIR + file_path_rela, 'rb')}
|
||||
if params:
|
||||
url = url.format(**params)
|
||||
if self.isGetingToken:
|
||||
wait_num = 0
|
||||
while self.isGetingToken and wait_num < 3:
|
||||
time.sleep(0.5)
|
||||
wait_num = wait_num + 1
|
||||
r = getattr(requests, method)('{}{}'.format(settings.SP_BASE_URL, url),
|
||||
headers=self.headers, params=params, json=json,
|
||||
timeout=timeout, files=files, verify=False)
|
||||
|
|
|
@ -5,6 +5,7 @@ from apps.utils.tasks import CustomTask
|
|||
from apps.third.models import Tlog
|
||||
from celery import shared_task
|
||||
from django.utils import timezone
|
||||
from apps.third.clients import xxClient, dhClient, spClient
|
||||
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
|
@ -16,3 +17,18 @@ def clear_tlog():
|
|||
now = timezone.now()
|
||||
days7_ago = now - timedelta(days=7)
|
||||
Tlog.objects.filter(create_time__lte=days7_ago).delete()
|
||||
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
def get_xx_token():
|
||||
xxClient._get_token()
|
||||
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
def get_dh_token():
|
||||
dhClient._get_token()
|
||||
|
||||
|
||||
@shared_task(base=CustomTask)
|
||||
def get_sp_token():
|
||||
spClient._get_token()
|
||||
|
|
|
@ -50,11 +50,6 @@ class XxClient:
|
|||
json['licence'] = settings.XX_LICENCE
|
||||
self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method,
|
||||
"params": params, "body": json, "target": "xunxi", "result": 10, "headers": self.headers}
|
||||
if self.isGetingToken:
|
||||
wait_num = 0
|
||||
while self.isGetingToken and wait_num < 3:
|
||||
time.sleep(0.5)
|
||||
wait_num = wait_num + 1
|
||||
r = getattr(requests, method)('{}{}'.format(settings.XX_BASE_URL, url),
|
||||
headers=self.headers, params=params, json=json, timeout=timeout, verify=False)
|
||||
# if settings.DEBUG:
|
||||
|
@ -63,10 +58,10 @@ class XxClient:
|
|||
if 300 > r.status_code >= 200:
|
||||
ret = r.json()
|
||||
if ret['errorCode'] != 0:
|
||||
if ret.get('errorCode') in ['1060000', 1060000]:
|
||||
self._get_token() # 重新获取token
|
||||
self.request(url, method, params, json, timeout, raise_exception) # 重新请求
|
||||
return
|
||||
# if ret.get('errorCode') in ['1060000', 1060000]:
|
||||
# self._get_token() # 重新获取token
|
||||
# self.request(url, method, params, json, timeout, raise_exception) # 重新请求
|
||||
# return
|
||||
err_detail = dict(detail='寻息错误:' + '|'.join(ret['errorMsg']),
|
||||
code='xx_' + str(ret['errorCode']))
|
||||
self.handle_log(result='fail', response=ret)
|
||||
|
|
Loading…
Reference in New Issue