-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnowtrace.py
More file actions
44 lines (36 loc) · 1.43 KB
/
snowtrace.py
File metadata and controls
44 lines (36 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import json
from importlib import resources
import requests
import snowtrace
from snowtrace import configs
from snowtrace.enums.fields_enum import FieldsEnum as fields
from snowtrace.utils.parsing import ResponseParser as parser
class Snowtrace:
def __new__(cls, api_key: str, net: str = "MAIN"):
with resources.path(configs, f"{net.upper()}-stable.json") as path:
config_path = str(path)
return cls.from_config(api_key=api_key, config_path=config_path, net=net)
@staticmethod
def __load_config(config_path: str) -> dict:
with open(config_path, "r") as f:
return json.load(f)
@staticmethod
def __run(func, api_key: str, net: str):
def wrapper(*args, **kwargs):
url = (
f"{fields.PREFIX.format(net.lower()).replace('-main','')}"
f"{func(*args, **kwargs)}"
f"{fields.API_KEY}"
f"{api_key}"
)
r = requests.get(url, headers={"User-Agent": ""})
return parser.parse(r)
return wrapper
@classmethod
def from_config(cls, api_key: str, config_path: str, net: str):
config = cls.__load_config(config_path)
for func, v in config.items():
if not func.startswith("_"): # disabled if _
attr = getattr(getattr(snowtrace, v["module"]), func)
setattr(cls, func, cls.__run(attr, api_key, net))
return cls