@@ -433,7 +433,7 @@ def get_filtered_api(self, api_dict):
433433 filtered_dict ["bte" ] = filtered_dict ["api" ].pop ("bte" )
434434
435435 return filtered_dict
436- class MetaKGQueryHandler (QueryHandler ):
436+ class MetaKGQueryHandler (MetaKGHandlerMixin , QueryHandler ):
437437 """
438438 Support metakg queries with biolink model's semantic descendants
439439
@@ -717,7 +717,7 @@ async def get(self, *args, **kwargs):
717717 await asyncio .sleep (0.01 )
718718 self .finish (res )
719719
720- class MetaKGParserHandler (BaseHandler ):
720+ class MetaKGParserHandler (MetaKGHandlerMixin , BaseHandler ):
721721 """
722722 Handles parsing of SmartAPI metadata from a given URL or request body.
723723
@@ -774,22 +774,20 @@ def process_apis(self, apis):
774774 apis ["api" ] = filtered_api
775775 return apis
776776
777+
777778 async def get (self , * args , ** kwargs ):
779+ url = self .get_argument ("url" , None )
778780 if not self .get_argument ("url" , None ):
779781 raise HTTPError (400 , reason = "A url value is expected for the request, please provide a url." )
780782
781- # Set initial args
783+ # Set initial args and handle potential errors in query parameters
782784 parser = MetaKGParser ()
783- url = self .get_argument ("url" )
784785
785786 try :
786787 self .args .api_details = int (self .get_argument ("api_details" , 0 ))
787- except ValueError :
788- raise HTTPError (400 , reason = f"Value, { self .get_argument ('api_details' )} , not accepted for api_details. Please enter integer, 0 or 1." )
789- try :
790788 self .args .bte = int (self .get_argument ("bte" , 0 ))
791- except ValueError :
792- raise HTTPError (400 , reason = f"Value,, { self . get_argument ( 'bte' ) } , not accepted for bte . Please enter integer, 0 or 1." )
789+ except ValueError as err :
790+ raise HTTPError (400 , reason = f"Invalid value for parameter: { str ( err ) } . Please enter integer, 0 or 1." )
793791
794792 try :
795793 trapi_data = parser .get_TRAPI_metadatas (data = None , url = url )
@@ -806,18 +804,17 @@ async def get(self, *args, **kwargs):
806804 except DownloadError :
807805 raise HTTPError (400 , reason = "There was an error downloading the data from the given input." )
808806
809- combined_data = trapi_data + nontrapi_data
810-
811807 # Apply filtering -- if data found
808+ combined_data = trapi_data + nontrapi_data
812809 if combined_data :
813810 for i , api_dict in enumerate (combined_data ):
814- filtered_api = self .get_filtered_api (api_dict )
815- combined_data [ i ] = filtered_api
816- # parser does not pick up this information, so we add it here
817- if self .args .api_details == 1 :
818- for data_dict in combined_data :
819- if "metadata" in data_dict ["api" ]["smartapi" ] and data_dict ["api" ]["smartapi" ]["metadata" ] is None :
820- data_dict ["api" ]["smartapi" ]["metadata" ] = self . args . url
811+ combined_data [ i ] = self .get_filtered_api (api_dict )
812+
813+ # Add url to metadata if api_details is set to 1
814+ if self .args .api_details == 1 :
815+ for data_dict in combined_data :
816+ if "metadata" in data_dict ["api" ]["smartapi" ] and data_dict ["api" ]["smartapi" ]["metadata" ] is None :
817+ data_dict ["api" ]["smartapi" ]["metadata" ] = url
821818
822819 response = {
823820 "total" : len (combined_data ),
@@ -829,62 +826,56 @@ async def get(self, *args, **kwargs):
829826 async def post (self , * args , ** kwargs ):
830827 if not self .request .body :
831828 raise HTTPError (400 , reason = "Request body cannot be empty." )
832- content_type = self .request .headers .get ("Content-Type" , "" )
833- data_body = self .request .body
834829
835- if content_type == "application/json" :
836- try :
837- data = to_dict (data_body , ctype = "application/json" )
838- except ValueError :
839- raise HTTPError (400 , reason = "Invalid data. Please provide a valid JSON object." )
840- except TypeError :
841- raise HTTPError (400 , reason = "Invalid data type. Please provide a valid type." )
842- if content_type == "application/x-yaml" :
843- try :
844- data = to_dict (data_body )
845- except ValueError :
846- raise HTTPError (400 , reason = "Invalid input data. Please provide a valid YAML object." )
847- except TypeError :
848- raise HTTPError (400 , reason = "Invalid type data. Please provide a valid type." )
849- # # Ensure the parsed data is a dictionary
850- if not isinstance (data , dict ):
851- raise ValueError ("Invalid input data. Please provide a valid JSON/YAML object." )
852-
853- parser = MetaKGParser ()
830+ content_type = self .request .headers .get ("Content-Type" , "" ).lower ()
831+ raw_body = self .request .body
854832
833+ # Try to parse the request body based on content type
855834 try :
856- self .args .api_details = int (self .get_argument ("api_details" , 0 ))
857- except ValueError :
858- raise HTTPError (400 , reason = f"Unexcepted value for api_details, { self .get_argument ('api_details' )} . Please enter integer, 0 or 1." )
835+ if content_type == "application/json" :
836+ data = to_dict (raw_body , ctype = "application/json" )
837+ elif content_type == "application/x-yaml" :
838+ data = to_dict (raw_body , ctype = "application/x-yaml" )
839+ else :
840+ # Default to YAML parsing if the content type is unknown or not specified
841+ data = to_dict (raw_body )
842+ except ValueError as val_err :
843+ if 'mapping values are not allowed here' in str (val_err ):
844+ raise HTTPError (400 , reason = "Formatting issue, please consider using --data-binary to maintain YAML format." )
845+ else :
846+ raise HTTPError (400 , reason = "Invalid value, please provide a valid YAML object." )
847+ except TypeError :
848+ raise HTTPError (400 , reason = "Invalid type, provide valid type metadata." )
849+
850+ # Ensure the parsed data is a dictionary
851+ if not isinstance (data , dict ):
852+ raise ValueError ("Invalid input data type. Please provide a valid JSON/YAML object." )
859853
854+ # Extract query parameters (assuming these need to be parsed from the request)
860855 try :
856+ self .args .api_details = int (self .get_argument ("api_details" , 0 ))
861857 self .args .bte = int (self .get_argument ("bte" , 0 ))
862- except ValueError :
863- raise HTTPError (400 , reason = f"Unexcepted value for bte, { self . get_argument ( 'bte' ) } . Please enter integer, 0 or 1. " )
858+ except ValueError as err :
859+ raise HTTPError (400 , reason = f"Invalid query parameter: { str ( err ) } " )
864860
865- # Process metadata
861+ # Process the parsed metadata
862+ parser = MetaKGParser ()
866863 try :
867864 trapi_data = parser .get_TRAPI_metadatas (data = data )
868- except MetadataRetrievalError as retrieve_err :
869- raise HTTPError (retrieve_err .status_code , reason = retrieve_err .message )
870- except DownloadError :
871- raise HTTPError (400 , reason = "There was an error downloading the data from the given input." )
872-
873- try :
874865 nontrapi_data = parser .get_non_TRAPI_metadatas (data = data )
875866 except MetadataRetrievalError as retrieve_err :
876867 raise HTTPError (retrieve_err .status_code , reason = retrieve_err .message )
877868 except DownloadError :
878- raise HTTPError (400 , reason = "There was an error downloading the data from the given input." )
869+ raise HTTPError (400 , reason = "Error downloading the data from the provided input." )
879870
880871 combined_data = trapi_data + nontrapi_data
881872
882- # Apply filtering -- if data found
873+ # Apply filtering to the combined data
883874 if combined_data :
884875 for i , api_dict in enumerate (combined_data ):
885- filtered_api = self .get_filtered_api (api_dict )
886- combined_data [i ] = filtered_api
876+ combined_data [i ] = self .get_filtered_api (api_dict )
887877
878+ # Send the response back to the client
888879 response = {
889880 "total" : len (combined_data ),
890881 "hits" : combined_data ,
0 commit comments