Last active
February 22, 2021 23:14
-
-
Save Nikolaj-K/3b8c8b3674d6b850a91faa505ce64e99 to your computer and use it in GitHub Desktop.
Using python to run through the Compendia API examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Using the python requests library to run through query examples calls following | |
https://docs.compendia.org/api | |
or | |
https://api.ark.io/api | |
Manually, one can do the same using curl via, e.g., | |
'curl https://api.nos.dev/api/v2/blockchain' | |
which you might call from python (Not recommended, as this variant not return python dicts) | |
import subprocess; subprocess.run(["curl", "https://api.nos.dev/api/v2/blockchain"]) | |
See also the plot explorers | |
https://explorer.nos.dev/ | |
and | |
https://explorer.ark.io/ | |
""" | |
import pprint | |
import requests | |
import time | |
URL_ARK = "https://api.ark.io" + "/api" | |
URL_COMPENDIA = "https://api.nos.dev" + "/api/v2" | |
class Config: | |
URL = URL_ARK | |
def _api_get(query_key, sleep=0.4): | |
""" | |
:param query_key: e.g. "/blocks" | |
""" | |
query_url = Config.URL + query_key | |
print(f"\n* query_url = '{query_url}'") | |
data = requests.get(query_url).json() | |
data_key_exists = 'data' in data | |
length = len(data['data']) if data_key_exists else 0 | |
print(f"* Data length = {length}." if data_key_exists else "'data' key not in data return value.") | |
time.sleep(sleep) | |
return data, data_key_exists | |
def _blockchain(): | |
""" | |
See | |
https://docs.compendia.org/api/blockchain.html#endpoint | |
""" | |
print("\n>>> Requesting current info...") | |
data, _exists = _api_get("/blockchain") | |
pprint.pprint(data) | |
def _blocks(): | |
""" | |
See | |
https://docs.compendia.org/api/blocks.html#list-all-blocks | |
""" | |
TEST_HEIGHT = 1490000 | |
print("\n>>> Requesting block data...") | |
data, _exists = _api_get("/blockchain") | |
pprint.pprint(data) | |
for ref in ("first", "last",): | |
print(f"\n>>> Requesting {ref} block...") | |
data, _exists = _api_get(f"/blocks/{ref}") | |
pprint.pprint(data) | |
print(f"\n>>> Requesting block height={TEST_HEIGHT}...") | |
data, _exists = _api_get(f"/blocks?TEST_HEIGHT={TEST_HEIGHT}") # syntax "?TEST_HEIGHT=" | |
pprint.pprint(data) | |
print(f"\n>>> Requesting block height={TEST_HEIGHT} (again)...") | |
data, _exists = _api_get(f"/blocks/{TEST_HEIGHT}") # syntax "/" | |
pprint.pprint(data) | |
# I think Id can also be used here | |
print(f"\n>>> Requesting block height={TEST_HEIGHT}'s transactions...") | |
data, _exists = _api_get(f"/blocks/{TEST_HEIGHT}/transactions") | |
pprint.pprint(data) | |
# data['data'] can easily be empty list | |
# One can also request blocks and apply a filter on the node side | |
# ??how? | |
if True: | |
print("\n>>> Requesting 100 blocks...") | |
data, exists = _api_get("/blocks") | |
if exists: | |
print("meta value:") | |
pprint.pprint(data["meta"]) | |
def _delegates(): | |
print("\n>>> Requesting delegates...") | |
data, exists = _api_get("/delegates") | |
if exists: | |
print("\n>> meta value:") | |
pprint.pprint(data["meta"]) | |
print("\n>> First delegate:") | |
pprint.pprint(data["data"][0]) | |
def _locks(): # Not operational on Compendia Mainnet | |
pass | |
def _node(): | |
""" | |
See | |
https://docs.compendia.org/api/node.html#retrieve-the-configuration | |
""" | |
QUERY_KEYS = ( | |
"/node/configuration", # A nodes configurations | |
"/node/configuration/crypto", # Cryptographic operations | |
"/node/fees", | |
"/node/status", | |
"/node/syncing", | |
"/node/syncing", | |
) | |
for query_key in QUERY_KEYS: | |
data, _exists = _api_get(query_key) | |
pprint.pprint(data["data"]) | |
def _peers(): | |
""" | |
See | |
https://docs.compendia.org/api/peers.html#list-all-peers | |
""" | |
data, _exists = _api_get("/peers") # Peers known by the node | |
pprint.pprint(data["data"]) | |
print("ip's:\n" + ", ".join(x["ip"] for x in data["data"])) | |
ID = "104.248.56.157" | |
data, exists = _api_get(f"/peers/{ID}") # Peers known by the node | |
if exists: | |
pprint.pprint(data["data"]) | |
def _rounds(): | |
""" | |
See | |
https://docs.compendia.org/api/rounds.html#list-rounds-data | |
""" | |
data, _exists = _api_get("/rounds/5000/delegates") | |
pprint.pprint(data["data"]) | |
def _transactions(): | |
""" | |
See | |
https://docs.compendia.org/api/transactions.html | |
See also the plot explorers | |
https://explorer.ark.io/wallets/AFrPtEmzu6wdVpa2CnRDEKGQQMWgq8nE9V | |
and | |
https://explorer.nos.dev/ | |
""" | |
data, _exists = _api_get("/transactions") | |
for idx in [0, 1, 99]: | |
print(f'\n\n* data["data"][{idx}]:\n') | |
pprint.pprint(data["data"][idx]) | |
#See also /transactions/unconfirmed | |
#There is also /transactions/search, but not explained in the docs | |
data, _exists = _api_get("/transactions/types") | |
pprint.pprint(data["data"]) | |
data, _exists = _api_get("/transactions/fees") | |
pprint.pprint(data["data"]) | |
data, _exists = _api_get("/transactions/schemas") | |
pprint.pprint(f'data["data"].keys(): {data["data"].keys()}') | |
def _votes(): | |
""" | |
See | |
https://docs.compendia.org/api/votes.html#list-all-votes | |
""" | |
data, _exists = _api_get("/votes") | |
pprint.pprint(data["data"]) | |
# TODO: There's more to votes | |
def _wallets(): | |
print("\n>>> Requesting wallets...") | |
data, l = _api_get("/wallets") | |
if l: | |
print("\n>> meta value:") | |
pprint.pprint(data["meta"]) | |
print("\n>> First wallet:") | |
pprint.pprint(data["data"][0]) | |
print("\n>> Sixth wallet:") | |
pprint.pprint(data["data"][5]) | |
# Attributes of wallet can include data such as delegate status | |
# TODO: There's more to wallet | |
if __name__=='__main__': | |
print("Run examples...\n") | |
if True: | |
_blockchain() | |
_blocks() | |
_delegates() | |
_node() | |
_peers() | |
_rounds() | |
_votes() | |
_wallets() | |
if True: | |
_transactions() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment