Skip to content

Commit

Permalink
Fixed Return Types (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbizon authored May 17, 2023
1 parent 8d095cf commit 012e382
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 22 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pytest==7.1.2
pytest-asyncio==0.15.1
pytest-dotenv==0.5.2
pyyaml==6.0
reasoner-pydantic==4.0.3
reasoner-pydantic==4.0.5
redis~=3.5.3
requests==2.28.1
uvicorn==0.17.6
Expand Down
11 changes: 3 additions & 8 deletions src/aragorn_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# from pamqp import specification as spec
from enum import Enum
from reasoner_pydantic import Query as PDQuery, AsyncQuery as PDAsyncQuery, Response as PDResponse
from reasoner_pydantic import Query as PDQuery, AsyncQuery as PDAsyncQuery, Response as PDResponse, AsyncQueryResponse, AsyncQueryStatusResponse
from pydantic import BaseModel
from fastapi import Body, FastAPI, BackgroundTasks
from src.openapi_constructor import construct_open_api_schema
Expand Down Expand Up @@ -75,13 +75,8 @@ class MethodName(str, Enum):
default_request_async: Body = Body(default=default_input_async, example=default_input_async)


# Create a async class
class AsyncReturn(BaseModel):
description: str


# async entry point
@ARAGORN_APP.post("/asyncquery", tags=["ARAGORN"], response_model=AsyncReturn)
@ARAGORN_APP.post("/asyncquery", tags=["ARAGORN"], response_model=AsyncQueryResponse)
async def async_query_handler(
background_tasks: BackgroundTasks, request: PDAsyncQuery = default_request_async, answer_coalesce_type: MethodName = MethodName.all
):
Expand Down Expand Up @@ -176,7 +171,7 @@ async def receive_aragorn_async_response(response: PDResponse) -> int:
# return the response code
return 200

@ARAGORN_APP.get("/asyncquery_status/{job_id}", tags=["ARAGORN"], status_code=200)
@ARAGORN_APP.get("/asyncquery_status/{job_id}", response_model=AsyncQueryStatusResponse, tags=["ARAGORN"], status_code=200)
async def status_query_handler(job_id: str):
"""Checks the status of an asynchronous query operation."""
return await status_query(job_id)
Expand Down
12 changes: 7 additions & 5 deletions src/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ async def async_query(background_tasks, request, answer_coalesce_type, logger, c

except KeyError as e:
logger.error(f"{guid}: Async message call key error {e}, callback URL was not specified")
return JSONResponse(content={"description": "callback URL missing"}, status_code=422)
return JSONResponse(content={"status": "Failed", "description": "callback URL missing", "job_id": guid}, status_code=422)
except ValueError as e:
logger.error(f"{guid}: Async message call value error {e}, callback URL was empty")
return JSONResponse(content={"description": "callback URL empty"}, status_code=422)
return JSONResponse(content={"status": "Failed", "description": "callback URL empty", "job_id": guid}, status_code=422)

# launch the process
background_tasks.add_task(execute_with_callback, message, answer_coalesce_type, callback_url, guid, logger, caller)
Expand All @@ -45,7 +45,9 @@ async def async_query(background_tasks, request, answer_coalesce_type, logger, c
add_item(guid, f"Query Commenced, callback url = {callback_url}", 200)

# package up the response and return it
return JSONResponse(content={"description": f"Query commenced. Will send result to {callback_url}"}, status_code=200)
return JSONResponse(content={"status": "Accepted",
"description": f"Query commenced. Will send result to {callback_url}",
"job_id": guid}, status_code=200)


async def sync_query(request, answer_coalesce_type, logger, caller="ARAGORN"):
Expand Down Expand Up @@ -202,11 +204,11 @@ async def status_query(job_id: str):
def create_logs(rows):
logs = []
for row in rows:
if row["status_code"] == "200":
if row["status_code"] == 200:
log_level = "INFO"
else:
log_level = "ERROR"
logs.append(create_log_entry(row["status_msg"], log_level, timestamp=["timestamp"]))
logs.append(create_log_entry(row["status_msg"], log_level, timestamp=[row["status_time"]]))
return logs

def callback(callback_url, final_msg, guid, logger):
Expand Down
11 changes: 3 additions & 8 deletions src/robokop_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import yaml

from enum import Enum
from reasoner_pydantic import Query as PDQuery, AsyncQuery as PDAsyncQuery, Response as PDResponse
from reasoner_pydantic import Query as PDQuery, AsyncQuery as PDAsyncQuery, Response as PDResponse, AsyncQueryStatusResponse, AsyncQueryResponse
from pydantic import BaseModel

from fastapi import Body, FastAPI, BackgroundTasks
Expand Down Expand Up @@ -64,13 +64,8 @@ class MethodName(str, Enum):
default_request_async: Body = Body(default=default_input_async, example=default_input_async)


# Create a async class
class AsyncReturn(BaseModel):
description: str


# async entry point
@ROBOKOP_APP.post("/asyncquery", tags=["ROBOKOP"], response_model=AsyncReturn)
@ROBOKOP_APP.post("/asyncquery", tags=["ROBOKOP"], response_model=AsyncQueryResponse)
async def async_query_handler(
background_tasks: BackgroundTasks, request: PDAsyncQuery = default_request_async, answer_coalesce_type: MethodName = MethodName.all
):
Expand All @@ -95,7 +90,7 @@ async def sync_query_handler(request: PDQuery = default_request_sync, answer_coa

return await sync_query(request, answer_coalesce_type, logger, "ROBOKOP")

@ROBOKOP_APP.get("/asyncquery_status", tags=["ROBOKOP"], status_code=200)
@ROBOKOP_APP.get("/asyncquery_status", tags=["ROBOKOP"], response_model=AsyncQueryStatusResponse, status_code=200)
async def status_query_handler(job_id: str):
"""Checks the status of an asynchronous query operation."""
return await status_query(job_id)
Expand Down
1 change: 1 addition & 0 deletions tests/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_add_items():
assert status_response["description"] == "The job has completed successfully."
assert len(status_response["logs"]) == 2
assert status_response["logs"][0]["message"] == "Starting job"
assert status_response["logs"][0]["level"] == "INFO"
assert status_response["logs"][1]["message"] == "Complete"

def test_running():
Expand Down

0 comments on commit 012e382

Please sign in to comment.