77
88import modal
99from fastapi import APIRouter , HTTPException
10- from fastapi .responses import JSONResponse
1110
1211from src .modal .budget_window_state import (
1312 build_batch_status_response ,
2726 PolicyEngineBundle ,
2827 SimulationRequest ,
2928)
29+ from src .modal .gateway .responses import (
30+ batch_status_response ,
31+ failed_job_response ,
32+ running_job_response ,
33+ )
3034
3135logger = logging .getLogger (__name__ )
3236
@@ -82,16 +86,6 @@ def _serialize_job_metadata(
8286 }
8387
8488
85- def _batch_status_payload (response : BudgetWindowBatchStatusResponse ) -> dict :
86- payload = response .model_dump (mode = "json" )
87- if response .policyengine_bundle is not None :
88- payload ["policyengine_bundle" ] = response .policyengine_bundle .model_dump (
89- mode = "json" ,
90- exclude_none = True ,
91- )
92- return payload
93-
94-
9589def _build_budget_window_parent_payload (
9690 request : BudgetWindowBatchRequest ,
9791 * ,
@@ -114,15 +108,6 @@ def _build_budget_window_parent_payload(
114108 return payload
115109
116110
117- def _json_batch_status_response (response : BudgetWindowBatchStatusResponse ):
118- payload = _batch_status_payload (response )
119- if response .status in {"submitted" , "running" }:
120- return JSONResponse (status_code = 202 , content = payload )
121- if response .status == "failed" :
122- return JSONResponse (status_code = 500 , content = payload )
123- return response
124-
125-
126111def get_app_name (country : str , version : Optional [str ]) -> tuple [str , str ]:
127112 """
128113 Resolve country + version to Modal app name.
@@ -286,25 +271,9 @@ async def get_job_status(job_id: str):
286271 status = "complete" , result = result , ** (job_metadata or {})
287272 )
288273 except TimeoutError :
289- return JSONResponse (
290- status_code = 202 ,
291- content = {
292- "status" : "running" ,
293- "result" : None ,
294- "error" : None ,
295- ** (job_metadata or {}),
296- },
297- )
274+ return running_job_response (job_metadata )
298275 except Exception as e :
299- return JSONResponse (
300- status_code = 500 ,
301- content = {
302- "status" : "failed" ,
303- "result" : None ,
304- "error" : str (e ),
305- ** (job_metadata or {}),
306- },
307- )
276+ return failed_job_response (error = str (e ), job_metadata = job_metadata )
308277
309278
310279@router .get (
@@ -318,7 +287,7 @@ async def get_budget_window_job_status(batch_job_id: str):
318287 """
319288 state = get_batch_job_state (batch_job_id )
320289 if state is not None :
321- return _json_batch_status_response (build_batch_status_response (state ))
290+ return batch_status_response (build_batch_status_response (state ))
322291
323292 seed_state = get_batch_job_seed (batch_job_id )
324293 if seed_state is None :
@@ -329,19 +298,19 @@ async def get_budget_window_job_status(batch_job_id: str):
329298 try :
330299 call = modal .FunctionCall .from_id (batch_job_id )
331300 except Exception :
332- return _json_batch_status_response (build_batch_status_response (seed_state ))
301+ return batch_status_response (build_batch_status_response (seed_state ))
333302
334303 try :
335304 result = call .get (timeout = 0 )
336305 except TimeoutError :
337- return _json_batch_status_response (build_batch_status_response (seed_state ))
306+ return batch_status_response (build_batch_status_response (seed_state ))
338307 except Exception as e :
339308 seed_state .status = "failed"
340309 seed_state .error = str (e )
341- return _json_batch_status_response (build_batch_status_response (seed_state ))
310+ return batch_status_response (build_batch_status_response (seed_state ))
342311
343312 response = BudgetWindowBatchStatusResponse .model_validate (result )
344- return _json_batch_status_response (response )
313+ return batch_status_response (response )
345314
346315
347316@router .get ("/versions" )
0 commit comments