From 012e3825ba2f6535ead54caf9b21a5950910f8d6 Mon Sep 17 00:00:00 2001 From: cbizon Date: Wed, 17 May 2023 09:41:11 -0400 Subject: [PATCH] Fixed Return Types (#174) --- requirements.txt | 2 +- src/aragorn_app.py | 11 +++-------- src/common.py | 12 +++++++----- src/robokop_app.py | 11 +++-------- tests/test_status.py | 1 + 5 files changed, 15 insertions(+), 22 deletions(-) diff --git a/requirements.txt b/requirements.txt index baa9f4d..19b5af1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/src/aragorn_app.py b/src/aragorn_app.py index f36d697..0ab467d 100644 --- a/src/aragorn_app.py +++ b/src/aragorn_app.py @@ -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 @@ -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 ): @@ -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) diff --git a/src/common.py b/src/common.py index 65c913b..ac3f6cd 100644 --- a/src/common.py +++ b/src/common.py @@ -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) @@ -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"): @@ -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): diff --git a/src/robokop_app.py b/src/robokop_app.py index 91915cb..e48e9ed 100644 --- a/src/robokop_app.py +++ b/src/robokop_app.py @@ -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 @@ -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 ): @@ -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) diff --git a/tests/test_status.py b/tests/test_status.py index 6fe8aa2..a172cca 100644 --- a/tests/test_status.py +++ b/tests/test_status.py @@ -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():