Ejemplo n.º 1
0
 def __get(url, payload):
     header = {'Content-type': 'application/json'}
     try:
         response = requests.get(url,
                                 params=json.dumps(payload),
                                 headers=header,
                                 timeout=10)
     except requests.exceptions.MissingSchema as e:
         raise SDKException(ErrorCode.connect_err(e.args[0]))
     except (requests.exceptions.ConnectionError,
             requests.exceptions.ConnectTimeout):
         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]))
     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]))
     if content['error'] != 0:
         if content['result'] != '':
             raise SDKException(ErrorCode.other_error(content['result']))
         else:
             raise SDKException(ErrorCode.other_error(content['desc']))
     return content
Ejemplo n.º 2
0
 def __get(self, url: str):
     try:
         response = requests.get(url, timeout=10)
     except requests.exceptions.MissingSchema as e:
         raise SDKException(ErrorCode.connect_err(e.args[0]))
     except requests.exceptions.ConnectTimeout:
         raise SDKException(
             ErrorCode.other_error(''.join(['ConnectTimeout: ',
                                            self.__url])))
     except requests.exceptions.ConnectionError:
         raise SDKException(
             ErrorCode.other_error(''.join(
                 ['ConnectionError: ', self.__url])))
     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:
         if response['Result'] != '':
             raise SDKException(ErrorCode.other_error(response['Result']))
         else:
             raise SDKException(ErrorCode.other_error(response['Desc']))
     return response
Ejemplo n.º 3
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:
         raise SDKException(ErrorCode.other_error(''.join(['ConnectTimeout: ', url]))) from None
     except requests.exceptions.ConnectionError:
         raise SDKException(ErrorCode.other_error(''.join(['ConnectionError: ', url]))) from None
     except requests.exceptions.ReadTimeout:
         raise SDKException(ErrorCode.other_error(''.join(['ReadTimeout: ', 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']))
         else:
             raise SDKException(ErrorCode.other_error(content['desc']))
     return content
Ejemplo n.º 4
0
 def __post(self, method, b58_address: str or None, pwd: str or None,
            params):
     header = {'Content-type': 'application/json'}
     payload = dict(qid=str(randint(0, maxsize)),
                    method=method,
                    params=params)
     if isinstance(b58_address, str):
         payload['account'] = b58_address
     if isinstance(pwd, str):
         payload['pwd'] = pwd
     try:
         response = requests.post(self.__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:
         raise SDKException(
             ErrorCode.other_error(''.join(['ConnectTimeout: ',
                                            self.__url]))) from None
     except requests.exceptions.ConnectionError:
         raise SDKException(
             ErrorCode.other_error(''.join(
                 ['ConnectionError: ', self.__url]))) from None
     except requests.exceptions.ReadTimeout:
         raise SDKException(
             ErrorCode.other_error(''.join(['ReadTimeout: ',
                                            self.__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_code'] != 0:
         if content['error_info'] != '':
             raise SDKException(ErrorCode.other_error(
                 content['error_info']))
         else:
             raise SDKException(ErrorCode.other_error(content['result']))
     return content
Ejemplo n.º 5
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
 def test_connect_url_err(self):
     msg = 'TEST'
     desc = "connect error: " + msg
     value = ErrorCode.connect_err(msg)
     self.assertEqual(value["desc"], desc)