Skip to content

Commit d9178a7

Browse files
committed
Redesigned URL build
To increase code reuse I have moved the build_url method from the child classes and into the parent class, 'Client'. I also desgined a highly pythonic way of contructing the url. The children classes are repsonible for setting the values of the api parameters that are located within the parent class.
1 parent 8e4a7e8 commit d9178a7

File tree

9 files changed

+37
-63
lines changed

9 files changed

+37
-63
lines changed
178 Bytes
Binary file not shown.
4.62 KB
Binary file not shown.
2.7 KB
Binary file not shown.
797 Bytes
Binary file not shown.
1005 Bytes
Binary file not shown.
1.08 KB
Binary file not shown.

etherscan/accounts.py

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,19 @@
55
class Account(Client):
66
def __init__(self, address=Client.dao_address, api_key='YourApiKeyToken'):
77
Client.__init__(self, address=address, api_key=api_key)
8-
self.module = self.URL_BASES['module'] + 'account'
9-
10-
def make_url(self, call_type=''):
11-
if call_type == 'balance':
12-
self.url = self.URL_BASES['prefix'] \
13-
+ self.module \
14-
+ self.action \
15-
+ self.address \
16-
+ self.tag \
17-
+ self.key
18-
elif call_type == 'transactions':
19-
self.url = self.URL_BASES['prefix'] \
20-
+ self.module \
21-
+ self.action \
22-
+ self.address \
23-
+ self.offset \
24-
+ self.page \
25-
+ self.key
26-
elif call_type == 'blocks':
27-
self.url = self.URL_BASES['prefix'] \
28-
+ self.module \
29-
+ self.action \
30-
+ self.address \
31-
+ self.blocktype \
32-
+ self.offset \
33-
+ self.page \
34-
+ self.key
8+
self.url_dict[self.MODULE] = 'account'
359

3610
def get_balance(self):
37-
self.action = self.URL_BASES['action'] + 'balance'
38-
self.tag = self.URL_BASES['tag'] + 'latest'
39-
self.make_url(call_type='transactions')
11+
self.url_dict[self.ACTION] = 'balance'
12+
self.url_dict[self.TAG] = 'latest'
13+
self.build_url()
4014
req = self.connect()
4115
return req['result']
4216

4317
def get_balance_multiple(self):
44-
self.action = self.URL_BASES['action'] + 'balancemulti'
45-
self.tag = self.URL_BASES['tag'] + 'latest'
46-
self.make_url(call_type='balance')
18+
self.url_dict[self.ACTION] = 'balancemulti'
19+
self.url_dict[self.TAG] = 'latest'
20+
self.build_url()
4721
req = self.connect()
4822
return req['result']
4923

