From e84f012e091e111c259e7a33b329489812f844c1 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Thu, 17 Nov 2022 17:50:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E4=B8=89=E6=96=B9token?= =?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/third/dahua.py | 52 +++++++++++++++++++++++---------------------- apps/third/xunxi.py | 52 +++++++++++++++++++++++---------------------- 2 files changed, 54 insertions(+), 50 deletions(-) diff --git a/apps/third/dahua.py b/apps/third/dahua.py index ed8b7513..a79e0e21 100644 --- a/apps/third/dahua.py +++ b/apps/third/dahua.py @@ -36,7 +36,7 @@ class DhClient: self.headers = {"Connection": "close"} self.isGetingToken = False self.log = {} - self._get_token() + # self._get_token() def _get_token(self): self.isGetingToken = True @@ -45,7 +45,7 @@ class DhClient: 'client_id': self.client_id, 'client_secret': self.client_secret } - is_ok, res = self.request(**dhapis['token_login'], params=params, raise_exception=False) + is_ok, res = self.request(**dhapis['token_login'], params=params, raise_exception=False, timeout=2) if is_ok == 'success': cache.set('dh_token', res['access_token'], timeout=None) self.isGetingToken = False @@ -64,29 +64,31 @@ class DhClient: 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) - ret = r.text - if 300 > r.status_code >= 200: - ret = r.json() - if ret['code'] not in ['0', '100', '00000', '1000', 0, 100, 1000]: - detail = '大华错误:' + \ - '{}|{}{}{}'.format(str(ret['code']), ret.get('errMsg', ''), - ret.get('desc', ''), str(ret.get('data', ''))) - err_detail = dict(detail=detail, code='dh_'+str(ret['code'])) - 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 'data' in ret else None - self.handle_log(result='error', response=ret) - if raise_exception: - raise APIException(**DH_REQUEST_ERROR) - return 'error', DH_REQUEST_ERROR + try: + 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) + ret = r.text + if 300 > r.status_code >= 200: + ret = r.json() + if ret['code'] not in ['0', '100', '00000', '1000', 0, 100, 1000]: + detail = '大华错误:' + \ + '{}|{}{}{}'.format(str(ret['code']), ret.get('errMsg', ''), + ret.get('desc', ''), str(ret.get('data', ''))) + err_detail = dict(detail=detail, code='dh_'+str(ret['code'])) + 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 'data' in ret else None + except Exception: + self.handle_log(result='error', response=None) + if raise_exception: + raise APIException(**DH_REQUEST_ERROR) + return 'error', DH_REQUEST_ERROR def _get_response_ms(self): """ diff --git a/apps/third/xunxi.py b/apps/third/xunxi.py index 6e78e830..11147d4a 100644 --- a/apps/third/xunxi.py +++ b/apps/third/xunxi.py @@ -31,14 +31,14 @@ class XxClient: self.isGetingToken = False self.headers = {"Connection": "close"} self.log = {} - self._get_token() + # self._get_token() def _get_token(self): self.isGetingToken = True json = { 'licence': self.licence } - is_ok, res = self.request(**xxapis['token_login'], json=json, raise_exception=False) + is_ok, res = self.request(**xxapis['token_login'], json=json, raise_exception=False, timeout=2) if is_ok == 'success': cache.set('xx_token', res['token'], timeout=None) self.isGetingToken = False @@ -52,29 +52,31 @@ 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} - 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.text - 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 - 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 - return 'success', ret['data'] - self.handle_log(result='error', response=ret) - if raise_exception: - raise APIException(**XX_REQUEST_ERROR) - return 'error', XX_REQUEST_ERROR + try: + 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.text + 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 + 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 + return 'success', ret['data'] + except Exception: + self.handle_log(result='error', response=None) + if raise_exception: + raise APIException(**XX_REQUEST_ERROR) + return 'error', XX_REQUEST_ERROR def _get_response_ms(self): """