1
1
import requests
2
2
import json
3
-
3
+ import collections
4
4
5
5
# Assume user puts his API key in the api_key.json file under variable name "key"
6
6
class Client (object ):
7
7
dao_address = '0xbb9bc244d798123fde783fcc1c72d3bb8c189413'
8
- URL_BASES = dict (
9
- prefix = 'https://api.etherscan.io/' ,
10
- module = 'api?module=' ,
11
- action = '&action=' ,
12
- tag = '&tag=' ,
13
- offset = '&offset=' ,
14
- page = '&page=' ,
15
- sort = '&sort=' ,
16
- blocktype = '&blocktype=' ,
17
- key = '&apikey=' ,
18
- address = '&address=' ,
19
- )
20
8
21
- def __init__ (self , address , api_key = 'YourApiKeyToken' ):
9
+ # Constants
10
+ PREFIX = 'https://api.etherscan.io/api?'
11
+ MODULE = 'module='
12
+ ACTION = '&action='
13
+ TOKEN_NAME = '&tokenname='
14
+ CONTRACT_ADDRESS = '&contractaddress='
15
+ ADDRESS = '&address='
16
+ OFFSET = '&offset='
17
+ PAGE = '&page='
18
+ SORT = '&sort='
19
+ BLOCK_TYPE = '&blocktype='
20
+ TO = '&to='
21
+ VALUE = '&value='
22
+ DATA = '&data='
23
+ POSITION = '&='
24
+ HEX = '&hex='
25
+ GAS_PRICE = '&gasPrice='
26
+ GAS = '&gas='
27
+ START_BLOCK = '&startblock='
28
+ END_BLOCK = '&endblock='
29
+ BLOCKNO = '&blockno='
30
+ TXHASH = '&txhash='
31
+ TAG = '&tag='
32
+ BOOLEAN = '&boolean='
33
+ INDEX = '&index='
34
+ API_KEY = '&apikey='
35
+
36
+ url_dict = {}
37
+
38
+ def __init__ (self , address , api_key = '' ):
22
39
self .http = requests .session ()
23
- self .url = ''
24
- self .module = ''
25
- self .action = ''
26
- self .tag = ''
27
- self .offset = ''
28
- self .page = ''
29
- self .sort = ''
30
- self .blocktype = ''
40
+ self .url_dict = collections .OrderedDict (
41
+ {
42
+ self .MODULE : '' ,
43
+ self .ADDRESS : '' ,
44
+ self .OFFSET : '' ,
45
+ self .PAGE : '' ,
46
+ self .SORT : '' ,
47
+ self .BLOCK_TYPE : '' ,
48
+ self .TO : '' ,
49
+ self .VALUE : '' ,
50
+ self .DATA : '' ,
51
+ self .POSITION : '' ,
52
+ self .HEX : '' ,
53
+ self .GAS_PRICE : '' ,
54
+ self .GAS : '' ,
55
+ self .START_BLOCK : '' ,
56
+ self .END_BLOCK : '' ,
57
+ self .BLOCKNO : '' ,
58
+ self .TXHASH : '' ,
59
+ self .TAG : '' ,
60
+ self .BOOLEAN : '' ,
61
+ self .INDEX : '' ,
62
+ self .API_KEY : api_key ,
63
+ })
31
64
32
- self .API_KEY = str (api_key )
65
+ # self.url_dict[ API_KEY] = str(api_key)
33
66
self .check_and_get_api ()
34
- self .key = self .URL_BASES ['key' ] + self .API_KEY
67
+ # self.key = self.URL_BASES['key'] + self.API_KEY
68
+
35
69
if (len (address ) > 20 ) and (type (address ) == list ):
36
70
print ("Etherscan only takes 20 addresses at a time" )
37
71
quit ()
38
72
elif (type (address ) == list ) and (len (address ) <= 20 ):
39
- self .address = self .URL_BASES [ 'address' ] + ',' .join (address )
73
+ self .url_dict [ self .ADDRESS ] = ',' .join (address )
40
74
else :
41
- self .address = self .URL_BASES ['address' ] + address
75
+ self .url_dict [self .ADDRESS ] = address
76
+
77
+ def build_url (self ):
78
+ self .url = self .PREFIX + '' .join ([k + v if v else '' for k , v in self .url_dict .items ()]) # TODO: better naming
42
79
43
80
def connect (self ):
44
81
# TODO: deal with "unknown exception" error
@@ -54,11 +91,11 @@ def connect(self):
54
91
print ("Invalid Request" )
55
92
exit ()
56
93
else :
57
- print ("problem with connection, status code: " , req .status_code )
94
+ print ("Problem with connection, status code: " , req .status_code )
58
95
exit ()
59
96
60
97
def check_and_get_api (self ):
61
- if self .API_KEY != 'YourApiKeyToken' :
98
+ if self .url_dict [ self . API_KEY ]: # Check if api_key is empty string
62
99
pass
63
100
else :
64
- self .API_KEY = input ('Please type your EtherScan.io API key: ' )
101
+ self .url_dict [ self . API_KEY ] = input ('Please type your EtherScan.io API key: ' )
0 commit comments