@@ -78,29 +52,29 @@ def get_transaction_page(self, page=1, offset=10000, sort='asc', internal=False)
7852
False -> (default) get normal external transactions
7953
"""
8054
if internal:
81-
self.action = self.URL_BASES['action'] + 'txlistinternal'
55+
self.url_dict[self.ACTION] = 'txlistinternal'
8256
else:
83-
self.action = self.URL_BASES['action'] + 'txlist'
84-
self.page = self.URL_BASES['page'] + str(page)
85-
self.offset = self.URL_BASES['offset'] + str(offset)
86-
self.sort = self.URL_BASES['sort'] + sort
87-
self.make_url(call_type='transactions')
57+
self.url_dict[self.ACTION] = 'txlist'
58+
self.url_dict[self.PAGE] = str(page)
59+
self.url_dict[self.OFFSET] = str(offset)
60+
self.url_dict[self.SORT] = sort
61+
self.build_url()
8862
req = self.connect()
8963
return req['result']
9064

9165
def get_all_transactions(self, offset=10000, sort='asc', internal=False) -> list:
9266
if internal:
93-
self.action = self.URL_BASES['action'] + 'txlistinternal'
67+
self.url_dict[self.ACTION] = 'txlistinternal'
9468
else:
95-
self.action = self.URL_BASES['action'] + 'txlist'
96-
self.page = self.URL_BASES['page'] + str(1)
97-
self.offset = self.URL_BASES['offset'] + str(offset)
98-
self.sort = self.URL_BASES['sort'] + sort
99-
self.make_url(call_type='transactions')
69+
self.url_dict[self.ACTION] = 'txlist'
70+
self.url_dict[self.PAGE] = str(1)
71+
self.url_dict[self.OFFSET] = str(offset)
72+
self.url_dict[self.SORT] = sort
73+
self.build_url()
10074

10175
trans_list = []
10276
while True:
103-
self.make_url(call_type='transactions')
77+
self.build_url()
10478
req = self.connect()
10579
if "No transactions found" in req['message']:
10680
print("Total number of transactions: {}".format(len(trans_list)))
@@ -109,9 +83,9 @@ def get_all_transactions(self, offset=10000, sort='asc', internal=False) -> list
10983
else:
11084
trans_list += req['result']
11185
# Find any character block that is a integer of any length
112-
page_number = re.findall(r'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0', self.page)
86+
page_number = re.findall(r'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0', self.url_dict[self.PAGE])
11387
print("page {} added".format(page_number[0]))
114-
self.page = self.URL_BASES['page'] + str(int(page_number[0]) + 1)
88+
self.url_dict[self.PAGE] = str(int(page_number[0]) + 1)
11589

11690
def get_blocks_mined_page(self, blocktype='blocks', page=1, offset=10000) -> list:
11791
"""
@@ -124,22 +98,22 @@ def get_blocks_mined_page(self, blocktype='blocks', page=1, offset=10000) -> lis
12498
'blocks' -> full blocks only
12599
'uncles' -> uncles only
126100
"""
127-
self.action = self.URL_BASES['action'] + 'getminedblocks'
128-
self.blocktype = self.URL_BASES['blocktype'] + blocktype
129-
self.page = self.URL_BASES['page'] + str(page)
130-
self.offset = self.URL_BASES['offset'] + str(offset)
131-
self.make_url(call_type='blocks')
101+
self.url_dict[self.ACTION] = 'getminedblocks'
102+
self.url_dict[self.BLOCK_TYPE] = blocktype
103+
self.url_dict[self.PAGE] = str(page)
104+
self.url_dict[self.OFFSET] = str(offset)
105+
self.build_url()
132106
req = self.connect()
133107
return req['result']
134108

135109
def get_all_blocks_mined(self, blocktype='blocks', offset=10000) -> list:
136-
self.action = self.URL_BASES['action'] + 'getminedblocks'
137-
self.blocktype = self.URL_BASES['blocktype'] + blocktype
138-
self.page = self.URL_BASES['page'] + str(1)
139-
self.offset = self.URL_BASES['offset'] + str(offset)
110+
self.url_dict[self.ACTION] = 'getminedblocks'
111+
self.url_dict[self.BLOCK_TYPE] = blocktype
112+
self.url_dict[self.PAGE] = str(1)
113+
self.url_dict[self.OFFSET] = str(offset)
140114
blocks_list = []
141115
while True:
142-
self.make_url(call_type='blocks')
116+
self.build_url()
143117
req = self.connect()
144118
print(req['message'])
145119
if "No transactions found" in req['message']:
@@ -148,9 +122,9 @@ def get_all_blocks_mined(self, blocktype='blocks', offset=10000) -> list:
148122
else:
149123
blocks_list += req['result']
150124
# Find any character block that is a integer of any length
151-
page_number = re.findall(r'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0', self.page)
125+
page_number = re.findall(r'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0', self.url_dict[self.PAGE])
152126
print("page {} added".format(page_number[0]))
153-
self.page = self.URL_BASES['page'] + str(int(page_number[0]) + 1)
127+
self.url_dict[self.PAGE] = str(int(page_number[0]) + 1)
154128

155129
def get_internal_by_hash(self, tx_hash=''):
156130
"""

etherscan/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def __init__(self, address, api_key=''):
7575
self.url_dict[self.ADDRESS] = address
7676

7777
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
79-
78+
self.url = self.PREFIX + ''.join([param + val if val else '' for param, val in self.url_dict.items()]) # TODO: better naming
79+
print(self.url)
8080
def connect(self):
8181
# TODO: deal with "unknown exception" error
8282
try:

etherscan/contracts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class Contract(Client):
55
def __init__(self, address=Client.dao_address, api_key='YourApiKeyToken'):
66
Client.__init__(self, address=address, api_key=api_key)
7-
self.url_dict[self.MODULE] = 'contract'
7+
self.url_dict[self.MODULE] = 'contract'
88

99
def get_abi(self):
1010
self.url_dict[self.ACTION] = 'getabi'

0 commit comments

Comments
 (0)