1616from controller import SmartAPI
1717from controller .exceptions import ControllerError , NotFoundError
1818from pipeline import MetaKGQueryPipeline
19- from utils .decoder import to_dict
2019from utils .downloader import DownloadError , download_async
2120from utils .metakg .biolink_helpers import get_expanded_values
2221from utils .metakg .cytoscape_formatter import CytoscapeDataFormatter
2322from utils .metakg .export import edges2graphml
24- from utils .metakg .metakg_errors import MetadataRetrievalError
2523from utils .metakg .parser import MetaKGParser
2624from utils .metakg .path_finder import MetaKGPathFinder
2725from utils .notification import SlackNewAPIMessage , SlackNewTranslatorAPIMessage
@@ -751,19 +749,17 @@ class MetaKGParserHandler(BaseHandler, MetaKGHandlerMixin):
751749 """
752750
753751 kwargs = {
752+ "*" : {
753+ "api_details" : {"type" : bool , "default" : False },
754+ "bte" : {"type" : bool , "default" : False },
755+ },
754756 "GET" : {
755757 "url" : {
756758 "type" : str ,
757759 "required" : True ,
758760 "max" : 1000 ,
759761 "description" : "URL of the SmartAPI metadata to parse"
760762 },
761- "api_details" : {"type" : bool , "default" : False },
762- "bte" : {"type" : bool , "default" : False },
763- },
764- "POST" : {
765- "api_details" : {"type" : bool , "default" : False },
766- "bte" : {"type" : bool , "default" : False },
767763 },
768764 }
769765
@@ -789,95 +785,69 @@ def process_apis(self, apis):
789785
790786 async def get (self , * args , ** kwargs ):
791787 url = self .args .url
792- if not url :
793- raise HTTPError (400 , reason = "A url value is expected for the request, please provide a url." )
794-
795- # Set initial args and handle potential errors in query parameters
796788 parser = MetaKGParser ()
797789
798790 try :
799- trapi_data = parser .get_TRAPI_metadatas (data = None , url = url )
800- except MetadataRetrievalError as retrieve_err :
801- raise HTTPError (retrieve_err .status_code , reason = retrieve_err .message )
802- except DownloadError :
803- raise HTTPError (400 , reason = "There was an error downloading the data from the given input." )
804-
805- # Get non-TRAPI metadata
806- try :
807- nontrapi_data = parser .get_non_TRAPI_metadatas (data = None , url = url )
808- except MetadataRetrievalError as retrieve_err :
809- raise HTTPError (retrieve_err .status_code , reason = retrieve_err .message )
791+ parsed_metakg = parser .get_metakg (url = url )
810792 except DownloadError :
811- raise HTTPError (400 , reason = "There was an error downloading the data from the given input." )
793+ self .write_error (400 , reason = "There was an error downloading the data from the given url." )
794+ except (ValueError , TypeError ) as err :
795+ self .write_error (
796+ status_code = 400 ,
797+ reason = "The data retrived from the given url is not a valid JSON or YAML object." ,
798+ message = str (err )
799+ )
812800
813801 # Apply filtering -- if data found
814- combined_data = trapi_data + nontrapi_data
815- if combined_data :
816- for i , api_dict in enumerate (combined_data ):
817- combined_data [i ] = self .get_filtered_api (api_dict )
802+ if parsed_metakg :
803+ for i , api_dict in enumerate (parsed_metakg ):
804+ parsed_metakg [i ] = self .get_filtered_api (api_dict )
818805
819806 # Add url to metadata if api_details is set to 1
820807 if self .args .api_details :
821- for data_dict in combined_data :
808+ for data_dict in parsed_metakg :
822809 if "metadata" in data_dict ["api" ]["smartapi" ] and data_dict ["api" ]["smartapi" ]["metadata" ] is None :
823810 data_dict ["api" ]["smartapi" ]["metadata" ] = url
824811
825812 response = {
826- "total" : len (combined_data ),
827- "hits" : combined_data ,
813+ "total" : len (parsed_metakg ),
814+ "hits" : parsed_metakg ,
828815 }
829816
830817 self .finish (response )
831818
832819 async def post (self , * args , ** kwargs ):
833- raw_body = self .request .body
834- if not raw_body :
835- raise HTTPError (400 , reason = "Request body cannot be empty." )
836-
837820 content_type = self .request .headers .get ("Content-Type" , "" ).lower ()
821+ if content_type in ["application/json" , "application/x-yaml" ]:
822+ # if content type is set properly, it should have alrady been parsed
823+ metadata_from_body = self .args_json or self .args_yaml
824+ elif self .request .body :
825+ # if request body is provided but no proper content type is set
826+ # we will parse it as YAML anyway
827+ metadata_from_body = self ._parse_yaml ()
828+ else :
829+ metadata_from_body = None
830+
831+ if metadata_from_body :
832+ # Process the parsed metadata
833+ parser = MetaKGParser ()
834+ parsed_metakg = parser .get_metakg (metadata_from_body )
835+
836+ # Apply filtering to the combined data
837+ if parsed_metakg :
838+ for i , api_dict in enumerate (parsed_metakg ):
839+ parsed_metakg [i ] = self .get_filtered_api (api_dict )
840+
841+ # Send the response back to the client
842+ response = {
843+ "total" : len (parsed_metakg ),
844+ "hits" : parsed_metakg ,
845+ }
838846
839- # Try to parse the request body based on content type
840- try :
841- if content_type == "application/json" :
842- data = to_dict (raw_body , ctype = "application/json" )
843- elif content_type == "application/x-yaml" :
844- data = to_dict (raw_body , ctype = "application/x-yaml" )
845- else :
846- # Default to YAML parsing if the content type is unknown or not specified
847- data = to_dict (raw_body )
848- except ValueError as val_err :
849- if 'mapping values are not allowed here' in str (val_err ):
850- raise HTTPError (400 , reason = "Formatting issue, please consider using --data-binary to maintain YAML format." )
851- else :
852- raise HTTPError (400 , reason = "Invalid value, please provide a valid YAML object." )
853- except TypeError :
854- raise HTTPError (400 , reason = "Invalid type, provide valid type metadata." )
855-
856- # Ensure the parsed data is a dictionary
857- if not isinstance (data , dict ):
858- raise ValueError ("Invalid input data type. Please provide a valid JSON/YAML object." )
859-
860- # Process the parsed metadata
861- parser = MetaKGParser ()
862- try :
863- trapi_data = parser .get_TRAPI_metadatas (data = data )
864- nontrapi_data = parser .get_non_TRAPI_metadatas (data = data )
865- except MetadataRetrievalError as retrieve_err :
866- raise HTTPError (retrieve_err .status_code , reason = retrieve_err .message )
867- except DownloadError :
868- raise HTTPError (400 , reason = "Error downloading the data from the provided input." )
869-
870- combined_data = trapi_data + nontrapi_data
871-
872- # Apply filtering to the combined data
873- if combined_data :
874- for i , api_dict in enumerate (combined_data ):
875- combined_data [i ] = self .get_filtered_api (api_dict )
876-
877- # Send the response back to the client
878- response = {
879- "total" : len (combined_data ),
880- "hits" : combined_data ,
881- }
882-
883- self .finish (response )
847+ self .finish (response )
848+ else :
849+ self .write_error (
850+ status_code = 400 ,
851+ reason = "Request body cannot be empty." ,
852+ message = "Please provide a valid JSON/YAML object in the request body."
853+ )
0 commit comments