def run(): body = request.get_json() # Get credentials if given in request if "credentialId" not in body: raise OperationOutcome("credentialId is required to run fhirpipe.") credential_id = body.pop("credentialId") try: credentials = get_pyrog_client().get_credentials(credential_id) except OperationOutcome as e: raise OperationOutcome( f"Error while fetching credientials for DB: {e}.") # Merge body with default_params to get parameters to use params = {**default_params, **body} try: # Connect to DB and run fp_run(**params, credentials=credentials, pyrog_client=get_pyrog_client()) except Exception as e: # If something went wrong raise OperationOutcome(e) return jsonify(success=True)
def run_graphql_query(self, graphql_query, variables=None, auth_required=True): """ This function queries a GraphQL endpoint and returns a json parsed response. """ PYROG_URL = fhirpipe.global_config["graphql"]["server"] if not PYROG_URL: raise OperationOutcome("PYROG_URL is missing from environment") try: response = requests.post( PYROG_URL, headers=self.get_headers(auth_required), json={ "query": graphql_query, "variables": variables }, ) except requests.exceptions.ConnectionError: raise OperationOutcome("Could not connect to the Pyrog service") if response.status_code != 200: raise Exception("Graphql query failed with returning code " f"{response.status_code}\n{response.json()}.") body = response.json() if "errors" in body: raise Exception( f"GraphQL query failed with errors: {body['errors']}.") return body
def get_resource_from_id(self, resource_id): resp = self.run_graphql_query(resource_from_id_query, variables={"resourceId": resource_id}) resource = resp["data"]["resource"] if not resource: raise OperationOutcome( f"Resource with id {resource_id} does not exist") return resource
def get_credentials(self, credential_id): resp = self.run_graphql_query( credential_query, variables={"credentialId": credential_id}) credentials = resp["data"]["credential"] if not credentials: raise OperationOutcome( f"Database using credentials ID {credential_id} does not exist" ) return credentials
def preview(resource_id, primary_key_value): resource_mapping = get_pyrog_client().get_resource_from_id(resource_id) # Get credentials if given in request if not resource_mapping["source"]["credential"]: raise OperationOutcome("credentialId is required to run fhirpipe.") credentials = resource_mapping["source"]["credential"] try: # Connect to DB and run fhir_objects, errors = fp_preview(resource_id, [primary_key_value], credentials, pyrog_client=get_pyrog_client()) except Exception as e: # If something went wrong raise OperationOutcome(e) return jsonify({"preview": fhir_objects[0], "errors": errors})
def get_headers(self, auth_required=True): if auth_required and not self.token: raise OperationOutcome( "PyrogClient is not authenticated (login has probably failed, check your logs)" ) headers = {"content-type": "application/json"} if auth_required: headers["Authorization"] = f"Bearer {self.token}" return headers
def login(self): PYROG_LOGIN = fhirpipe.global_config["graphql"]["login"] PYROG_PASSWORD = fhirpipe.global_config["graphql"]["password"] if not PYROG_LOGIN or not PYROG_PASSWORD: raise OperationOutcome( "graphql.login and graphql.password must be set in fhirpipe config" ) resp = self.run_graphql_query( login_mutation, variables={ "email": PYROG_LOGIN, "password": PYROG_PASSWORD }, auth_required=False, ) data = resp["data"] if not data: raise OperationOutcome( f"Could not login to pyrog (email={PYROG_LOGIN}): {resp['errors'][0]['message']}" ) return data["login"]["token"]
def fetch(concept_map_id: str) -> T: api_url = fhirpipe.global_config["fhir-api"]["url"] try: response = requests.get(f"{api_url}/ConceptMap/{concept_map_id}") except requests.exceptions.ConnectionError as e: raise OperationOutcome( f"Could not connect to the fhir-api service: {e}") if response.status_code != 200: raise Exception( f"Error while fetching concept map {concept_map_id}: {response.text}." ) return response.json()