|
10 | 10 |
|
11 | 11 |
|
12 | 12 | class Etherscan:
|
13 |
| - def __new__(cls, api_key: str): |
14 |
| - with resources.path(configs, "stable.json") as path: |
| 13 | + def __new__(cls, api_key: str, net: str = "MAIN"): |
| 14 | + with resources.path(configs, f"{net.upper()}-stable.json") as path: |
15 | 15 | config_path = str(path)
|
16 |
| - return cls.from_config(api_key=api_key, config_path=config_path) |
| 16 | + return cls.from_config(api_key=api_key, config_path=config_path, net=net) |
17 | 17 |
|
18 | 18 | @staticmethod
|
19 | 19 | def __load_config(config_path: str) -> dict:
|
20 | 20 | with open(config_path, "r") as f:
|
21 | 21 | return json.load(f)
|
22 | 22 |
|
23 | 23 | @staticmethod
|
24 |
| - def __run(func, api_key: str): |
| 24 | + def __run(func, api_key: str, net: str): |
25 | 25 | def wrapper(*args, **kwargs):
|
26 | 26 | url = (
|
27 |
| - f"{fields.PREFIX}" |
| 27 | + f"{fields.PREFIX.format(net.lower()).replace('-main','')}" |
28 | 28 | f"{func(*args, **kwargs)}"
|
29 | 29 | f"{fields.API_KEY}"
|
30 | 30 | f"{api_key}"
|
31 | 31 | )
|
32 |
| - r = requests.get(url) |
| 32 | + r = requests.get(url, headers={'User-Agent':''}) |
33 | 33 | return parser.parse(r)
|
34 | 34 |
|
35 | 35 | return wrapper
|
36 | 36 |
|
37 | 37 | @classmethod
|
38 |
| - def from_config(cls, api_key: str, config_path: str): |
| 38 | + def from_config(cls, api_key: str, config_path: str, net: str): |
39 | 39 | config = cls.__load_config(config_path)
|
40 | 40 | for func, v in config.items():
|
41 | 41 | if not func.startswith("_"): # disabled if _
|
42 | 42 | attr = getattr(getattr(etherscan, v["module"]), func)
|
43 |
| - setattr(cls, func, cls.__run(attr, api_key)) |
| 43 | + setattr(cls, func, cls.__run(attr, api_key, net)) |
44 | 44 | return cls
|
0 commit comments