From f5833d112ce34e8ced53808b2d560ff663a77b8d Mon Sep 17 00:00:00 2001
From: Quixote <quixotepool@proton.me>
Date: Mon, 2 Jan 2023 00:03:55 +0100
Subject: [PATCH] adding pagination and errors handling

---
 .../__pycache__/account.cpython-310.pyc       | Bin 5693 -> 6345 bytes
 .../__pycache__/environment.cpython-310.pyc   | Bin 775 -> 770 bytes
 .../__pycache__/network.cpython-310.pyc       | Bin 2156 -> 2156 bytes
 koios_python/__pycache__/urls.cpython-310.pyc | Bin 4631 -> 4654 bytes
 koios_python/account.py                       |  40 +++++++++++++++++-
 koios_python/environment.py                   |   2 +-
 koios_python/network.py                       |   2 +-
 koios_python/urls.py                          |   5 ++-
 8 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/koios_python/__pycache__/account.cpython-310.pyc b/koios_python/__pycache__/account.cpython-310.pyc
index 4c93d12992572d33bcb930132cef5e773a4718d8..3afaf4790de019a87aad131201e5118ddc5e7319 100644
GIT binary patch
delta 812
zcmYLH&rcIU6rMM`+g-|TDN8F<YO)w4EI&;A9Tm|6A|BL)YKX*Gn{I^`XmL7*Kz4zs
z@nSTRiSa;4@Zv=;O7!L*U`)JuA)Y*VR1bQvdhktA*<`<Y@69*goA+hj^t{~{DO*;U
z;CIV<nS1K4M3V4erF#exE3+x9*=xB&#$z5$6L;r}*UuwM1~Mi64E~jNAcT{EatM?<
zve4{NA10sxfy!x7@kvqjRiApweI?MiwjT1eqW%N|ZH)L@ny3U>MzL;w3`h_Hve3)J
zp6Z8I$r`;4Kzz#UrwBWY3R*LEDK+r0_yBP&w1`ULD<qaMD)oCtfa@Hfo>nxA*8D@%
z(^0Pn_3C~7pB|6!hMKP8nMX*V;I8E>tHg_}DO+_OozmD{tk-yB<T62ReNmUU-Oftd
zR&@^aIdVgDqgC-f`WdR?RbxkpDQHYpd~9qzTh$$2n8{b%YGO2>%}wS#XSy&`;7<9v
zljS@=Gs`P(BUAAJleV(BgieG_J2g0X;nMkwnN&J``l475cZ$Gv54Z-~B{^LZEJ?Ij
zp0mPyX(A(Av2IxlWh<3DuP{d@;=6rls2?L$SAkLrVX(mj3Tn*$5&;AAC=G*ZCVv>q
zHkB0vqBX=SHGdn+^cVfchBihUG-&bFns+KU(K~h9J5i2a)!0$&Bd$fd66_d8H7nS0
z8TCmxA$QwsC(Pu1SWKp~dVAeTnVpi^W4repOJOu_@-oj(XZ}ti$6^B#90`LG+Wy0C
zj5cEvd7jDUa^;&PTx$ko_J0z4nhq*BkGRs5gkkYcPi{PFdImHKVj$jUjo?Wan0(J}
cT-=LK)(=V5VF@GRXS@TF!r9TP8Xe}|KY81@pa1{>

delta 299
zcmX?UxL1cSpO=@50SKz@ZA`XNnaC%@*fvqyg;9KBfg>wZFoUMV#(it}CaVd5l_=p!
z;i+Mn&5*)7m${jdk)ehqh0mO!mSwWBNE~C;<V6C{fMlnjnogBeP-;n0W@TQwLVjMV
zLT-LhszOO-ZmOQ1o<<c1n5kc^S;P#ob@DAismV7*6hxwg9G#v0L;XVH9fN~iLxST&
zgM23c6VaILD5N>rUR2H?inj=AYCOoWD4vr1lEj?&#Ny)AlHww5pkcSzpd1AtCu;Is
zA=An0L=Bh)7&#_C6qRAL-TY7VI3v?5hRMgpZFud0YBU*(Kz`R0nJgfY!EFm<7J-C|
X6el-|3QwLb!N({yd6k4T6OSGMKbBT$

diff --git a/koios_python/__pycache__/environment.cpython-310.pyc b/koios_python/__pycache__/environment.cpython-310.pyc
index 507b1700597478be0d7a788d5dfd92dcf23673dd..e4d427e26170e63f65fc304cdf20179dd30be246 100644
GIT binary patch
delta 101
zcmZo?YhvTg=jG*M00P_38<WE)@>&TpMX{x@rm(dzM6su^2Qz4LOpJ)$xI>4Lk!$i(
p#x5Zqpjtmoj$7>U@hSPq@$p6MlN*`5MM1(vAcK$y?#U0C)Bw9h7UKW_

delta 104
zcmZo-YiHxl=jG*M0D`J}8<WE(@>&V9MzN)^rLea!M6suE1T$!IPK=0Vdda?VgFYi8
r_v8nRT|&G-<$jtRx7g$3Q}UDJ<BK>Z*D!gDf`p5>Q3;;OcbL=wADI{T

diff --git a/koios_python/__pycache__/network.cpython-310.pyc b/koios_python/__pycache__/network.cpython-310.pyc
index 5563a031b7dc7e88e0108714b3c9b76a3c7f1464..16b1ddcd4201be22ac8d14188fdab330081912e8 100644
GIT binary patch
delta 91
zcmaDO@J4_ypO=@50SMUMY)oe4-pHrQ%2LCW!aUiWRgs&ehH*B-TxKvonN^#Sb#gze
oC?nhErL5VE5^O+CRowRRsRjAT8S#1fwnZF3ks{8`8tg8N0NHyLHvj+t

delta 75
zcmaDO@J4_ypO=@50SHW=Z%qEfwUJMil_iBag=Mlit0FgR4dZNvxy&_8Kz=f-HY3~Q
cepXRN=FLl4vl&@9ftn`svrBB&VRv8z0M-Q&RR910

diff --git a/koios_python/__pycache__/urls.cpython-310.pyc b/koios_python/__pycache__/urls.cpython-310.pyc
index 5bf40afb184e04c601a71a54680bc75a5f25e329..17ea15a4b967f26bead2ae9f76866da42ffc2402 100644
GIT binary patch
delta 421
zcmZXPOG^S#6vw@4_#B;qh|qlN!csDI@|kTCg7z)WMr1}pnB&NuDY7D3)TW)>g_~9_
zlr{y;$LJdfeukiB5j5jYI*Z@o|A51pq+ipV%5h$sIk()qv>0<YBsqS3*u@A$h)pK4
zONn9(Vi<=wCLn=HNMZ_7n1(cFAhYba71kn|V=O>nsXfXb7NLkGC}A1OD1x~3y~;jT
zpn_GXVhw6ohdMT(flX+dIWKc^;GVz%n~_iAVH5joW4?aY(>q;BQqKqG*0i3kb&5vI
zv2c<(+j57AXjI*&gonUt$(VY62m#~S_vGM?%=c*9#EXrN*hz3o?CdLe=qp>kXd>HU
zO};_e?2^Cm(@oQvuPu+UZ$9dyG0SicRaIdrAzz?bOS?3Q_J_Jgw@pQNEaWWYt<bO2
m?m+8GdS4xA>`n-UN>+AlVYG@X_9lc$j(rG8{$KPfg#G}uaB|oH

delta 488
zcmZwB%S#(U7y$5mW;LtH+N1=@B^w{B_=<TNHSuw1&K^97d>|5bBNg_`hMBQw5PE5$
zc#}Ew(4H$&sE2s)Z&~mvc=8Vrp)<J&4$N=(p3Fbzr(*?{H5O6lmEF2OuB;2NuP(nA
zAmV`+^Rr~5O*zcbBu>&4PSG?@(+tj#gU+DG^k-=f<u=SV2l1$1q!N~>jAg1|g{oK`
z<j4F~s$q@RaE;b+oi=cT>R6`+HdH<7S>E_Jz)krVp23vAhOYYu(FY@oG12Ct(-tAD
zJn3)t%+Eq}dA(3z)&8Ej51HOX%XNd(jyP&@-WGf(I6hYIR{wtV1t9F-#eYYwPimAk
z6*m!fCJ#US0gy&=CtlI{s$yYF?j{@1l$XgbiEwzZie*$mCQ^1hG`d0Hvz(4sQ~9r9
zjJxclD>z%w3l=p?nq@_p5UkbVEmxcb9nSJPzoIE<ikgz9tm$da-g2%(P>iU{ClqER
jJ(PwtWMCc~D9Xo_z4*>hXl_#Ao%-!<J<Sb;0UwS3tH*dE

diff --git a/koios_python/account.py b/koios_python/account.py
index fce1bac..6b79da3 100644
--- a/koios_python/account.py
+++ b/koios_python/account.py
@@ -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...)
@@ -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
diff --git a/koios_python/environment.py b/koios_python/environment.py
index d1db3c8..e43b79b 100644
--- a/koios_python/environment.py
+++ b/koios_python/environment.py
@@ -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):
diff --git a/koios_python/network.py b/koios_python/network.py
index 19359fe..e5cbbd5 100644
--- a/koios_python/network.py
+++ b/koios_python/network.py
@@ -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)
diff --git a/koios_python/urls.py b/koios_python/urls.py
index caee6eb..a3b28af 100644
--- a/koios_python/urls.py
+++ b/koios_python/urls.py
@@ -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
@@ -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"