diff --git a/analysis_module/mockserver.py b/analysis_module/mockserver.py index a17b466..bf3a19d 100644 --- a/analysis_module/mockserver.py +++ b/analysis_module/mockserver.py @@ -17,7 +17,7 @@ from core.models import NLPRequest from core_server.settings import ENDPOINT_NAME from .mock_templates import MOCK_ENTRY_CLASSIFICATION, MOCK_ENTRY_CLASSIFICATION_FORMATTED, MOCK_GEOLOCATION # noqa -from .utils import send_callback_url_request +from .utils import send_callback_url_request, send_callback_url_request_for_summarization logger = logging.getLogger("__name__") @@ -27,8 +27,8 @@ def get_entries_data(url: str, timeout: int=30) -> Any: """get data""" response = requests.get(url, timeout=timeout) - entries_data = json.loads(response.text) - return entries_data + return response.json() #json.loads(response.text) + #return entries_data def save_data_local_and_get_url(dir_name: str, client_id: str, data: Any) -> str: @@ -135,23 +135,35 @@ def process_summarization(body: dict) -> Any: input_payload = get_entries_data(entries_url) excerpts = [x["excerpt"] for x in input_payload["data"]] except Exception: - send_callback_url_request( + send_callback_url_request_for_summarization( callback_url=callback_url, client_id=client_id, - filepath="", + summary_filepath="", + analytical_statement_filepath="", + info_gaps_filepath="", status=NLPRequest.RequestStatus.PROCESS_INPUT_URL_FAILED, ) return - data = " ".join(["This is a fake response.\n"] + excerpts) - filepath = save_data_local_and_get_url( - dir_name="summarization", client_id=client_id, data=data + summary_data = " ".join(["This is a fake response.\n"] + excerpts) + summary_filepath = save_data_local_and_get_url( + dir_name="summarization", client_id=client_id, data=summary_data + ) + analytical_statement_data = " ".join(["This is autogenerated Analytical Statement.\n"] + excerpts) + analytical_statement_filepath = save_data_local_and_get_url( + dir_name="summarization", client_id=client_id, data=analytical_statement_data + ) + info_gaps_data = " ".join(["This is autogenerated info gaps.\n"] + excerpts) + info_gaps_filepath = save_data_local_and_get_url( + dir_name="summarization", client_id=client_id, data=info_gaps_data ) - send_callback_url_request( + send_callback_url_request_for_summarization( callback_url=callback_url, client_id=client_id, - filepath=filepath, + summary_filepath=summary_filepath, + analytical_statement_filepath=analytical_statement_filepath, + info_gaps_filepath=info_gaps_filepath, status=NLPRequest.RequestStatus.SUCCESS, ) diff --git a/analysis_module/utils.py b/analysis_module/utils.py index 90b8cd7..64c2f34 100644 --- a/analysis_module/utils.py +++ b/analysis_module/utils.py @@ -161,6 +161,46 @@ def send_callback_url_request(callback_url: str, client_id: str, filepath: str, logging.error("No callback url found.") return json.dumps({"status": "No callback url found."}), 400 +@shared_task +def send_callback_url_request_for_summarization( + callback_url: str, + client_id: str, + summary_filepath: str, + analytical_statement_filepath: str, + info_gaps_filepath: str, + status: int +) -> Any: + """send callback url""" + + time.sleep(1) + if callback_url: + response_callback_url = requests.post( + callback_url, + json={ + "client_id": client_id, + "summary_url": summary_filepath, + "analytical_statement_url": analytical_statement_filepath, + "info_gaps_url": info_gaps_filepath, + "status": status, + }, + timeout=30, + ) + if response_callback_url.status_code == 200: + logging.info("Request sent successfully.") + return json.dumps({"status": "Request sent successfully."}) + else: + logging.info( + f"Some errors occurred. StatusCode: {response_callback_url.status_code}" + ) + return json.dumps( + { + "status": f"Error occurred with statuscode: {response_callback_url.status_code}" + } + ) + + logging.error("No callback url found.") + return json.dumps({"status": "No callback url found."}), 400 + def get_geolocations(excerpts: List[str], req_timeout: int = 60): """ Get geolocations from excerpts by requesting from geolocation module """