File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 8
8
from etherscan .enums .fields_enum import FieldsEnum as fields
9
9
from etherscan .utils .parsing import ResponseParser as parser
10
10
11
+ from etherscan .utils .shelve import Shelve as shelve
12
+
11
13
12
14
class Etherscan :
15
+ # Enable/disable caching of all requests
16
+ CACHING = True
17
+
13
18
def __new__ (cls , api_key : str , net : str = "MAIN" ):
14
19
with resources .path (configs , f"{ net .upper ()} -stable.json" ) as path :
15
20
config_path = str (path )
@@ -29,7 +34,14 @@ def wrapper(*args, **kwargs):
29
34
f"{ fields .API_KEY } "
30
35
f"{ api_key } "
31
36
)
37
+
38
+ r = shelve .shelve_load (url )
39
+ if r and Etherscan .CACHING :
40
+ return parser .parse (r )
41
+
32
42
r = requests .get (url , headers = {"User-Agent" : "" })
43
+ shelve .shelve_store (url , r )
44
+
33
45
return parser .parse (r )
34
46
35
47
return wrapper
Original file line number Diff line number Diff line change
1
+ import shelve
2
+
3
+
4
+ class Shelve :
5
+ SHELVE_FILE = r'data/etherscan_shelve'
6
+
7
+ @staticmethod
8
+ def shelve_store (key , data ):
9
+ # store result in cache
10
+ d = shelve .open (Shelve .SHELVE_FILE )
11
+ d [key ] = data
12
+ d .close ()
13
+
14
+ @staticmethod
15
+ def shelve_load (key ):
16
+ d = shelve .open (Shelve .SHELVE_FILE )
17
+ try :
18
+ data = d [key ]
19
+ return data
20
+ except KeyError as err :
21
+ return None
22
+ finally :
23
+ d .close ()
24
+
You can’t perform that action at this time.
0 commit comments