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