-
Notifications
You must be signed in to change notification settings - Fork 1
/
trongrid.py
59 lines (48 loc) · 1.68 KB
/
trongrid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import requests
import json
import util
trongrid_url = 'https://api.trongrid.io'
#https://api.trongrid.io/wallet/getblockbynum
class ApiClient():
def __init__(self):
self.__url = trongrid_url + '/wallet/'
def send_post(self, url, data=None):
data = json.dumps(data)
response = requests.post(url, data)
if response.status_code > 201:
try:
error = response.json()
except: # response.content not formatted as JSON
error = str(response.content)
raise APIError('TronGrid API returned HTTP %s (%s)' % (response.status_code, error))
else:
try:
return response.json()
except: # Nothing to return
return {}
def __post(self, url, data=None, retry = True):
while(True):
ret = {}
try:
ret = self.send_post(url, data)
except Exception, err:
if retry:
again = True
util.log_info('retry https post:%s because of %s' % (url, err.message))
else:
raise err
return ret
def get_block_by_number(self, number):
url = self.__url + 'getblockbynum'
return self.__post(url, data = {'num':number})
def get_now_block(self):
url = self.__url + 'getnowblock'
return self.__post(url)
def get_now_blocknumber(self):
block = self.get_now_block()
return block['block_header']['raw_data']['number']
def listwitnesses(self):
url = self.__url + 'listwitnesses'
return self.__post(url)
class APIError(Exception):
pass