def __post(self, method, b58_address: str or None, pwd: str or None, params): 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=self.__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): 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
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
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)