Skip to content

Commit

Permalink
adding pagination and errors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Quixote committed Jan 1, 2023
1 parent ac7ec9d commit f5833d1
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 5 deletions.
Binary file modified koios_python/__pycache__/account.cpython-310.pyc
Binary file not shown.
Binary file modified koios_python/__pycache__/environment.cpython-310.pyc
Binary file not shown.
Binary file modified koios_python/__pycache__/network.cpython-310.pyc
Binary file not shown.
Binary file modified koios_python/__pycache__/urls.cpython-310.pyc
Binary file not shown.
40 changes: 39 additions & 1 deletion koios_python/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,44 @@ def get_account_addresses(self, *args):


def get_account_assets(self, *args):
"""
Get the native asset balance of given accounts.
:param str args: staking address/es in bech32 format (stake1...)
:return: list with all account assets.
:rtype: list.
"""
timeout = BASE_TIMEOUT
retriyng_time = RETRYING_TIME

while True:
try:
get_format = {"_stake_addresses": [args]}
assets = requests.post(self.ACCOUNT_ASSETS_URL, json= get_format, timeout=timeout)
assets = json.loads(assets.content)
break

except requests.exceptions.ReadTimeout as timeout_error:
print(f"Exception: {timeout_error}")
if timeout < LIMIT_TIMEOUT:
timeout= timeout + 10
else:
print(f"Reach Limit Timeout= {LIMIT_TIMEOUT} seconds")
break
print(f"Retriyng with longer timeout: Total Timeout= {timeout}s")

except json.decoder.JSONDecodeError as decode_error:
print(f"Exception Decode: Payload too heavy. {decode_error}")
sleep(SLEEP_TIME)
retriyng_time += 1
print(f"Retriyng one more time...({retriyng_time} times)")
if retriyng_time >= LIMIT_RETRYING_TIMES:
print("Reached limit of attempts")
break

return assets

## Alternative to Paginate all list automately
def get_account_assets_2(self, *args):
"""
Get the native asset balance of given accounts.
:param str args: staking address/es in bech32 format (stake1...)
Expand Down Expand Up @@ -217,8 +255,8 @@ def get_account_assets(self, *args):
except json.decoder.JSONDecodeError as decode_error:
print(f"Exception Decode: Payload too heavy. {decode_error}")
sleep(SLEEP_TIME)
print(f"Retriyng one more time...({retriyng_time} time/s)")
retriyng_time += 1
print(f"Retriyng one more time...({retriyng_time} times)")
if retriyng_time >= LIMIT_RETRYING_TIMES:
print("Reached limit of attempts")
break
Expand Down
2 changes: 1 addition & 1 deletion koios_python/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
SLEEP_TIME= 10
OFFSET= 0
RETRYING_TIME= 1
LIMIT_RETRYING_TIMES= 7
LIMIT_RETRYING_TIMES= 10

# Exceptions Functions
def handle_timeout_exceptions(timeout, timeout_error):
Expand Down
2 changes: 1 addition & 1 deletion koios_python/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_totals(self, epoch_no=None):
while True:
try:
if epoch_no is None:
totals = requests.get(self.TOTALS_URL, timeout=10)
totals = requests.get(self.TOTALS_URL, timeout=timeout)
totals = json.loads(totals.content)
else:
totals = requests.get(f"{self.TOTALS_URL}?_epoch_no={epoch_no}", timeout=timeout)
Expand Down
5 changes: 3 additions & 2 deletions koios_python/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class URLs:
from .network import get_tip, get_genesis, get_totals
from .block import get_blocks, get_block_info, get_block_txs
from .address import get_address_info, get_address_txs, get_address_assets, get_credential_txs
from .account import get_account_info, get_account_info_cached, get_account_list, get_account_rewards, get_account_updates, get_account_addresses, get_account_assets, get_account_history
from .account import get_account_info, get_account_info_cached, get_account_list, get_account_rewards, get_account_updates, get_account_addresses, get_account_assets, get_account_history, get_account_assets_2
from .asset import get_asset_list, get_asset_address_list, get_asset_info, get_asset_history, get_asset_policy_info, get_asset_summary, get_asset_txs
from .pool import get_pool_list, get_pool_info, get_pool_stake_snapshot, get_pool_delegators, get_pool_delegators_history, get_pool_blocks, get_pool_history, get_pool_updates, get_pool_relays, get_pool_metadata
from .scripts import get_native_script_list, get_plutus_script_list, get_script_redeemers, get_datum_info
Expand Down Expand Up @@ -62,7 +62,8 @@ def __init__(self, url='https://api.koios.rest/api/v0/', network='mainnet'):
self.ACCOUNT_REWARDS_URL = self.url + "account_rewards"
self.ACCOUNT_UPDATES_URL = self.url + "account_updates"
self.ACCOUNT_ADDRESSES_URL = self.url + "account_addresses"
self.ACCOUNT_ASSETS_URL = self.url + "account_assets?offset="
#self.ACCOUNT_ASSETS_URL = self.url + "account_assets?offset="
self.ACCOUNT_ASSETS_URL = self.url + "account_assets"
self.ACCOUNT_HISTORY_URL = self.url + "account_history"
# Asset URLs
self.ASSET_LIST_URL = self.url + "asset_list"
Expand Down

0 comments on commit f5833d1

Please sign in to comment.