22
22
from datetime import datetime , timezone
23
23
from threading import Lock , Thread
24
24
25
+ from xata .api_response import ApiResponse
26
+
25
27
from .client import XataClient
26
28
27
29
BP_DEFAULT_THREAD_POOL_SIZE = 4
@@ -396,7 +398,7 @@ def get(self, table: str, record_id: str, columns: list[str] = []) -> None:
396
398
def run (self , branch_name : str = None , retry : bool = True , flush_on_error : bool = False ) -> dict :
397
399
"""
398
400
Commit the transactions. Flushes the operations queue if no error happened.
399
- In case of too many connections, hitting rate limits, two extra attempts are taken
401
+ In case of too many connections, hitting rate limits, two extra attempts are taken
400
402
with an incremental back off.
401
403
402
404
:param branch_name: str Override the branch name from the client init
@@ -414,7 +416,9 @@ def run(self, branch_name: str = None, retry: bool = True, flush_on_error: bool
414
416
while attempt < 3 and not r .is_success ():
415
417
wait = attempt * TRX_BACKOFF
416
418
time .sleep (wait )
417
- self .logger .info (f"request { attempt } encountered a 429: too many requests error. will retry in { wait } ms." )
419
+ self .logger .info (
420
+ f"request { attempt } encountered a 429: too many requests error. will retry in { wait } ms."
421
+ )
418
422
r = self .client .records ().transaction (self .operations , branch_name = branch_name )
419
423
attempt += 1
420
424
@@ -423,17 +427,44 @@ def run(self, branch_name: str = None, retry: bool = True, flush_on_error: bool
423
427
self .operations ["operations" ] = []
424
428
425
429
# build response
426
- return {
427
- "status_code" : r .status_code ,
428
- "results" : r ["results" ] if "results" in r else [],
429
- "has_errors" : True if "errors" in r else False ,
430
- "errors" : r ["errors" ] if "errors" in r else [],
431
- "attempts" : attempt
432
- }
430
+ return self .Summary (r , attempt )
433
431
434
432
def size (self ) -> int :
435
433
"""
436
434
Get amount of operations in queue
437
435
:returns int
438
436
"""
439
437
return len (self .operations ["operations" ])
438
+
439
+ class Summary (dict ):
440
+ """
441
+ :link https://github.com/xataio/xata-py/issues/170
442
+ """
443
+
444
+ def __init__ (self , response : ApiResponse , attempts : int ):
445
+ super ()
446
+ super ().__setitem__ ("status_code" , response .status_code )
447
+ super ().__setitem__ ("results" , response .get ("results" , []))
448
+ super ().__setitem__ ("errors" , response .get ("errors" , []))
449
+ super ().__setitem__ ("has_errors" , len (response .get ("errors" , [])) > 0 )
450
+ super ().__setitem__ ("attempts" , attempts )
451
+
452
+ @property
453
+ def status_code (self ) -> int :
454
+ return self .__getitem__ ("status_code" )
455
+
456
+ @property
457
+ def attempts (self ) -> int :
458
+ return self .__getitem__ ("attempts" )
459
+
460
+ @property
461
+ def results (self ) -> list :
462
+ return self .__getitem__ ("results" )
463
+
464
+ @property
465
+ def errors (self ) -> list :
466
+ return self .__getitem__ ("errors" )
467
+
468
+ @property
469
+ def has_errors (self ) -> bool :
470
+ return self .__getitem__ ("has_errors" )
0 commit comments