2323from enum import IntEnum
2424from azure .identity import InteractiveBrowserCredential
2525from PowerPlatform .Dataverse .client import DataverseClient
26+ from PowerPlatform .Dataverse .core .errors import MetadataError
2627import requests
2728
2829
@@ -38,17 +39,32 @@ class Priority(IntEnum):
3839 HIGH = 3
3940
4041
41- def backoff (op , * , delays = (0 , 2 , 5 , 10 )):
42+ def backoff (op , * , delays = (0 , 2 , 5 , 10 , 20 , 20 )):
4243 last = None
44+ total_delay = 0
45+ attempts = 0
4346 for d in delays :
4447 if d :
4548 time .sleep (d )
49+ total_delay += d
50+ attempts += 1
4651 try :
47- return op ()
52+ result = op ()
53+ if attempts > 1 :
54+ retry_count = attempts - 1
55+ print (
56+ f" ↺ Backoff succeeded after { retry_count } retry(s); waited { total_delay } s total."
57+ )
58+ return result
4859 except Exception as ex : # noqa: BLE001
4960 last = ex
5061 continue
5162 if last :
63+ if attempts :
64+ retry_count = max (attempts - 1 , 0 )
65+ print (
66+ f" ⚠ Backoff exhausted after { retry_count } retry(s); waited { total_delay } s total."
67+ )
5268 raise last
5369
5470
@@ -183,7 +199,8 @@ def main():
183199 # Multiple read with filter
184200 log_call (f"client.get('{ table_name } ', filter='new_quantity gt 5')" )
185201 all_records = []
186- for page in client .get (table_name , filter = "new_quantity gt 5" ):
202+ records_iterator = backoff (lambda : client .get (table_name , filter = "new_quantity gt 5" ))
203+ for page in records_iterator :
187204 all_records .extend (page )
188205 print (f"✓ Found { len (all_records )} records with new_quantity > 5" )
189206 for rec in all_records :
@@ -232,7 +249,8 @@ def main():
232249 # Query with paging
233250 log_call (f"client.get('{ table_name } ', page_size=5)" )
234251 print ("Fetching records with page_size=5..." )
235- for page_num , page in enumerate (client .get (table_name , orderby = ["new_Quantity" ], page_size = 5 ), start = 1 ):
252+ paging_iterator = backoff (lambda : client .get (table_name , orderby = ["new_Quantity" ], page_size = 5 ))
253+ for page_num , page in enumerate (paging_iterator , start = 1 ):
236254 record_ids = [r .get ("new_walkthroughdemoid" )[:8 ] + "..." for r in page ]
237255 print (f" Page { page_num } : { len (page )} records - IDs: { record_ids } " )
238256
@@ -321,7 +339,7 @@ def main():
321339 print (f"✓ Deleted table: { table_name } " )
322340 except Exception as ex : # noqa: BLE001
323341 code = getattr (getattr (ex , "response" , None ), "status_code" , None )
324- if isinstance (ex , requests .exceptions .HTTPError ) and code == 404 :
342+ if ( isinstance (ex , ( requests .exceptions .HTTPError , MetadataError )) and code == 404 ) :
325343 print (f"✓ Table removed: { table_name } " )
326344 else :
327345 raise
0 commit comments