Ejemplo n.º 1
0
 def __post(url, payload):
     header = {'Content-type': 'application/json'}
     try:
         response = requests.post(url,
                                  json=payload,
                                  headers=header,
                                  timeout=10)
     except requests.exceptions.MissingSchema as e:
         raise SDKException(ErrorCode.connect_err(e.args[0])) from None
     except (requests.exceptions.ConnectTimeout,
             requests.exceptions.ConnectionError,
             requests.exceptions.ReadTimeout):
         raise SDKException(ErrorCode.connect_timeout(url)) from None
     try:
         content = response.content.decode('utf-8')
     except Exception as e:
         raise SDKException(ErrorCode.other_error(e.args[0])) from None
     if response.status_code != 200:
         raise SDKException(ErrorCode.other_error(content))
     try:
         content = json.loads(content)
     except json.decoder.JSONDecodeError as e:
         raise SDKException(ErrorCode.other_error(e.args[0])) from None
     if content['error'] != 0:
         if content['result'] != '':
             raise SDKException(ErrorCode.other_error(
                 content['result'])) from None
         else:
             raise SDKException(ErrorCode.other_error(
                 content['desc'])) from None
     return content
Ejemplo n.º 2
0
 async def __send_recv(self, msg: dict, is_full: bool):
     if self.__ws_client is None or self.__ws_client.closed:
         try:
             await self.connect()
         except TimeoutError:
             raise SDKException(ErrorCode.connect_timeout(
                 self.__url)) from None
     await self.__ws_client.send(json.dumps(msg))
     response = await self.__ws_client.recv()
     response = json.loads(response)
     if is_full:
         return response
     if response['Error'] != 0:
         raise SDKException(
             ErrorCode.other_error(response.get('Result', '')))
     return response.get('Result', dict())
Ejemplo n.º 3
0
 def __post(self, url: str, data: str):
     try:
         response = requests.post(url, data=data, timeout=10)
     except requests.exceptions.MissingSchema as e:
         raise SDKException(ErrorCode.connect_err(e.args[0]))
     except (requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError):
         raise SDKException(ErrorCode.connect_timeout(self._url)) from None
     if response.status_code != 200:
         raise SDKException(ErrorCode.other_error(response.content.decode('utf-8')))
     try:
         response = json.loads(response.content.decode('utf-8'))
     except json.decoder.JSONDecodeError as e:
         raise SDKException(ErrorCode.other_error(e.args[0]))
     if response['Error'] != 0:
         raise SDKException(ErrorCode.other_error(response['Result']))
     return response
Ejemplo n.º 4
0
 async def __post(self, url: str, data: str):
     try:
         if self.__session is None:
             async with ClientSession() as session:
                 async with session.post(url, data=data, timeout=10) as response:
                     res = json.loads(await response.content.read(-1))
         else:
             async with self.__session.post(url, data=data, timeout=10) as response:
                 res = json.loads(await response.content.read(-1))
         if res['Error'] != 0:
             if res['Result'] != '':
                 raise SDKException(ErrorCode.other_error(res['Result']))
             else:
                 raise SDKException(ErrorCode.other_error(res['Desc']))
         return res
     except (asyncio.TimeoutError, client_exceptions.ClientConnectorError):
         raise SDKException(ErrorCode.connect_timeout(self._url)) from None
Ejemplo n.º 5
0
 async def __post(self, payload):
     header = {'Content-type': 'application/json'}
     try:
         if self._session is None:
             async with ClientSession() as session:
                 async with session.post(self._url, json=payload, headers=header, timeout=10) as response:
                     res = json.loads(await response.content.read(-1))
         else:
             async with self._session.post(self._url, json=payload, headers=header, timeout=10) as response:
                 res = json.loads(await response.content.read(-1))
         if res['error'] != 0:
             if res['result'] != '':
                 raise SDKException(ErrorCode.other_error(res['result']))
             else:
                 raise SDKException(ErrorCode.other_error(res['desc']))
     except (asyncio.TimeoutError, client_exceptions.ClientConnectorError):
         raise SDKException(ErrorCode.connect_timeout(self._url)) from None
     return res