diff --git a/src/handlers/api.py b/src/handlers/api.py index 8869517..fdbfc6e 100644 --- a/src/handlers/api.py +++ b/src/handlers/api.py @@ -687,7 +687,6 @@ async def get(self, *args, **kwargs): self.finish(res) class MetaKGParserHandler(BaseHandler): - name="metakgparser" kwargs = { "GET": { "url": { @@ -713,6 +712,7 @@ def initialize(self, *args, **kwargs): def get_filtered_api(self, api_dict): """Extract and return filtered API information.""" api_info = api_dict["api"] + # Default structure to preserve top-level keys filtered_dict = { "subject": api_dict.get("subject"), @@ -721,6 +721,7 @@ def get_filtered_api(self, api_dict): "subject_prefix": api_dict.get("subject_prefix"), "object_prefix": api_dict.get("object_prefix"), } + # case: bte=1, api_details=0 if self.args.bte == "1" and self.args.api_details == "0": filtered_api = { @@ -732,13 +733,16 @@ def get_filtered_api(self, api_dict): ), "bte": api_info.get("bte", {}), } + # case: bte=0, api_details=1 elif self.args.bte == "0" and self.args.api_details == "1": api_info.pop("bte", None) filtered_api = api_info + # case: api_details=1, bte=1 elif self.args.bte == "1" and self.args.api_details == "1": filtered_api = api_info + # case: bte=0, api_details=0 else: filtered_api = { @@ -752,6 +756,10 @@ def get_filtered_api(self, api_dict): # Add the filtered 'api' key to the preserved top-level structure filtered_dict["api"] = filtered_api + # Remove 'bte' from 'api' if it exists + if "bte" in filtered_dict["api"]: + filtered_dict['bte'] = filtered_dict["api"].pop("bte", None) + return filtered_dict def process_apis(self, apis): @@ -796,7 +804,7 @@ async def get(self, *args, **kwargs): } self.set_header("Content-Type", "application/json") - self.write(json.dumps(response)) + self.write(response) async def post(self, *args, **kwargs): try: @@ -805,6 +813,8 @@ async def post(self, *args, **kwargs): # Parse the JSON content data = json.loads(body) parser = MetaKGParser() + self.args.api_details = self.get_argument("api_details", "0") + self.args.bte = self.get_argument("bte", "0") trapi_data = parser.get_TRAPI_metadatas(data=data) nontrapi_data = parser.get_non_TRAPI_metadatas(data=data) combined_data = trapi_data + nontrapi_data @@ -821,8 +831,7 @@ async def post(self, *args, **kwargs): } self.set_header("Content-Type", "application/json") - self.write(json.dumps(response)) + self.write(response) except json.JSONDecodeError: - self.set_status(400) - self.write({"error": "Invalid JSON format"}) + raise ValueError("Invalid JSON content in request body.") diff --git a/src/tests/_utils/metakg/integration/parser/parse.py b/src/tests/_utils/metakg/integration/parser/parse.py new file mode 100644 index 0000000..f628f43 --- /dev/null +++ b/src/tests/_utils/metakg/integration/parser/parse.py @@ -0,0 +1,80 @@ +import unittest +import requests +import json + + +class TestAPI(unittest.TestCase): + URL_EXAMPLE = "https://raw.githubusercontent.com/NCATS-Tangerine/translator-api-registry/master/mygene.info/openapi_full.yml" + + def setUp(self): + self.headers = {"Content-Type": "application/json"} + with open('/Users/nacosta/Documents/smartAPI/WORKING_BRANCH/add-metakg-endpoint/smartAPI/src/metadata_content.json', 'r') as file: + self.data = json.load(file) + + # POST Tests + def test_post_metakg_parse_api_details_1_bte_1(self): + url = "http://localhost:8000/api/metakg/parse?api_details=1&bte=1" + response = requests.post(url, headers=self.headers, json=self.data) + json_response = response.json() + self.assertEqual(response.status_code, 200) + self.assertIn('api', json_response['hits'][0].keys()) + self.assertIn('bte', json_response['hits'][0].keys()) + + def test_post_metakg_parse_api_details_0_bte_1(self): + url = "http://localhost:8000/api/metakg/parse?api_details=0&bte=1" + response = requests.post(url, headers=self.headers, json=self.data) + json_response = response.json() + self.assertEqual(response.status_code, 200) + self.assertIn('bte', json_response['hits'][0].keys()) + + def test_post_metakg_parse_api_details_1_bte_0(self): + url = "http://localhost:8000/api/metakg/parse?api_details=1&bte=0" + response = requests.post(url, headers=self.headers, json=self.data) + json_response = response.json() + self.assertEqual(response.status_code, 200) + self.assertIn('api', json_response['hits'][0].keys()) + self.assertNotIn('bte', json_response['hits'][0].keys()) + + def test_post_metakg_parse_api_details_0_bte_0(self): + url = "http://localhost:8000/api/metakg/parse?api_details=0&bte=0" + response = requests.post(url, headers=self.headers, json=self.data) + json_response = response.json() + self.assertEqual(response.status_code, 200) + self.assertNotIn('bte', json_response['hits'][0].keys()) + self.assertIn('subject', json_response['hits'][0].keys()) + + # GET Tests + def test_get_metakg_parse_api_details_1_bte_1(self): + url = f"http://localhost:8000/api/metakg/parse?url={self.URL_EXAMPLE}&api_details=1&bte=1" + response = requests.get(url) + json_response = response.json() + self.assertEqual(response.status_code, 200) + self.assertIn('api', json_response['hits'][0].keys()) + self.assertIn('bte', json_response['hits'][0].keys()) + + def test_get_metakg_parse_api_details_0_bte_1(self): + url = f"http://localhost:8000/api/metakg/parse?url={self.URL_EXAMPLE}&api_details=0&bte=1" + response = requests.get(url) + json_response = response.json() + self.assertEqual(response.status_code, 200) + self.assertIn('bte', json_response['hits'][0].keys()) + + def test_get_metakg_parse_api_details_1_bte_0(self): + url = f"http://localhost:8000/api/metakg/parse?url={self.URL_EXAMPLE}&api_details=1&bte=0" + response = requests.get(url) + json_response = response.json() + self.assertEqual(response.status_code, 200) + self.assertIn('api', json_response['hits'][0].keys()) + self.assertNotIn('bte', json_response['hits'][0].keys()) + + def test_get_metakg_parse_api_details_0_bte_0(self): + url = f"http://localhost:8000/api/metakg/parse?url={self.URL_EXAMPLE}&api_details=0&bte=0" + response = requests.get(url) + json_response = response.json() + self.assertEqual(response.status_code, 200) + self.assertNotIn('bte', json_response['hits'][0].keys()) + self.assertIn('subject', json_response['hits'][0].keys()) + + +if __name__ == "__main__": + unittest.main()