From cc2785d42e19f427819ff96e9a39709b75b319ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E5=89=8D=E6=98=8E?= <909355014@qq.com> Date: Mon, 26 Sep 2022 15:06:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=96=B9=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/hrm/services.py | 2 +- apps/third/dahua.py | 29 ++++++++---------- apps/third/speaker.py | 48 ++++++++--------------------- apps/third/tapis.py | 4 +++ apps/third/xunxi.py | 70 +++++++++++++++++++++---------------------- 5 files changed, 65 insertions(+), 88 deletions(-) diff --git a/apps/hrm/services.py b/apps/hrm/services.py index 66803eaf..b3d41d08 100755 --- a/apps/hrm/services.py +++ b/apps/hrm/services.py @@ -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 diff --git a/apps/third/dahua.py b/apps/third/dahua.py index 12fd58cd..7133d1f1 100644 --- a/apps/third/dahua.py +++ b/apps/third/dahua.py @@ -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: diff --git a/apps/third/speaker.py b/apps/third/speaker.py index 83b62f82..a29563a5 100644 --- a/apps/third/speaker.py +++ b/apps/third/speaker.py @@ -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: diff --git a/apps/third/tapis.py b/apps/third/tapis.py index a0854c00..84ac807d 100755 --- a/apps/third/tapis.py +++ b/apps/third/tapis.py @@ -136,6 +136,10 @@ dhapis = { # 寻息API接口 xxapis = { + "token_login": { + "url": "/getAccessTokenV2", + "method": "post" + }, "blg_list": { "url": "/api/devicesV3/blgs", "method": "post" diff --git a/apps/third/xunxi.py b/apps/third/xunxi.py index 2551ed70..083e4202 100644 --- a/apps/third/xunxi.py +++ b/apps/third/xunxi.py @@ -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)