第三方请求优化
This commit is contained in:
parent
5d15469b05
commit
cc2785d42e
|
@ -149,7 +149,7 @@ class HrmService:
|
|||
"endDate": endDate
|
||||
}
|
||||
_, res = dhClient.request(**dhapis['card_add'], json=json_data)
|
||||
time.sleep(8) # 等待确保生成卡片
|
||||
time.sleep(10) # 等待确保生成卡片
|
||||
cls.save(ep, data={'dh_face_card': cardNumber})
|
||||
return cardNumber
|
||||
|
||||
|
|
|
@ -40,15 +40,18 @@ class DhClient:
|
|||
|
||||
def _get_token_loop(self):
|
||||
while self.isRuning:
|
||||
self.isGetingToken = True
|
||||
params = {
|
||||
'grant_type': 'client_credentials',
|
||||
'client_id': self.client_id,
|
||||
'client_secret': self.client_secret
|
||||
}
|
||||
_, res = self.request(**dhapis['token_login'], params=params, raise_exception=False)
|
||||
self.token = res['access_token']
|
||||
self.headers['Authorization'] = 'bearer ' + res['access_token']
|
||||
self.headers['User-Id'] = '1'
|
||||
is_ok, res = self.request(**dhapis['token_login'], params=params, raise_exception=False)
|
||||
if is_ok == 'success':
|
||||
self.token = res['access_token']
|
||||
self.headers['Authorization'] = 'bearer ' + res['access_token']
|
||||
self.headers['User-Id'] = '1'
|
||||
self.isGetingToken = False
|
||||
time.sleep(1200)
|
||||
|
||||
def setup(self):
|
||||
|
@ -71,18 +74,12 @@ class DhClient:
|
|||
files = None
|
||||
if file_path_rela: # 相对路径
|
||||
files = {'file': open(settings.BASE_DIR + file_path_rela, 'rb')}
|
||||
try:
|
||||
if params:
|
||||
url = url.format(**params)
|
||||
self.log.update({"path": url})
|
||||
r = getattr(requests, method)('{}{}'.format(settings.DAHUA_BASE_URL, url),
|
||||
headers=self.headers, params=params, json=json,
|
||||
timeout=timeout, files=files, verify=False)
|
||||
except Exception:
|
||||
self.handle_log(result='error', errors=traceback.format_exc())
|
||||
if raise_exception:
|
||||
raise APIException(**DH_REQUEST_ERROR)
|
||||
return 'error', DH_REQUEST_ERROR
|
||||
if params:
|
||||
url = url.format(**params)
|
||||
self.log.update({"path": url})
|
||||
r = getattr(requests, method)('{}{}'.format(settings.DAHUA_BASE_URL, url),
|
||||
headers=self.headers, params=params, json=json,
|
||||
timeout=timeout, files=files, verify=False)
|
||||
# if settings.DEBUG:
|
||||
# print_roundtrip(r)
|
||||
if r.status_code == 200:
|
||||
|
|
|
@ -38,34 +38,18 @@ class SpClient:
|
|||
|
||||
def _get_token_loop(self):
|
||||
while self.isRuning:
|
||||
params = {
|
||||
self.isGetingToken = True
|
||||
json_data = {
|
||||
"user": {
|
||||
"email": self.username,
|
||||
"password": self.password
|
||||
}
|
||||
}
|
||||
try:
|
||||
r = requests.post(json=params,
|
||||
url=settings.SP_BASE_URL + '/api/users/login', verify=False)
|
||||
if r.status_code == 200:
|
||||
ret = r.json()
|
||||
self.headers['Authorization'] = 'Bearer ' + ret['user']['token']
|
||||
time.sleep(1200)
|
||||
except Exception:
|
||||
myLogger.error('喇叭服务连接失败', exc_info=True)
|
||||
|
||||
def get_token(self):
|
||||
self.isGetingToken = True
|
||||
params = {
|
||||
'grant_type': 'client_credentials',
|
||||
'client_id': self.client_id,
|
||||
'client_secret': self.client_secret
|
||||
}
|
||||
r = requests.post(params=params, url=settings.SP_BASE_URL + '/evo-apigw/evo-oauth/oauth/token', verify=False)
|
||||
if r.status_code == 200:
|
||||
ret = r.json()
|
||||
self.headers['Authorization'] = 'Bearer ' + ret['user']['token']
|
||||
is_ok, res = self.request(**spapis['user_login'], json=json_data, raise_exception=False)
|
||||
if is_ok == 'success':
|
||||
self.headers['Authorization'] = 'Bearer ' + res['user']['token']
|
||||
self.isGetingToken = False
|
||||
time.sleep(1200)
|
||||
|
||||
def setup(self):
|
||||
t = Thread(target=self._get_token_loop, args=(), daemon=True)
|
||||
|
@ -83,21 +67,15 @@ class SpClient:
|
|||
if not settings.SP_ENABLED:
|
||||
raise ParseError('音响对接未启用')
|
||||
self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method,
|
||||
"params": params, "body": json, "target": "speaker", "result": 10}
|
||||
"params": params, "body": json, "target": "speaker", "result": 10, "headers": self.headers}
|
||||
files = None
|
||||
if file_path_rela: # 相对路径
|
||||
files = {'file': open(settings.BASE_DIR + file_path_rela, 'rb')}
|
||||
try:
|
||||
if params:
|
||||
url = url.format(**params)
|
||||
r = getattr(requests, method)('{}{}'.format(settings.SP_BASE_URL, url),
|
||||
headers=self.headers, params=params, json=json,
|
||||
timeout=timeout, files=files, verify=False)
|
||||
except Exception:
|
||||
self.handle_log(result='error', errors=traceback.format_exc())
|
||||
if raise_exception:
|
||||
raise APIException(**SP_REQUEST_ERROR)
|
||||
return 'error', SP_REQUEST_ERROR
|
||||
if params:
|
||||
url = url.format(**params)
|
||||
r = getattr(requests, method)('{}{}'.format(settings.SP_BASE_URL, url),
|
||||
headers=self.headers, params=params, json=json,
|
||||
timeout=timeout, files=files, verify=False)
|
||||
# if settings.DEBUG:
|
||||
# print_roundtrip(r)
|
||||
if r.status_code == 200:
|
||||
|
@ -109,7 +87,7 @@ class SpClient:
|
|||
if raise_exception:
|
||||
raise ParseError(**err_detail)
|
||||
return 'fail', dict(detail=detail, code='sp_'+str(ret['code']))
|
||||
self.handle_log(result='success', response=ret)
|
||||
# self.handle_log(result='success', response=ret)
|
||||
return 'success', ret
|
||||
self.handle_log(result='error', errors=traceback.format_exc())
|
||||
if raise_exception:
|
||||
|
|
|
@ -136,6 +136,10 @@ dhapis = {
|
|||
|
||||
# 寻息API接口
|
||||
xxapis = {
|
||||
"token_login": {
|
||||
"url": "/getAccessTokenV2",
|
||||
"method": "post"
|
||||
},
|
||||
"blg_list": {
|
||||
"url": "/api/devicesV3/blgs",
|
||||
"method": "post"
|
||||
|
|
|
@ -35,17 +35,15 @@ class XxClient:
|
|||
|
||||
def _get_token_loop(self):
|
||||
while self.isRuning:
|
||||
self.isGetingToken = True
|
||||
json = {
|
||||
'licence': self.licence
|
||||
}
|
||||
try:
|
||||
r = requests.post(json=json, url=settings.XX_BASE_URL + '/getAccessTokenV2', verify=False, timeout=120)
|
||||
ret = r.json()
|
||||
if ret.get('errorCode', 1) == 0:
|
||||
self.token = ret['data']['token']
|
||||
time.sleep(1200)
|
||||
except Exception:
|
||||
myLogger.error('寻息服务连接失败', exc_info=True)
|
||||
is_ok, res = self.request(**xxapis['token_login'], json=json, raise_exception=False)
|
||||
if is_ok == 'success':
|
||||
self.token = res['token']
|
||||
self.isGetingToken = False
|
||||
time.sleep(1200)
|
||||
|
||||
def get_token(self):
|
||||
self.isGetingToken = True
|
||||
|
@ -77,35 +75,35 @@ class XxClient:
|
|||
json['buildId'] = settings.XX_BUILDID
|
||||
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}
|
||||
if self.isGetingToken:
|
||||
req_num = 0
|
||||
while True:
|
||||
time.sleep(0.5)
|
||||
if not self.isGetingToken:
|
||||
self.request(url, method, params, json, timeout, raise_exception)
|
||||
req_num = req_num + 1
|
||||
if req_num > 4:
|
||||
break
|
||||
"params": params, "body": json, "target": "xunxi", "result": 10, "headers": self.headers}
|
||||
# if self.isGetingToken:
|
||||
# req_num = 0
|
||||
# while True:
|
||||
# time.sleep(0.5)
|
||||
# if not self.isGetingToken:
|
||||
# self.request(url, method, params, json, timeout, raise_exception)
|
||||
# req_num = req_num + 1
|
||||
# if req_num > 4:
|
||||
# break
|
||||
# else:
|
||||
r = getattr(requests, method)('{}{}'.format(settings.XX_BASE_URL, url),
|
||||
headers=self.headers, params=params, json=json, timeout=timeout, verify=False)
|
||||
# if settings.DEBUG:
|
||||
# print_roundtrip(r)
|
||||
ret = r.json()
|
||||
if ret.get('errorCode') in ['1060000', 1060000]:
|
||||
self.get_token() # 重新获取token
|
||||
self.request(url, method, params, json, timeout, raise_exception) # 重新请求
|
||||
else:
|
||||
r = getattr(requests, method)('{}{}'.format(settings.XX_BASE_URL, url),
|
||||
headers=self.headers, params=params, json=json, timeout=timeout, verify=False)
|
||||
# if settings.DEBUG:
|
||||
# print_roundtrip(r)
|
||||
ret = r.json()
|
||||
if ret.get('errorCode') in ['1060000', 1060000]:
|
||||
self.get_token() # 重新获取token
|
||||
self.request(url, method, params, json, timeout, raise_exception) # 重新请求
|
||||
else:
|
||||
if ret['errorCode'] != 0:
|
||||
err_detail = dict(detail='寻息错误:' + '|'.join(ret['errorMsg']),
|
||||
code='xx_' + str(ret['errorCode']))
|
||||
self.handle_log(result='fail', response=ret)
|
||||
if raise_exception:
|
||||
raise ParseError(**err_detail)
|
||||
return 'fail', err_detail
|
||||
# self.handle_log(result='success', response=ret)
|
||||
return 'success', ret['data']
|
||||
if ret['errorCode'] != 0:
|
||||
err_detail = dict(detail='寻息错误:' + '|'.join(ret['errorMsg']),
|
||||
code='xx_' + str(ret['errorCode']))
|
||||
self.handle_log(result='fail', response=ret)
|
||||
if raise_exception:
|
||||
raise ParseError(**err_detail)
|
||||
return 'fail', err_detail
|
||||
# self.handle_log(result='success', response=ret)
|
||||
return 'success', ret['data']
|
||||
self.handle_log(result='error', errors=traceback.format_exc())
|
||||
if raise_exception:
|
||||
raise APIException(**XX_REQUEST_ERROR)
|
||||
|
|
Loading…
Reference in New Issue