Skip to content

Commit

Permalink
Added dynamically generating list that is used to select the availabl…
Browse files Browse the repository at this point in the history
…e Networks.

Signed-off-by: KoleBarnes <kbarnes261@gmail.com>
  • Loading branch information
KoleBarnes committed Dec 20, 2021
1 parent 418de4f commit 01acb3c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 12 additions & 0 deletions fetch-validator-status/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import urllib.request
import sys
import re
from enum import Enum
from collections import namedtuple
from util import log
from singleton import Singleton

Network = namedtuple('Network', ['id', 'name', 'genesis_url', 'genesis_path'])

class NetworkEnum(Enum):
def _generate_next_value_(name, start, count, last_values):
return name

class Networks(object, metaclass=Singleton):
def __init__(self):
self._networks = self.__load_network_list()
Expand Down Expand Up @@ -45,6 +50,13 @@ def __download_genesis_file(genesis_url: str, destination_path: str):
log("Fetching genesis file ...")
urllib.request.urlretrieve(genesis_url, destination_path)

@staticmethod
def get_NetworkEnum() -> NetworkEnum:
"""Dynamically generates a NetworkEnum that can be used to select the available Networks.
"""
networks = Networks()
return NetworkEnum('Network', list(networks.ids))

def resolve(self, network_id: str = None, genesis_url: str = None, genesis_path: str = None):
network_name = None
genesis_path_base = f"{self.__get_script_dir()}/"
Expand Down
12 changes: 7 additions & 5 deletions fetch-validator-status/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
create_did
)
from pool import PoolCollection
from networks import Networks
from networks import Networks, NetworkEnum
from fetch_status import FetchStatus, NodeNotFound
from plugin_collection import PluginCollection

Expand All @@ -30,6 +30,8 @@
pool_collection = None
node_info = None

Network: NetworkEnum = Networks.get_NetworkEnum()

def set_plugin_parameters(status: bool = False, alerts: bool = False):
# Store args and monitor_plugins for lazy loading.
global default_args, pool_collection, node_info
Expand Down Expand Up @@ -72,25 +74,25 @@ async def networks():
return data

@app.get("/networks/{network}")
async def network(network: str = Path(..., example="sbn", description="The network code."),
async def network(network: Network = Path(Network.sbn, example="sbn", description="The network code."),
status: bool = Query(False, description="Filter results to status only."),
alerts: bool = Query(False, description="Filter results to alerts only."),
seed: Optional[str] = Header(None, description="Your network monitor seed.")):
monitor_plugins = set_plugin_parameters(status, alerts)
ident = create_did(seed)
result = await node_info.fetch(network_id=network, monitor_plugins=monitor_plugins, ident=ident)
result = await node_info.fetch(network_id=network.value, monitor_plugins=monitor_plugins, ident=ident)
return result

@app.get("/networks/{network}/{node}")
async def node(network: str = Path(..., example="sbn", description="The network code."),
async def node(network: Network = Path(Network.sbn, example="sbn", description="The network code."),
node: str = Path(..., example="FoundationBuilder", description="The node name."),
status: bool = Query(False, description="Filter results to status only."),
alerts: bool = Query(False, description="Filter results to alerts only."),
seed: Optional[str] = Header(None, description="Your network monitor seed.")):
monitor_plugins = set_plugin_parameters(status, alerts)
ident = create_did(seed)
try:
result = await node_info.fetch(network_id=network, monitor_plugins=monitor_plugins, nodes=node, ident=ident)
result = await node_info.fetch(network_id=network.value, monitor_plugins=monitor_plugins, nodes=node, ident=ident)
except NodeNotFound as error:
print(error)
raise HTTPException(status_code=400, detail=str(error))
Expand Down

0 comments on commit 01acb3c

Please sign in to comment.