|
1 | 1 | """Nylas router module.""" |
2 | 2 |
|
| 3 | +import asyncio |
3 | 4 | from fastapi import ( |
4 | 5 | APIRouter, |
5 | 6 | Depends, |
| 7 | + HTTPException, |
6 | 8 | ) |
| 9 | +import httpx |
7 | 10 | from odmantic.session import ( |
8 | 11 | AIOSession, |
9 | 12 | ) |
|
29 | 32 |
|
30 | 33 | router = APIRouter(prefix="/api/v1") |
31 | 34 |
|
| 35 | +JUDGE0_API_URL = "https://judge0-ce.p.rapidapi.com/submissions" |
| 36 | + |
| 37 | +headers = { |
| 38 | + "X-RapidAPI-Key": settings().RAPIDAPI_KEY, |
| 39 | + "Content-Type": "application/json", |
| 40 | +} |
| 41 | + |
| 42 | +submission_status_dict = {} |
| 43 | + |
32 | 44 |
|
33 | 45 | @router.post( |
34 | 46 | "/nylas/generate-auth-url", |
@@ -334,3 +346,77 @@ async def search_emails( |
334 | 346 | item.as_json(enforce_read_only=False) for item in threads_with_messages |
335 | 347 | ] |
336 | 348 | return res_json |
| 349 | + |
| 350 | + |
| 351 | +@router.post( |
| 352 | + "/nylas/execute-code", |
| 353 | + response_model=None, |
| 354 | + status_code=200, |
| 355 | + name="nylas:execute-code", |
| 356 | +) |
| 357 | +async def execute_code( |
| 358 | + request_body: nylas_schemas.CodeExecutionSchema, |
| 359 | + current_user: users_schemas.UserObjectSchema = Depends( |
| 360 | + dependencies.get_current_user |
| 361 | + ), |
| 362 | +) -> Any: |
| 363 | + try: |
| 364 | + payload = { |
| 365 | + "source_code": request_body.code, |
| 366 | + "language_id": int(request_body.language_id), |
| 367 | + "stdin": "", |
| 368 | + "expected_output": "", |
| 369 | + "cpu_time_limit": 2, |
| 370 | + "cpu_extra_time": 0.5, |
| 371 | + "wall_time_limit": 5, |
| 372 | + "memory_limit": 512000, |
| 373 | + } |
| 374 | + |
| 375 | + async with httpx.AsyncClient() as client: |
| 376 | + response = await client.post( |
| 377 | + JUDGE0_API_URL, |
| 378 | + json=payload, |
| 379 | + headers=headers, |
| 380 | + ) |
| 381 | + |
| 382 | + if response.status_code == 201: |
| 383 | + submission_token = response.json()["token"] |
| 384 | + submission_status_dict[submission_token] = "Running" |
| 385 | + timeout = 60 |
| 386 | + start_time = asyncio.get_event_loop().time() |
| 387 | + result = None |
| 388 | + while submission_status_dict.get(submission_token) == "Running": |
| 389 | + await asyncio.sleep(1) |
| 390 | + |
| 391 | + if asyncio.get_event_loop().time() - start_time > timeout: |
| 392 | + print("Timeout") |
| 393 | + submission_status_dict[submission_token] = "Timeout" |
| 394 | + break |
| 395 | + |
| 396 | + async with httpx.AsyncClient() as client: |
| 397 | + new_response = await client.get( |
| 398 | + f"{JUDGE0_API_URL}/{submission_token}", |
| 399 | + headers=headers, |
| 400 | + ) |
| 401 | + |
| 402 | + if new_response.status_code == 200: |
| 403 | + print(new_response.json()) |
| 404 | + submission_stdout = new_response.json()["stdout"] |
| 405 | + if submission_stdout: |
| 406 | + print("Finished") |
| 407 | + submission_status_dict[submission_token] = "Finished" |
| 408 | + result = new_response.json() |
| 409 | + break |
| 410 | + else: |
| 411 | + return HTTPException( |
| 412 | + status_code=500, detail="Failed to retrieve result" |
| 413 | + ) |
| 414 | + return result |
| 415 | + else: |
| 416 | + return HTTPException( |
| 417 | + status_code=500, detail="Code execution failed" |
| 418 | + ) |
| 419 | + |
| 420 | + except Exception as e: |
| 421 | + print(e) |
| 422 | + return HTTPException(status_code=500, detail=str(e)) |
0 commit comments