2222from utils .metakg .cytoscape_formatter import CytoscapeDataFormatter
2323from utils .metakg .biolink_helpers import get_expanded_values
2424from utils .notification import SlackNewAPIMessage , SlackNewTranslatorAPIMessage
25+ from utils .metakg .parser import MetaKGParser
2526
2627logger = logging .getLogger ("smartAPI" )
2728
29+ from tornado .web import RequestHandler
2830
2931def github_authenticated (func ):
3032 """
@@ -687,7 +689,7 @@ async def get(self, *args, **kwargs):
687689 await asyncio .sleep (0.01 )
688690 self .finish (res )
689691
690- class MetaKGParserHandler (QueryHandler ):
692+ class MetaKGParserHandler (BaseHandler ): #RequestHandler/BaseAPIHandler
691693 name = "metakgparser"
692694 kwargs = {
693695 "GET" : {
@@ -698,20 +700,79 @@ class MetaKGParserHandler(QueryHandler):
698700 "description" : "URL of the SmartAPI metadata to parse"
699701 },
700702 },
701- "POST" : {
702- "type" : dict ,
703- "required" : True ,
704- "description" : "Metadata content of the SmartAPI in JSON format"
705- },
703+ "POST" : { }
706704 }
707-
708705
709706 async def get (self , * args , ** kwargs ):
710707 if self .request .method == "GET" :
711- smartapi = SmartAPI (self .args .url )
712- content = smartapi .get_metakg (metadata_url = self .args .url )
713- self .finish (f"{ content } " )
714- elif self .request .method == "POST" :
715- print (f"\n \n [INFO] HERE" )
716- # pass
717- self .finish (f"HERE" )
708+ if not self .get_argument ("url" , None ): # Check if the 'url' argument is present
709+ self .set_status (400 )
710+ self .write ({"error" : "Missing 'url' argument" })
711+ return
712+ # smartapi = API(url=self.get_argument("url"))
713+ # metakg_doc = smartapi.get_metakg()
714+ # call parser // pass URL to parser
715+ parser = MetaKGParser ()
716+ url = self .get_argument ("url" )
717+
718+ trapi_data = parser .get_TRAPI_metadatas (data = None , url = url )
719+ nontrapi_data = parser .get_non_TRAPI_metadatas (data = None , url = url )
720+ combined_data = trapi_data + nontrapi_data
721+
722+ # Transform the combined data to include an 'api' key and organize it into 'hits'
723+ hits = []
724+ for edge in combined_data :
725+ print (f"\n { json .dumps (edge , indent = 4 )} \n " )
726+ transformed_edge = {
727+ "_id" : edge ['api' ].get ("_id" ), # Include an ID if available
728+ "_score" : 1 , # Placeholder for scoring logic
729+ "api" : {
730+ "name" : edge ['api' ].get ("name" ), # Replace with actual API name key
731+ "smartapi" : {
732+ "id" : edge ['api' ]['smartapi' ].get ("id" ) # Replace with actual SmartAPI ID key
733+ }
734+ },
735+ "subject" : edge .get ("subject" ),
736+ "subject_prefix" : edge .get ("subject_prefix" ),
737+ "predicate" : edge .get ("predicate" ),
738+ "object" : edge .get ("object" ),
739+ "object_prefix" : edge .get ("object_prefix" ),
740+ }
741+ hits .append (transformed_edge )
742+
743+ # Create final response format
744+ response = {
745+ "took" : 1 , # Placeholder for actual timing logic
746+ "total" : len (hits ),
747+ "max_score" : 1 , # Placeholder for scoring logic
748+ "hits" : hits
749+ }
750+
751+ # Write response
752+ self .set_header ("Content-Type" , "application/json" )
753+ self .write (json .dumps (response ))
754+
755+ async def post (self , * args , ** kwargs ):
756+ try :
757+ # Read the raw request body
758+ body = self .request .body
759+ # Parse the JSON content
760+ data = json .loads (body )
761+ parser = MetaKGParser ()
762+ trapi_data = parser .get_TRAPI_metadatas (data = data )
763+ nontrapi_data = parser .get_non_TRAPI_metadatas (data = data )
764+ combined_data = trapi_data + nontrapi_data
765+ # self.write(json.dumps(combined_data))
766+ # Clean up the metakg_doc to remove the 'api' key
767+ cleaned_metakg_doc = []
768+ for edge in combined_data :
769+ if 'api' in edge :
770+ edge .pop ('api' ) # Remove the 'api' key
771+ cleaned_metakg_doc .append (edge )
772+
773+ # Return the cleaned metakg document
774+ self .set_header ("Content-Type" , "application/json" )
775+ self .write (json .dumps (cleaned_metakg_doc )) # make dict
776+ except json .JSONDecodeError :
777+ self .set_status (400 )
778+ self .write ({"error" : "Invalid JSON format" })
0 commit comments