Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f156c32
working GET endpoint add, POST is setup
NikkiBytes Nov 8, 2024
05dd22b
adding working mkg parser handler
NikkiBytes Nov 20, 2024
f52acb6
added url parameter for parser methods
NikkiBytes Nov 20, 2024
51b02b5
filter for get and post output in parse
NikkiBytes Dec 3, 2024
8ee011d
updated error handling for parser
NikkiBytes Dec 4, 2024
d43b730
added get_metakg method
NikkiBytes Dec 4, 2024
3ce64f8
added missing )
NikkiBytes Dec 4, 2024
f4de5e9
added tests and clean metakg parse endpoint
NikkiBytes Dec 5, 2024
49cff33
added timeout
NikkiBytes Dec 5, 2024
eb798c5
added timeout
NikkiBytes Dec 5, 2024
663d572
error handle updates
NikkiBytes Jan 31, 2025
1028d5b
error handling update
NikkiBytes Jan 31, 2025
4bd1ad4
error handling update for parse POST
NikkiBytes Feb 5, 2025
3c3c001
flake8 clean up
NikkiBytes Feb 12, 2025
260d7d0
errors raised for unique instances with clear error message
NikkiBytes Feb 20, 2025
2d6987c
adding unique MetadataRetrivalError class for identifying metadata er…
NikkiBytes Feb 20, 2025
bad1284
mkg parser and handler clean up error code
NikkiBytes Feb 24, 2025
95f8f84
added Mixin function for improved code
NikkiBytes Mar 13, 2025
3819a83
error handling cleanup:
NikkiBytes Mar 17, 2025
4b6d261
code cleanup
NikkiBytes Mar 17, 2025
4c8d92e
code cleanup with flake8
NikkiBytes Mar 18, 2025
220e4a3
set ui key to none when empty value
NikkiBytes Mar 18, 2025
dec2502
exchanged basehandler for queryhandler in metakgparserhandler, remove…
NikkiBytes Mar 27, 2025
56af963
code cleanup, whitespaces, etc.
NikkiBytes Mar 27, 2025
8ea30ae
cleaned up excess code
NikkiBytes Mar 27, 2025
6316bf5
removed old code
NikkiBytes Apr 8, 2025
745e16a
removed not needed code
NikkiBytes Apr 15, 2025
bfbd6f0
removed print statement
NikkiBytes Apr 16, 2025
8f06ce7
style: :art: minor coding style fixes
newgene May 2, 2025
4bcd99b
refactor: :recycle: simplify and refactor metakg parsing logics
newgene May 5, 2025
3777a48
Merge branch 'main' into add-metakg-endpoint
NikkiBytes Jul 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
error handling update
  • Loading branch information
NikkiBytes committed Jan 31, 2025
commit 1028d5b230b75fca31b6872b03b360a2a4007f42
64 changes: 39 additions & 25 deletions src/handlers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,25 @@ async def get(self, *args, **kwargs):
self.finish(res)

class MetaKGParserHandler(BaseHandler):
"""
Handles parsing of SmartAPI metadata from a given URL or request body.

This handler processes SmartAPI metadata and returns structured,
cleaned results based on the specified query parameters.

Supported HTTP methods:
- **GET**: Parses metadata from a provided URL.
- **POST**: Parses metadata from the request body.

Query Parameters:
- `url` (str, required): The URL of the SmartAPI metadata to parse.
Maximum length: 1000 characters.
- `api_details` (bool, optional, default: `False`):
Whether to return detailed API information.
- `bte` (bool, optional, default: `False`):
Whether to include BTE (BioThings Explorer) specific metadata.
"""

kwargs = {
"GET": {
"url": {
Expand All @@ -713,17 +732,18 @@ def get_filtered_api(self, api_dict):
"""Extract and return filtered API information."""
api_info = api_dict["api"]

# Convert arguments to integers for consistency
bte = int(self.args.bte)
api_details = int(self.args.api_details)

# Default structure to preserve top-level keys
filtered_dict = {
"subject": api_dict.get("subject"),
"object": api_dict.get("object"),
"predicate": api_dict.get("predicate"),
"subject_prefix": api_dict.get("subject_prefix"),
"object_prefix": api_dict.get("object_prefix"),
}
key: api_dict.get(key)
for key in ["subject", "object", "predicate", "subject_prefix", "object_prefix"]
}

# case: bte=1, api_details=0
if self.args.bte == "1" and self.args.api_details == "0":
# Determine filtered API structure based on `bte` and `api_details`
if bte == 1 and api_details == 0:
filtered_api = {
**({"name": api_info["name"]} if "name" in api_info else {}),
**(
Expand All @@ -733,18 +753,12 @@ 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:
elif api_details == 1:
# Covers both (bte=0, api_details=1) and (bte=1, api_details=1)
filtered_api = api_info.copy()
if bte == 0:
filtered_api.pop("bte", None)
else: # bte == 0 and api_details == 0
filtered_api = {
**({"name": api_info["name"]} if "name" in api_info else {}),
**(
Expand All @@ -753,15 +767,17 @@ def get_filtered_api(self, api_dict):
else {}
),
}

# Add the filtered 'api' key to the preserved top-level structure
filtered_dict["api"] = filtered_api

# Remove 'bte' from 'api' if it exists
# Remove 'bte' from 'api' and move it to the top level
if "bte" in filtered_dict["api"]:
filtered_dict['bte'] = filtered_dict["api"].pop("bte", None)
filtered_dict["bte"] = filtered_dict["api"].pop("bte")

return filtered_dict


def process_apis(self, apis):
"""Process each API dict based on provided args."""
if isinstance(apis, list):
Expand All @@ -779,9 +795,7 @@ def process_apis(self, apis):

async def get(self, *args, **kwargs):
if not self.get_argument("url", None):
self.set_status(400)
self.write({"error": "Missing 'url' argument"})
return
raise HTTPError(400, reason="A url value is expected for the request, please provide a url.")

parser = MetaKGParser()
url = self.get_argument("url")
Expand Down