Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
tonynguyen-ccl committed Aug 22, 2024
1 parent 2eb8978 commit aab4692
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/collectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,17 +372,17 @@ def alive(self):
"""Returns true if endpoint is alive, false if not."""
# Run cached query because we can also fetch client version from this
# later on. This will save us an RPC call per run.
return self.interface.cached_restApi_rpc_get() is not None
return self.interface.cached_rest_api_rpc_get() is not None

def block_height(self):
"""Runs a cached query to return block height"""
blockchain_info = self.interface.cached_restApi_rpc_get()
blockchain_info = self.interface.cached_rest_api_rpc_get()
return validate_dict_and_return_key_value(
blockchain_info, 'block_height', self._logger_metadata, to_number=True)

def client_version(self):
"""Runs a cached query to return client version."""
blockchain_info = self.interface.cached_restApi_rpc_get()
blockchain_info = self.interface.cached_rest_api_rpc_get()
version = validate_dict_and_return_key_value(
blockchain_info, 'git_hash', self._logger_metadata, stringify=True)
if version is None:
Expand Down
24 changes: 10 additions & 14 deletions src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ def strip_url(url) -> str:
return urllib.parse.urlparse(url).hostname


def return_and_validate_json_result(message: str, type: str,logger_metadata) -> dict:
def return_and_validate_json_result(message: str, json_type: str,logger_metadata) -> dict:
"""Loads json rpc response text and validates the response
as per JSON-RPC 2.0 Specification. In case the message is
as per JSON-RPC 2.0 Specification or JSON parsable if type is REST. In case the message is
not valid it returns None. This method is used by both HTTPS and
Websocket Interface."""
try:
if type=='RPC':
if json_type=='RPC':
parsed = parse_json(message)
if isinstance(parsed, Ok): # pylint: disable=no-else-return
return parsed.result
Expand All @@ -34,29 +34,25 @@ def return_and_validate_json_result(message: str, type: str,logger_metadata) ->
message=message,
error=error,
**logger_metadata)

except Exception as error:
logger.error('Unexpected error while parsing JSON.',
message=message,
error=error,
**logger_metadata)
return None

def return_and_validate_rpc_json_result(message: str, logger_metadata) -> dict:
return return_and_validate_json_result(message,type='RPC',logger_metadata=logger_metadata)
"""Validate that message is JSON parsable and per JSON-RPC specs"""
return return_and_validate_json_result(message,json_type='RPC',logger_metadata=logger_metadata)

def return_and_validate_restApi_json_result(message: str, logger_metadata) -> dict:
return return_and_validate_json_result(message,type='REST',logger_metadata=logger_metadata)
def return_and_validate_rest_api_json_result(message: str, logger_metadata) -> dict:
"""Validate that message is JSON parsable"""
return return_and_validate_json_result(message,json_type='REST',logger_metadata=logger_metadata)

def validate_dict_and_return_key_value(data, key, logger_metadata, stringify=False, to_number=False):
def validate_dict_and_return_key_value(data, key, logger_metadata, stringify=False, to_number=False): # pylint: disable=line-too-long
"""Validates that a dict is provided and returns the key value either in
original form or as a string"""
if isinstance(data, dict):
value = data.get(key)
if value is not None:
if stringify:
return str(value)
elif to_number:
if to_number:
try:
return float(value)
except ValueError:
Expand Down
10 changes: 5 additions & 5 deletions src/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import requests
from urllib3 import Timeout

from helpers import strip_url, return_and_validate_rpc_json_result, return_and_validate_restApi_json_result
from helpers import strip_url, return_and_validate_rpc_json_result, return_and_validate_rest_api_json_result # pylint: disable=line-too-long
from cache import Cache
from log import logger

Expand Down Expand Up @@ -94,26 +94,26 @@ def cached_json_rpc_post(self, payload: dict):
self.cache.store_key_value(cache_key, value)
return value

def json_restApi_get(self, params: dict = None):
def json_rest_api_get(self, params: dict = None):
"""Checks the validity of a successful json-rpc response. If any of the
validations fail, the method returns type None. """
response = self._return_and_validate_request(method='GET', params=params)
if response is not None:
result = return_and_validate_restApi_json_result(
result = return_and_validate_rest_api_json_result(
response, self._logger_metadata)
if result is not None:
return result
return None

def cached_restApi_rpc_get(self, params: dict = None):
def cached_rest_api_rpc_get(self, params: dict = None):
"""Calls json_rpc_get and stores the result in in-memory cache."""
cache_key = f"rest:{str(params)}"

if self.cache.is_cached(cache_key):
return_value = self.cache.retrieve_key_value(cache_key)
return return_value

value = self.json_restApi_get(params)
value = self.json_rest_api_get(params)
if value is not None:
self.cache.store_key_value(cache_key, value)
return value
Expand Down

0 comments on commit aab4692

Please sign in to comment.