Skip to content

Commit b0d3ecd

Browse files
committed
[W.I.P.] URL Build Alternate Design
In order to improve code reuse, I completely redesigned the way that URLs are built. I moved the make_url function (now renamed build_url) to the Client class. I thought this to be a better solution as opposed to having the function implemented in every child class. A few changes still need to be made in order to increase readability.
1 parent 1bd91aa commit b0d3ecd

File tree

1 file changed

+66
-29
lines changed

1 file changed

+66
-29
lines changed

etherscan/client.py

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,81 @@
11
import requests
22
import json
3-
3+
import collections
44

55
# Assume user puts his API key in the api_key.json file under variable name "key"
66
class Client(object):
77
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-
)
208

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=''):
2239
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+
})
3164

32-
self.API_KEY = str(api_key)
65+
# self.url_dict[API_KEY] = str(api_key)
3366
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+
3569
if (len(address) > 20) and (type(address) == list):
3670
print("Etherscan only takes 20 addresses at a time")
3771
quit()
3872
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)
4074
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
4279

4380
def connect(self):
4481
# TODO: deal with "unknown exception" error
@@ -54,11 +91,11 @@ def connect(self):
5491
print("Invalid Request")
5592
exit()
5693
else:
57-
print("problem with connection, status code: ", req.status_code)
94+
print("Problem with connection, status code: ", req.status_code)
5895
exit()
5996

6097
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
6299
pass
63100
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

Comments
 (0)