Skip to content

Commit

Permalink
Add result key check to RPC time requests since some chains do not re…
Browse files Browse the repository at this point in the history
…turn the data with result key as the top level
  • Loading branch information
pharr117 committed Oct 21, 2023
1 parent b595fed commit 27dc43c
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,14 @@ def get_latest_block_height_rpc(rpc_url):
response = requests.get(f"{rpc_url}/status", timeout=1)
response.raise_for_status()
data = response.json()

if "result" in data.keys():
data = data["result"]

return int(
data.get("result", {}).get("sync_info", {}).get("latest_block_height", 0)
data.get("sync_info", {}).get("latest_block_height", 0)
)

# RPC endpoints can return a 200 but not JSON (usually an HTML error page due to throttling or some other error)
# Catch everything instead of just requests.RequestException
except Exception:
Expand All @@ -194,10 +199,15 @@ def get_block_time_rpc(rpc_url, height):
response = requests.get(f"{rpc_url}/block?height={height}", timeout=1)
response.raise_for_status()
data = response.json()
return data.get("result", {}).get("block", {}).get("header", {}).get("time", "")

if "result" in data.keys():
data = data["result"]

return data.get("block", {}).get("header", {}).get("time", "")
# RPC endpoints can return a 200 but not JSON (usually an HTML error page due to throttling or some other error)
# Catch everything instead of just requests.RequestException
except Exception:
except Exception as e:
print(e)
return None


Expand Down

0 comments on commit 27dc43c

Please sign in to comment.