refector: 重构一点third中的三方client代码
This commit is contained in:
parent
0e0262d170
commit
33dba0a30e
|
@ -29,11 +29,12 @@ class DhClient:
|
||||||
大华
|
大华
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, client_id=settings.DAHUA_CLIENTID,
|
def __init__(self):
|
||||||
client_secret=settings.DAHUA_SECRET):
|
self.dahua_enabled = getattr(settings, 'DAHUA_ENABLED', False)
|
||||||
if settings.DAHUA_ENABLED:
|
if self.dahua_enabled:
|
||||||
self.client_id = client_id
|
self.dahua_enabled = True
|
||||||
self.client_secret = client_secret
|
self.client_id = settings.DAHUA_CLIENTID
|
||||||
|
self.client_secret = settings.DAHUA_SECRET
|
||||||
self.headers = {"Connection": "close"}
|
self.headers = {"Connection": "close"}
|
||||||
self.isGetingToken = False
|
self.isGetingToken = False
|
||||||
self.log = {}
|
self.log = {}
|
||||||
|
@ -54,7 +55,7 @@ class DhClient:
|
||||||
|
|
||||||
def request(self, url: str, method: str, params=dict(), json=dict(), timeout=120,
|
def request(self, url: str, method: str, params=dict(), json=dict(), timeout=120,
|
||||||
file_path_rela=None, raise_exception=True):
|
file_path_rela=None, raise_exception=True):
|
||||||
if not settings.DAHUA_ENABLED:
|
if not self.dahua_enabled:
|
||||||
raise ParseError('大华对接未启用')
|
raise ParseError('大华对接未启用')
|
||||||
self.headers['Authorization'] = 'bearer ' + cache.get('dh_token', '')
|
self.headers['Authorization'] = 'bearer ' + cache.get('dh_token', '')
|
||||||
self.headers['User-Id'] = '1'
|
self.headers['User-Id'] = '1'
|
||||||
|
|
|
@ -24,7 +24,8 @@ class KingClient(HandleLogMixin):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
if settings.KING_ENABLED:
|
self.king_enabled = getattr(settings, 'KING_ENABLED', False)
|
||||||
|
if self.king_enabled:
|
||||||
self.headers = {}
|
self.headers = {}
|
||||||
self.log = {}
|
self.log = {}
|
||||||
# self._get_token()
|
# self._get_token()
|
||||||
|
@ -45,7 +46,7 @@ class KingClient(HandleLogMixin):
|
||||||
cache.set('king_token', res['Authorization'], timeout=None)
|
cache.set('king_token', res['Authorization'], timeout=None)
|
||||||
|
|
||||||
def request(self, url: str, method: str = 'post', params=dict(), json=dict(), timeout=20, raise_exception=True):
|
def request(self, url: str, method: str = 'post', params=dict(), json=dict(), timeout=20, raise_exception=True):
|
||||||
if not settings.KING_ENABLED:
|
if not self.king_enabled:
|
||||||
raise ParseError('亚控对接未启用')
|
raise ParseError('亚控对接未启用')
|
||||||
self.headers['Authorization'] = cache.get('king_token', '')
|
self.headers['Authorization'] = cache.get('king_token', '')
|
||||||
self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method,
|
self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method,
|
||||||
|
|
|
@ -26,16 +26,15 @@ class SpClient:
|
||||||
音响接口
|
音响接口
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, username=settings.SP_USERNAME,
|
def __init__(self) -> None:
|
||||||
password=settings.SP_PASSWORD) -> None:
|
self.sp_enabled = getattr(settings, 'SP_ENABLED', False)
|
||||||
if not settings.SP_ENABLED:
|
if self.sp_enabled:
|
||||||
return None
|
self.username = settings.SP_USERNAME
|
||||||
self.username = username
|
self.password = settings.SP_PASSWORD
|
||||||
self.password = password
|
self.headers = {"Connection": "close"}
|
||||||
self.headers = {"Connection": "close"}
|
self.isGetingToken = False
|
||||||
self.isGetingToken = False
|
self.log = {}
|
||||||
self.log = {}
|
self._get_token()
|
||||||
self._get_token()
|
|
||||||
|
|
||||||
def _get_token(self):
|
def _get_token(self):
|
||||||
self.isGetingToken = True
|
self.isGetingToken = True
|
||||||
|
@ -52,7 +51,7 @@ class SpClient:
|
||||||
|
|
||||||
def request(self, url: str, method: str, params=dict(), json=dict(), timeout=120,
|
def request(self, url: str, method: str, params=dict(), json=dict(), timeout=120,
|
||||||
file_path_rela=None, raise_exception=True):
|
file_path_rela=None, raise_exception=True):
|
||||||
if not settings.SP_ENABLED:
|
if not self.sp_enabled:
|
||||||
raise ParseError('音响对接未启用')
|
raise ParseError('音响对接未启用')
|
||||||
self.headers['Authorization'] = 'Bearer ' + cache.get('sp_token', '')
|
self.headers['Authorization'] = 'Bearer ' + cache.get('sp_token', '')
|
||||||
self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method,
|
self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method,
|
||||||
|
|
|
@ -25,10 +25,11 @@ class XxClient:
|
||||||
寻息
|
寻息
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, licence=settings.XX_LICENCE, username=settings.XX_USERNAME) -> None:
|
def __init__(self) -> None:
|
||||||
if settings.XX_ENABLED:
|
self.xx_enabled = getattr(settings, 'XX_ENABLED', False)
|
||||||
self.licence = licence
|
if self.xx_enabled:
|
||||||
self.username = username
|
self.licence = settings.XX_LICENCE
|
||||||
|
self.username = settings.XX_USERNAME
|
||||||
self.isGetingToken = False
|
self.isGetingToken = False
|
||||||
self.headers = {"Connection": "close"}
|
self.headers = {"Connection": "close"}
|
||||||
self.log = {}
|
self.log = {}
|
||||||
|
@ -45,7 +46,7 @@ class XxClient:
|
||||||
self.isGetingToken = False
|
self.isGetingToken = False
|
||||||
|
|
||||||
def request(self, url: str, method: str = 'post', params=dict(), json=dict(), timeout=20, raise_exception=True):
|
def request(self, url: str, method: str = 'post', params=dict(), json=dict(), timeout=20, raise_exception=True):
|
||||||
if not settings.XX_ENABLED:
|
if not self.xx_enabled:
|
||||||
raise ParseError('寻息对接未启用')
|
raise ParseError('寻息对接未启用')
|
||||||
params['accessToken'] = cache.get('xx_token', '')
|
params['accessToken'] = cache.get('xx_token', '')
|
||||||
json['username'] = self.username
|
json['username'] = self.username
|
||||||
|
|
Loading…
Reference in New Issue