1+ import json
2+ import os
3+ from typing import Dict , List , Optional , Any
4+ from dataclasses import dataclass
5+
6+ @dataclass
7+ class Node :
8+ nodes : List [str ]
9+ query : Dict [str , Any ]
10+ params : Dict [str , Any ]
11+
12+ class NodeReader :
13+ def __init__ (self ) -> None :
14+ current = os .getcwd ()
15+ self .path = os .path .join (current , "nodes.json" )
16+ self .nodes : List [Node ] = []
17+
18+ @property
19+ def data (self ) -> List [Node ]:
20+ return self .nodes
21+
22+ @data .setter
23+ def data (self , value : List [Node ]) -> None :
24+ self .nodes = value
25+
26+ @staticmethod
27+ def reader ():
28+ try :
29+ current = os .path .dirname (os .path .abspath (__file__ ))
30+ node_path = os .path .join (current , 'nodes.json' )
31+ if not os .path .exists (node_path ):
32+ raise FileNotFoundError (f"Config file not found at: { node_path } " )
33+
34+ with open (node_path , 'r' ) as f :
35+ content = json .load (f )
36+ return content
37+ except Exception as e :
38+ print (f"Failed to NodeReader: { str (e )} " )
39+ raise
40+
41+ @classmethod
42+ def get_nodes (cls ) -> List [Node ]:
43+ reader = cls .reader ()
44+ nodes = reader .get ('nodes' )
45+ if not nodes :
46+ raise ValueError ("No nodes found in config file" )
47+ return nodes
48+
49+ @classmethod
50+ def get_query (cls , _Query : str ) -> Optional [str ]:
51+ """
52+ Standard queries:
53+ - address
54+ - tx
55+ - block
56+ - blockIndex
57+ """
58+ query_standard = ["address" , "tx" , "block" , "blockIndex" ]
59+ if _Query not in query_standard :
60+ raise ValueError (f"Invalid query type: { _Query } " )
61+ reader = cls .reader ()
62+ _query = reader .get ('query' )
63+ if not _query :
64+ raise ValueError ("No nodes found in config file" )
65+ return _query .get (_Query )
66+
67+ @classmethod
68+ def get_params (cls ) -> Dict [str , Any ]:
69+ reader = cls .reader ()
70+ params = reader .get ('params' )
71+ if not params :
72+ raise ValueError ("No nodes found in config file" )
73+ return params
74+
75+ @classmethod
76+ def get_addr_params (cls , method : str ) -> Optional [str ]:
77+ """params for address endpoint
78+ Standard methods:
79+ - basic # return address details without txs details
80+ - txs # return address details with txs all details
81+ - txsLight # return address details with txid and vin and vout
82+ - txLight # return address details with txid only
83+ """
84+ params = cls .get_params ()
85+ addr = params .get ('addr' )
86+ if not addr :
87+ raise ValueError ("No address params found in config file" )
88+ return addr .get (method )
89+
90+ # Example Usage:
91+ # nodereader = NodeReader()
92+ # nodes = nodereader.get_nodes()
93+ # print(nodes)
94+ # addr_query = nodereader.get_query('address')
95+ # tx_query = nodereader.get_query('tx')
96+ # block_query = nodereader.get_query('block')
97+ # block_index_query = nodereader.get_query('blockIndex')
98+ # addr_params = nodereader.get_addr_params('basic')
99+ # tx_params = nodereader.get_addr_params('txs')
100+ # tx_light_params = nodereader.get_addr_params('txsLight')
0 commit comments