Skip to content

Commit 621a8a2

Browse files
author
Christian Gumpert
committed
make URL endpoint configurable
1 parent e059d7c commit 621a8a2

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

etherscan/etherscan.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010

1111

1212
class Etherscan:
13-
def __new__(cls, api_key: str, net: str = "MAIN"):
13+
def __new__(cls, api_key: str, endpoint: str = "https://api.etherscan.io/api?", net: str = "MAIN"):
1414
with resources.path(configs, f"{net.upper()}-stable.json") as path:
1515
config_path = str(path)
16-
return cls.from_config(api_key=api_key, config_path=config_path, net=net)
16+
return cls.from_config(api_key=api_key, endpoint=endpoint, config_path=config_path)
1717

1818
@staticmethod
1919
def __load_config(config_path: str) -> dict:
2020
with open(config_path, "r") as f:
2121
return json.load(f)
2222

2323
@staticmethod
24-
def __run(func, api_key: str, net: str):
24+
def __run(func, endpoint: str, api_key: str):
2525
def wrapper(*args, **kwargs):
2626
url = (
27-
f"{fields.PREFIX.format(net.lower()).replace('-main','')}"
27+
f"{endpoint}"
2828
f"{func(*args, **kwargs)}"
2929
f"{fields.API_KEY}"
3030
f"{api_key}"
@@ -35,10 +35,10 @@ def wrapper(*args, **kwargs):
3535
return wrapper
3636

3737
@classmethod
38-
def from_config(cls, api_key: str, config_path: str, net: str):
38+
def from_config(cls, api_key: str, endpoint: str, config_path: str):
3939
config = cls.__load_config(config_path)
4040
for func, v in config.items():
4141
if not func.startswith("_"): # disabled if _
4242
attr = getattr(getattr(etherscan, v["module"]), func)
43-
setattr(cls, func, cls.__run(attr, api_key, net))
43+
setattr(cls, func, cls.__run(attr, endpoint=endpoint, api_key=api_key))
4444
return cls

test/test_modules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def methods(self, net):
2929
print(f"\nNET: {net}")
3030
print(f"MODULE: {self._MODULE}")
3131
config = load(CONFIG_PATH.format(net))
32-
etherscan = Etherscan(API_KEY, net)
32+
etherscan = Etherscan(api_key=API_KEY, net=net)
3333
for fun, v in config.items():
3434
if not fun.startswith("_"): # disabled if _
3535
if v["module"] == self._MODULE:

0 commit comments

Comments
 (0)