def submit(req): """Query submission""" if req.method != 'POST': return HttpResponse('Only POST is permitted!', status=405) try: data = json.loads(req.body) # if 'message' not in data: # return HttpResponse('Not a valid Translator query json', status=400) # create a head message try: validate_Query(data) except ValidationError as ve: logger.debug("Warning! Input query failed TRAPI validation " + str(data)) logger.debug(ve) return HttpResponse('Input query failed TRAPI validation', status=400) message = Message.create(code=200, status='Running', data=data, actor=get_default_actor()) if 'name' in data: message.name = data['name'] # save and broadcast message.save() data = message.to_dict() return HttpResponse(json.dumps(data, indent=2), content_type='application/json', status=201) except: logger.debug("Unexpected error: %s" % sys.exc_info()) return HttpResponse('Content is not JSON', status=400)
def validateRequestBody(self): """ Verify its a valid TRAPI request :return: None :raises: Will raise if not valid """ # todo, Multiomics provider is sending bad TRAPI format! reasoner_validator.validate_Query(self.requestBody)
def userRequestBodyValidation(self): """ A function to evaluate whether the JSON body received from the client conforms to the proper input standard. :return: Boolean: True meaning the body is valid, False meaning the body is not valid """ try: reasoner_validator.validate_Query(self.userRequestBody) return {"isValid": True, "error": None} except ValidationError as e: return {"isValid": False, "error": e}
def userRequestBodyValidate(body: dict): """ A function to evaluate whether the JSON body received from the client conforms to the proper input standard. :param body: A dictionary representing a JSON body :return: Boolean: True meaning the body is valid, False meaning the body is not valid """ try: reasoner_validator.validate_Query(body) return None except ValidationError as e: return e
def knowledgeProviderResponseBodyIsValid(body: dict): """ A function to evaluate whether the JSON body received from the knowledge provider conforms to our assumptions. :param body: A dictionary representing a JSON body :return: Boolean: True meaning the body is valid, False meaning the body is not valid (the knowledge provider has changed their api!) """ try: reasoner_validator.validate_Query(body) return True except Exception as e: return False
def query(self, query: dict): """ Sends a query to MolePro. :param query: TRAPI Message JSON object to send to the Genetics Provider :return: Query object of the response. """ try: response = requests.post(url=MolecularDataProvider.url, json=query) response.raise_for_status() except Exception as e: raise e # verify it is a TRAPI response try: trapi_response = response.json() reasoner_validator.validate_Query(trapi_response) except Exception as e: raise e return trapi_response
def buildOneHopRequestBody(node1: dict, node2: dict, edge: dict): """ Builds a TRAPI one hop query message. :param node1: node1 dictionary :param node2: node2 dictioanry :param edge: edge dictionary :return: """ requestBody = { "message": { "query_graph": { "edges": { "e0": { "subject": "n0", "predicate": edge["predicate"], "object": "n1" } }, "nodes": { "n0": { "category": node1["category"] }, "n1": { "category": node2["category"] } } } } } if "id" in node1: requestBody["message"]["query_graph"]["nodes"]["n0"]["id"] = node1[ "id"] if "id" in node2: requestBody["message"]["query_graph"]["nodes"]["n0"]["id"] = node2[ "id"] # ensure the message is valid before returning it reasoner_validator.validate_Query(requestBody) return requestBody
def build_one_hop_query(node_1, node_2, edge): """ Builds a TRAPI one hop query message. """ message = { "message": { "query_graph": { "edges": { "e0": { "subject": "n0", "predicate": edge["predicate"], "object": "n1" } }, "nodes": { "n0": { "category": node_1["category"] }, "n1": { "category": node_2["category"] } } } } } if "id" in node_1: message["message"]["query_graph"]["nodes"]["n0"]["id"] = node_1[ "id"] if "id" in node_2: message["message"]["query_graph"]["nodes"]["n0"]["id"] = node_2[ "id"] # ensure the message is valid before returning it reasoner_validator.validate_Query(message) return message
import reasoner_validator nominal = { "message": { "query_graph": { "edges": { "e00": { "subject": "n00", "object": "n01", "type": "biolink:associated" } }, "nodes": { "n00": { "curie": "MONDO:0004981", "type": "biolink:Disease" }, "n01": { "type": "biolink:Gene" } } } } } reasoner_validator.validate_Query(nominal) x = 5