Skip to content

Commit 3c3c001

Browse files
committed
flake8 clean up
1 parent 4bd1ad4 commit 3c3c001

File tree

3 files changed

+34
-109
lines changed

3 files changed

+34
-109
lines changed

src/handlers/api.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -688,21 +688,21 @@ async def get(self, *args, **kwargs):
688688

689689
class MetaKGParserHandler(BaseHandler):
690690
"""
691-
Handles parsing of SmartAPI metadata from a given URL or request body.
691+
Handles parsing of SmartAPI metadata from a given URL or request body.
692692
693-
This handler processes SmartAPI metadata and returns structured,
694-
cleaned results based on the specified query parameters.
693+
This handler processes SmartAPI metadata and returns structured,
694+
cleaned results based on the specified query parameters.
695695
696696
Supported HTTP methods:
697697
- **GET**: Parses metadata from a provided URL.
698698
- **POST**: Parses metadata from the request body.
699699
700700
Query Parameters:
701-
- `url` (str, required): The URL of the SmartAPI metadata to parse.
701+
- `url` (str, required): The URL of the SmartAPI metadata to parse.
702702
Maximum length: 1000 characters.
703-
- `api_details` (bool, optional, default: `False`):
703+
- `api_details` (bool, optional, default: `False`):
704704
Whether to return detailed API information.
705-
- `bte` (bool, optional, default: `False`):
705+
- `bte` (bool, optional, default: `False`):
706706
Whether to include BTE (BioThings Explorer) specific metadata.
707707
"""
708708

@@ -775,7 +775,6 @@ def get_filtered_api(self, api_dict):
775775

776776
return filtered_dict
777777

778-
779778
def process_apis(self, apis):
780779
"""Process each API dict based on provided args."""
781780
if isinstance(apis, list):
@@ -798,20 +797,25 @@ async def get(self, *args, **kwargs):
798797
# Set initial args
799798
parser = MetaKGParser()
800799
url = self.get_argument("url")
801-
self.args.api_details = int(self.get_argument("api_details", 0))
802-
self.args.bte = int(self.get_argument("bte", 0))
800+
try:
801+
self.args.api_details = int(self.get_argument("api_details", 0))
802+
except ValueError:
803+
raise HTTPError(400, reason=f"Unexcepted value for api_details, {self.get_argument('api_details')}. Please enter integer, 0 or 1.")
804+
try:
805+
self.args.bte = int(self.get_argument("bte", 0))
806+
except ValueError:
807+
raise HTTPError(400, reason=f"Unexcepted value for bte, {self.get_argument('bte')}. Please enter integer, 0 or 1.")
803808

804809
# Get data
805810
trapi_data = parser.get_TRAPI_metadatas(data=None, url=url)
806811
nontrapi_data = parser.get_non_TRAPI_metadatas(data=None, url=url)
807812
combined_data = trapi_data + nontrapi_data
808813

809-
if not combined_data:
810-
raise HTTPError(404, reason="Metadata not found.")
811-
812-
for i, api_dict in enumerate(combined_data):
813-
filtered_api = self.get_filtered_api(api_dict)
814-
combined_data[i] = filtered_api
814+
# Apply filtering -- if data found
815+
if combined_data:
816+
for i, api_dict in enumerate(combined_data):
817+
filtered_api = self.get_filtered_api(api_dict)
818+
combined_data[i] = filtered_api
815819

816820
response = {
817821
"took": 1,
@@ -831,32 +835,34 @@ async def post(self, *args, **kwargs):
831835
try:
832836
data = json.loads(self.request.body)
833837
except json.JSONDecodeError:
834-
raise HTTPError(400, reason="Invalid JSON content in request body.")
838+
raise HTTPError(400, reason=f"Unexcepted value for api_details, {self.get_argument('api_details')}. Please enter integer, 0 or 1.")
835839

836840
# Ensure the parsed data is a dictionary
837841
if not isinstance(data, dict):
838-
raise HTTPError(400, reason="Invalid JSON format. Expected a JSON object.")
842+
raise HTTPError(400, reason=f"Unexcepted value for bte, {self.get_argument('bte')}. Please enter integer, 0 or 1.")
839843

840844
parser = MetaKGParser()
841-
845+
842846
try:
843847
self.args.api_details = int(self.get_argument("api_details", 0))
844-
self.args.bte = int(self.get_argument("bte", 0))
845848
except ValueError:
846849
raise HTTPError(400, reason="Invalid query parameter value. 'api_details' and 'bte' must be integers.")
847850

851+
try:
852+
self.args.bte = int(self.get_argument("bte", 0))
853+
except ValueError:
854+
raise HTTPError(400, reason=f"Unexcepted value for bte, {self.get_argument('bte')}. Please enter integer, 0 or 1.")
855+
848856
# Process metadata
849857
trapi_data = parser.get_TRAPI_metadatas(data=data)
850858
nontrapi_data = parser.get_non_TRAPI_metadatas(data=data)
851859
combined_data = trapi_data + nontrapi_data
852860

853-
if not combined_data:
854-
raise HTTPError(404, reason="Metadata not found.")
855-
856-
# Apply filtering
857-
for i, api_dict in enumerate(combined_data):
858-
filtered_api = self.get_filtered_api(api_dict)
859-
combined_data[i] = filtered_api
861+
# Apply filtering -- if data found
862+
if combined_data:
863+
for i, api_dict in enumerate(combined_data):
864+
filtered_api = self.get_filtered_api(api_dict)
865+
combined_data[i] = filtered_api
860866

861867
response = {
862868
"took": 1,
@@ -866,4 +872,4 @@ async def post(self, *args, **kwargs):
866872
}
867873

868874
self.set_header("Content-Type", "application/json")
869-
self.write(response)
875+
self.write(response)

src/tests/_utils/metakg/integration/parser/parse.py

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/utils/metakg/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def get_TRAPI_with_metakg_endpoint(self, data=None, url=None):
6363
parser = API(smartapi_doc=data) if data else API(url=url)
6464
try:
6565
metadata = parser.metadata
66-
except DownloadError as dl_err:
66+
except DownloadError:
6767
raise HTTPError(400, reason="Error fetching data from given input.")
6868
_paths = metadata.get("paths", {})
6969
_team = metadata.get("x-translator", {}).get("team")

0 commit comments

Comments
 (0)