33from urllib .parse import urljoin
44import backoff
55from ..request import post , get
6+ from ..async_request import post as async_post , get as async_get
67from ..exceptions import ApiCallExecutionError , ApiTimeoutError
78from ..util .transforms import client_result_from_wire
89from ..config import (client_timeout , do_client_api_compatibility_check_once ,
1213
1314
1415def post_call (endpoint : str , data : dict ):
16+ api_call_context = data .get ('api_call_context' , None )
17+ if api_call_context is None :
18+ api_call_context = current_context ()
19+ host = api_call_context .qcware_host
20+ # replace the ApiCallContext class with a jsonable dict
21+ data ['api_call_context' ] = api_call_context .dict ()
22+ url = urljoin (host , endpoint )
23+ return post (url , data )
24+
25+
26+ async def async_post_call (endpoint : str , data : dict ):
1527 """
1628 Centralizes the post for the API call. Assumes the data dict
1729 contains an entry with key 'api_key'; if this is missing or set to
@@ -25,31 +37,37 @@ def post_call(endpoint: str, data: dict):
2537 # replace the ApiCallContext class with a jsonable dict
2638 data ['api_call_context' ] = api_call_context .dict ()
2739 url = urljoin (host , endpoint )
28- return post (url , data )
40+ return await async_post (url , data )
2941
3042
3143def api_call (api_call_context : ApiCallContext , call_token : str ):
3244 api_call_context = current_context (
3345 ) if api_call_context is None else api_call_context
34- api_call_context = api_call_context .dict ()
3546 do_client_api_compatibility_check_once ()
36- return post (f'{ api_call_context ["qcware_host" ]} /api_calls' , locals ())
47+ return post (
48+ f'{ api_call_context .qcware_host } /api_calls' ,
49+ dict (api_call_context = api_call_context .dict (), call_token = call_token ))
50+
51+
52+ async def async_api_call (api_call_context : ApiCallContext , call_token : str ):
53+ api_call_context = current_context (
54+ ) if api_call_context is None else api_call_context
55+ do_client_api_compatibility_check_once ()
56+ return await async_post (
57+ f'{ api_call_context .qcware_host } /api_calls' ,
58+ dict (api_call_context = api_call_context .dict (), call_token = call_token ))
3759
3860
3961def status (call_token : str ):
4062 api_call_context = current_context ()
41- api_call_context = api_call_context .dict ()
4263 do_client_api_compatibility_check_once ()
43- return post (f'{ api_call_context ["qcware_host" ]} /api_calls/status' ,
44- locals ())
64+ return post (f'{ api_call_context .qcware_host } /api_calls/status' , locals ())
4565
4666
4767def cancel (call_token : str ):
4868 api_call_context = current_context ()
49- api_call_context = api_call_context .dict ()
5069 do_client_api_compatibility_check_once ()
51- return post (f'{ api_call_context ["qcware_host" ]} /api_calls/cancel' ,
52- locals ())
70+ return post (f'{ api_call_context .qcware_host } /api_calls/cancel' , locals ())
5371
5472
5573def _print_waiting_handler (details : Dict ):
@@ -70,6 +88,18 @@ def wait_for_call(call_token: str, api_call_context=None):
7088 return api_call (api_call_context , call_token )
7189
7290
91+ @backoff .on_predicate (backoff .constant ,
92+ interval = 1 ,
93+ predicate = lambda a : a .get ('state' ) == 'open' ,
94+ max_time = client_timeout ,
95+ on_backoff = _print_waiting_handler )
96+ async def async_wait_for_call (call_token : str , api_call_context = None ):
97+ api_call_context = current_context (
98+ ) if api_call_context is None else api_call_context
99+ # backoff.on_predicate is mildly problematic.
100+ return await async_api_call (api_call_context , call_token )
101+
102+
73103def handle_result (api_call ):
74104 if api_call ['state' ] == 'error' :
75105 if 'result_url' in api_call :
@@ -145,6 +175,6 @@ async def async_retrieve_result(call_token: str,
145175 """
146176 while True :
147177 try :
148- return handle_result (wait_for_call (call_token ))
178+ return handle_result (await async_wait_for_call (call_token ))
149179 except ApiTimeoutError as e :
150180 await asyncio .sleep (async_interval_between_tries ())
0 commit comments