4343
4444app = FastAPI ()
4545
46+
4647def json_serializer (obj ):
4748 """JSON serializer for objects not serializable by default json code"""
4849 if isinstance (obj , (datetime .datetime , datetime .date , datetime .time )):
@@ -253,7 +254,9 @@ async def cli_auth(auth_provider: str, code: str, state: str, db_context=Depends
253254 raise HTTPException (status_code = 400 , detail = f"Invalid state parameter: { e } " ) from None
254255
255256 # Determine API URL (handle potential None value)
256- api_base_url = os .environ .get ("DISCORD_CLUSTER_MANAGER_API_BASE_URL" ) or os .getenv ("POPCORN_API_URL" )
257+ api_base_url = os .environ .get ("DISCORD_CLUSTER_MANAGER_API_BASE_URL" ) or os .getenv (
258+ "POPCORN_API_URL"
259+ )
257260 if not api_base_url :
258261 raise HTTPException (
259262 status_code = 500 ,
@@ -266,6 +269,8 @@ async def cli_auth(auth_provider: str, code: str, state: str, db_context=Depends
266269 user_id = None
267270 user_name = None
268271
272+ print (redirect_uri )
273+
269274 try :
270275 if auth_provider == "discord" :
271276 user_id , user_name = await _handle_discord_oauth (code , redirect_uri )
@@ -277,10 +282,15 @@ async def cli_auth(auth_provider: str, code: str, state: str, db_context=Depends
277282 raise e
278283 except Exception as e :
279284 # Catch unexpected errors during OAuth handling
280- raise HTTPException (status_code = 500 , detail = f"Error during { auth_provider } OAuth flow: { e } " ) from e
285+ raise HTTPException (
286+ status_code = 500 , detail = f"Error during { auth_provider } OAuth flow: { e } "
287+ ) from e
281288
282289 if not user_id or not user_name :
283- raise HTTPException (status_code = 500 ,detail = "Failed to retrieve user ID or username from provider." ,)
290+ raise HTTPException (
291+ status_code = 500 ,
292+ detail = "Failed to retrieve user ID or username from provider." ,
293+ )
284294
285295 try :
286296 with db_context as db :
@@ -290,7 +300,9 @@ async def cli_auth(auth_provider: str, code: str, state: str, db_context=Depends
290300 db .create_user_from_cli (user_id , user_name , cli_id , auth_provider )
291301
292302 except AttributeError as e :
293- raise HTTPException (status_code = 500 , detail = f"Database interface error during update: { e } " ) from e
303+ raise HTTPException (
304+ status_code = 500 , detail = f"Database interface error during update: { e } "
305+ ) from e
294306 except Exception as e :
295307 raise HTTPException (status_code = 400 , detail = f"Database update failed: { e } " ) from e
296308
@@ -302,6 +314,7 @@ async def cli_auth(auth_provider: str, code: str, state: str, db_context=Depends
302314 "is_reset" : is_reset ,
303315 }
304316
317+
305318async def _stream_submission_response (
306319 submission_request : SubmissionRequest ,
307320 submission_mode_enum : SubmissionMode ,
@@ -365,6 +378,7 @@ async def _stream_submission_response(
365378 except asyncio .CancelledError :
366379 pass
367380
381+
368382@app .post ("/{leaderboard_name}/{gpu_type}/{submission_mode}" )
369383async def run_submission ( # noqa: C901
370384 leaderboard_name : str ,
@@ -403,13 +417,13 @@ async def run_submission( # noqa: C901
403417 )
404418 return StreamingResponse (generator , media_type = "text/event-stream" )
405419
420+
406421async def enqueue_background_job (
407422 req : ProcessedSubmissionRequest ,
408423 mode : SubmissionMode ,
409424 backend : KernelBackend ,
410425 manager : BackgroundSubmissionManager ,
411426):
412-
413427 # pre-create the submission for api returns
414428 with backend .db as db :
415429 sub_id = db .create_submission (
@@ -423,7 +437,8 @@ async def enqueue_background_job(
423437 job_id = db .upsert_submission_job_status (sub_id , "initial" , None )
424438 # put submission request in queue
425439 await manager .enqueue (req , mode , sub_id )
426- return sub_id ,job_id
440+ return sub_id , job_id
441+
427442
428443@app .post ("/submission/{leaderboard_name}/{gpu_type}/{submission_mode}" )
429444async def run_submission_async (
@@ -450,34 +465,39 @@ async def run_submission_async(
450465 JSONResponse: A JSON response containing job_id and and submission_id for the client to poll for status.
451466 """
452467 try :
453-
454468 await simple_rate_limit ()
455- logger .info (f"Received submission request for { leaderboard_name } { gpu_type } { submission_mode } " )
456-
469+ logger .info (
470+ f"Received submission request for { leaderboard_name } { gpu_type } { submission_mode } "
471+ )
457472
458473 # throw error if submission request is invalid
459474 try :
460475 submission_request , submission_mode_enum = await to_submit_info (
461- user_info , submission_mode , file , leaderboard_name , gpu_type , db_context
476+ user_info , submission_mode , file , leaderboard_name , gpu_type , db_context
462477 )
463478
464479 req = prepare_submission (submission_request , backend_instance )
465480
466481 except Exception as e :
467- raise HTTPException (status_code = 400 , detail = f"failed to prepare submission request: { str (e )} " ) from e
482+ raise HTTPException (
483+ status_code = 400 , detail = f"failed to prepare submission request: { str (e )} "
484+ ) from e
468485
469486 # prepare submission request before the submission is started
470487 if not req .gpus or len (req .gpus ) != 1 :
471488 raise HTTPException (status_code = 400 , detail = "Invalid GPU type" )
472489
473490 # put submission request to background manager to run in background
474- sub_id ,job_status_id = await enqueue_background_job (
491+ sub_id , job_status_id = await enqueue_background_job (
475492 req , submission_mode_enum , backend_instance , background_submission_manager
476493 )
477494
478495 return JSONResponse (
479496 status_code = 202 ,
480- content = {"details" :{"id" : sub_id , "job_status_id" : job_status_id }, "status" : "accepted" },
497+ content = {
498+ "details" : {"id" : sub_id , "job_status_id" : job_status_id },
499+ "status" : "accepted" ,
500+ },
481501 )
482502 # Preserve FastAPI HTTPException as-is
483503 except HTTPException :
@@ -542,7 +562,7 @@ async def create_dev_leaderboard(
542562 if not definition .gpus :
543563 raise HTTPException (
544564 status_code = 400 ,
545- detail = "No gpus specified in task.yml. Add 'gpus:' field with list of GPU types."
565+ detail = "No gpus specified in task.yml. Add 'gpus:' field with list of GPU types." ,
546566 )
547567
548568 with db_context as db :
@@ -591,7 +611,9 @@ async def admin_stats(
591611 _ : Annotated [None , Depends (require_admin )],
592612 db_context = Depends (get_db ),
593613 last_day_only : bool = False ,
594- leaderboard_name : Optional [str ] = Query (None , description = "Filter stats to a specific leaderboard name" ),
614+ leaderboard_name : Optional [str ] = Query (
615+ None , description = "Filter stats to a specific leaderboard name"
616+ ),
595617) -> dict :
596618 with db_context as db :
597619 stats = db .generate_stats (last_day_only , leaderboard_name )
@@ -635,7 +657,7 @@ async def admin_update_problems(
635657 branch = branch ,
636658 force = force ,
637659 creator_id = 0 , # API-created
638- forum_id = - 1 , # No Discord forum
660+ forum_id = - 1 , # No Discord forum
639661 )
640662 except ValueError as e :
641663 raise HTTPException (status_code = 400 , detail = str (e )) from e
@@ -781,7 +803,9 @@ async def get_user_submission(
781803
782804 # Verify ownership
783805 if str (submission ["user_id" ]) != str (user_info ["user_id" ]):
784- raise HTTPException (status_code = 403 , detail = "Not authorized to view this submission" )
806+ raise HTTPException (
807+ status_code = 403 , detail = "Not authorized to view this submission"
808+ )
785809
786810 # RunItem is a TypedDict (already a dict), select fields to expose
787811 run_fields = ("start_time" , "end_time" , "mode" , "secret" , "runner" , "score" , "passed" )
@@ -826,7 +850,9 @@ async def delete_user_submission(
826850
827851 # Verify ownership
828852 if str (submission ["user_id" ]) != str (user_info ["user_id" ]):
829- raise HTTPException (status_code = 403 , detail = "Not authorized to delete this submission" )
853+ raise HTTPException (
854+ status_code = 403 , detail = "Not authorized to delete this submission"
855+ )
830856
831857 db .delete_submission (submission_id )
832858
0 commit comments