@@ -369,25 +369,68 @@ async def tml_import(
369369 tmls : list [TMLObject ],
370370 * ,
371371 policy : _types .TMLImportPolicy = "ALL_OR_NONE" ,
372+ use_async_endpoint : bool = False ,
373+ wait_for_completion : bool = False ,
374+ log_errors : bool = False ,
372375 http : RESTAPIClient ,
373376 ** tml_import_options ,
374377) -> _types .APIResult :
375378 """Import a metadata object, alerting about warnings and errors."""
376- r = await http .metadata_tml_import (tmls = [t .dumps () for t in tmls ], policy = policy , ** tml_import_options )
377- r .raise_for_status ()
379+ if use_async_endpoint :
380+ _LOG .debug (f"Async import initiated on { len (tmls ):,} objects (behave synchronously: { wait_for_completion } )." )
381+ r = await http .metadata_tml_async_import (tmls = [t .dumps () for t in tmls ], policy = policy , ** tml_import_options )
382+ r .raise_for_status ()
383+ d = r .json ()
384+
385+ _LOG .debug (f"RAW DATA\n { json .dumps (d , indent = 2 , default = str )} \n " )
386+
387+ # IF WE'RE NOT WAITING FOR THE JOB TO COMPLETE, RETURN THE ASYNC JOB INFO DIRECTLY.
388+ if not wait_for_completion :
389+ return d
378390
379- for tml_import_info , tml in zip (r .json (), tmls ):
380- tml_type = tml .tml_type_name .upper ()
391+ async_job_id = d ["task_id" ]
381392
382- if tml_import_info ["response" ]["status" ]["status_code" ] == "ERROR" :
383- errors = tml_import_info ["response" ]["status" ]["error_message" ].replace ("<br/>" , "\n " )
384- _LOG .error (f"{ tml_type } '{ tml .name } ' failed to import, ThoughtSpot errors:\n [fg-error]{ errors } " )
393+ # AFTER FIVE 5-second ITERATIONS (25s), WE'LL ELEVATE THE LOGGING LEVEL.
394+ n_iterations = 0
385395
386- if tml_import_info ["response" ]["status" ]["status_code" ] == "WARNING" :
387- errors = tml_import_info ["response" ]["status" ]["error_message" ].replace ("<br/>" , "\n " )
388- _LOG .warning (f"{ tml_type } '{ tml .name } ' partially imported, ThoughtSpot errors:\n [fg-warn]{ errors } " )
396+ # OTHERWISE, PROCESS THE JOB AS IF IT WERE A SYNCHRONOUS PAYLOAD.
397+ while d .get ("task_status" ) != "COMPLETED" :
398+ log_level = logging .DEBUG if n_iterations < 5 else logging .INFO
399+ n_iterations += 1
400+ _LOG .log (log_level , f"Checking status of asynchronous import { async_job_id } " )
401+ _ = await asyncio .sleep (5 ) # type: ignore[func-returns-value]
402+ r = await http .metadata_tml_async_status (task_ids = [async_job_id ])
403+ r .raise_for_status ()
404+
405+ # RAW DATA
406+ _ = r .json ()
407+ _LOG .debug (f"RAW DATA\n { json .dumps (_ , indent = 2 , default = str )} \n " )
389408
390- if tml_import_info ["response" ]["status" ]["status_code" ] == "OK" :
391- _LOG .debug (f"{ tml_type } '{ tml .name } ' successfully imported" )
409+ # FIRST STATUS (we only 1 in job), BUT ONLY REASSIGN while LOOP VAR IF THE KEY EXISTS.
410+ d = next (iter (_ ["status_list" ]), d )
411+ _LOG .log (log_level , f"TASK ID: { async_job_id } \n { json .dumps (d , indent = 2 , default = str )} \n " )
392412
393- return r .json ()
413+ # POST-PROCESSING TO MIMIC THE SYNCHRONOUS RESPONSE.
414+ d = d ["import_response" ]["object" ]
415+
416+ else :
417+ r = await http .metadata_tml_import (tmls = [t .dumps () for t in tmls ], policy = policy , ** tml_import_options )
418+ r .raise_for_status ()
419+ d = r .json ()
420+
421+ if log_errors :
422+ for tml_import_info , tml in zip (d , tmls ):
423+ tml_type = tml .tml_type_name .upper ()
424+
425+ if tml_import_info ["response" ]["status" ]["status_code" ] == "ERROR" :
426+ errors = tml_import_info ["response" ]["status" ]["error_message" ].replace ("<br/>" , "\n " )
427+ _LOG .error (f"{ tml_type } '{ tml .name } ' failed to import, ThoughtSpot errors:\n [fg-error]{ errors } " )
428+
429+ if tml_import_info ["response" ]["status" ]["status_code" ] == "WARNING" :
430+ errors = tml_import_info ["response" ]["status" ]["error_message" ].replace ("<br/>" , "\n " )
431+ _LOG .warning (f"{ tml_type } '{ tml .name } ' partially imported, ThoughtSpot errors:\n [fg-warn]{ errors } " )
432+
433+ if tml_import_info ["response" ]["status" ]["status_code" ] == "OK" :
434+ _LOG .debug (f"{ tml_type } '{ tml .name } ' successfully imported" )
435+
436+ return d
0 commit comments