def pull_request(currency, identifier, useragent): try: client = SteemNodeRPC(get_api_url(currency)) return client.get_account(identifier).get('balance').split(' ')[0] # Need to know which exceptions might throwing here except (ValueError, KeyError) as error: return error
def pull_request(currency, identifier, useragent): # TODO: I have noticed that time to time the following api is working very slow: # TODO: http://xcp.blockscan.com/api2?module=address&action=balance&btc_address={identifier} # TODO: I think we need to find any alternative api. try: request = Request(get_api_url(currency).format(identifier=identifier), method='GET') request.add_header('User-Agent', useragent) with urlopen(request, timeout=60) as f: response = json.loads(f.read().decode('utf-8')) if response['status'] == 'error': return response['message'] elif response['status'] == 'success': # TODO: We have the ability to return all the assets data = dict() for asset in response['data']: data[asset['asset']] = asset['balance'] return data['XCP'] except HTTPError as error: return error.reason except URLError as error: return error.reason except (ValueError, KeyError) as error: return error
def test_get_websocket(self): currencies = ['STEEM', 'GOLOS'] for i in range(0, len(currencies)): with self.subTest(i=i): api_url = get_api_url(currencies[i]) self.assertRegex(api_url, '^wss[:]{1}[/]{2}(?!/).+$', 'Function returns: {}'.format(api_url)) print('Function get_api_url for {} returns: {}'.format( currencies[i], api_url))
def test_get_api_url(self): currencies = get_supported_currencies('list') currencies.remove('GOLOS') currencies.remove('STEEM') for i in range(0, len(currencies)): with self.subTest(i=i): api_url = get_api_url(currencies[i]) self.assertRegex( api_url, '(^http[:]{1}[/]{2}(?!/).+$)|(^https[:]{1}[/]{2}(?!/).+$)', 'Function returns: {}'.format(api_url)) print('Function get_api_url for {} returns: {}'.format( currencies[i], api_url))
def pull_request(currency, identifier, useragent): try: request = Request(get_api_url(currency).format(identifier=identifier), method='GET') request.add_header('User-Agent', useragent) with urlopen(request, timeout=60) as f: return f.read().decode('utf-8') except HTTPError as error: return error.reason except URLError as error: return error.reason except (ValueError, KeyError) as error: return error
def pull_request(currency, identifier, useragent): try: request = Request(get_api_url(currency).format(identifier=identifier), method='GET') request.add_header('User-Agent', useragent) with urlopen(request, timeout=60) as f: response = json.loads(f.read().decode('utf-8')) return str(response['balance']) except HTTPError as error: response = json.loads(error.read().decode('utf-8')) return 'Error: {}. Reason: {}'.format(error.reason, response['ErrorCode']) except URLError as error: return error.reason except (ValueError, KeyError) as error: return error
def pull_request(currency, identifier, useragent): try: request = Request(get_api_url(currency).format(network=currency, identifier=identifier), method='GET') request.add_header('User-Agent', useragent) with urlopen(request, timeout=60) as f: return json.loads( f.read().decode('utf-8'))['data']['confirmed_balance'] except HTTPError as error: response = json.loads(error.read().decode('utf-8')) return "{}. {}".format(response['data']['network'], response['data']['address']) except URLError as error: return error.reason except (ValueError, KeyError) as error: return error
def pull_request(currency, identifier, useragent): try: request = Request(get_api_url(currency).format( identifier=identifier.replace('-', '')), method='GET') request.add_header('User-Agent', useragent) with urlopen(request, timeout=60) as f: response = json.loads(f.read().decode('utf-8')) if 'error' in response: return 'Error: {}. Reason: {}'.format(response['error'], response['message']) return str(converter(response['account']['balance'])) except HTTPError as error: return error.reason except URLError as error: return error.reason except (ValueError, KeyError) as error: return error
def get_peers(currency, useragent): try: # Getting list of available peers from: http://nxtpeers.com/api/peers.php # Further we can to get balance connecting to peer on port 7876. # For example: http://{ip or domain of peer}:7876/nxt?requestType=getBalance&account=NXT-7LB8-8ZPX-3YR9-3L85B request = Request(get_api_url(currency), method='GET') request.add_header('User-Agent', useragent) with urlopen(request, timeout=60) as f: peers = list() response = json.loads(f.read().decode('utf-8')) for i in response: peers.append(i['screenname']) return peers except HTTPError as error: return error.reason except URLError as error: return error.reason except (ValueError, KeyError) as error: return error
def pull_request(currency, identifier, useragent): # TODO: We need to perform validation wallet because api returns always positive response. # TODO: For example: http://api.etherscan.io/api?module=account&action=balance&address=bla-bla-bla&tag=latest # TODO: returns {"status":"1","message":"OK","result":"0"}, but it's not mean that balance is zero. # TODO: This is case when address of wallet not valid try: request = Request(get_api_url(currency).format(identifier=identifier), method='GET') request.add_header('User-Agent', useragent) with urlopen(request, timeout=60) as f: response = json.loads(f.read().decode('utf-8')) if response['message'] == 'NOTOK': # TODO: We need return more informative message if api returns error. It's not good: "NOTOK. Error!" return "{}. {}".format(response['message'], response['result']) return str(converter(int(response['result']))) except HTTPError as error: return error.reason except URLError as error: return error.reason except (ValueError, KeyError) as error: return error