From 24466b2b92316391ae8efdf6a4d234ede9aba2b2 Mon Sep 17 00:00:00 2001 From: thiagotrabach Date: Tue, 1 Aug 2023 18:38:46 -0300 Subject: [PATCH 1/4] initial commit --- fhir_utils/healthcare_api.py | 44 ++++++++++++++++++++++++++++++++++++ requirements.txt | 19 ++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 fhir_utils/healthcare_api.py create mode 100644 requirements.txt diff --git a/fhir_utils/healthcare_api.py b/fhir_utils/healthcare_api.py new file mode 100644 index 0000000..a2bb4da --- /dev/null +++ b/fhir_utils/healthcare_api.py @@ -0,0 +1,44 @@ +from googleapiclient import discovery +import json + +class HealthcareApi: + def __init__(self, project_id, location, dataset_id, fhir_store_id): + self.api_version = "v1" + self.service_name = "healthcare" + self.header = "application/fhir+json;charset=utf-8" + self.fhir_store_parent = f"projects/{project_id}/locations/{location}/datasets/{dataset_id}" + self.fhir_store_name = f"{self.fhir_store_parent}/fhirStores/{fhir_store_id}" + self.client = discovery.build(self.service_name, self.api_version) + + + def create(self, resource: str, body: dict): + request = self.client.projects().locations().datasets().fhirStores().fhir().create(parent = self.fhir_store_name, + type = resource, + body = body) + request.headers["content-type"] = self.header + + response = request.execute() + print(f"Created Patient resource with ID {response['id']}") + + return response + + + def update(self, resource: str, resource_id: str, body: dict): + fhir_resource_path = f"{self.fhir_store_name}/fhir/{resource}/{resource_id}" + + request = self.client.projects().locations().datasets().fhirStores().fhir().update(name = fhir_resource_path, + body = body) + + request.headers["content-type"] = self.header + response = request.execute() + + print(f"Updated {resource} resource with ID {resource_id}:\n {json.dumps(response, indent=2)}") + + return response + + + def read(self): + return None + + def delete(self): + return None diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e1f1b55 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,19 @@ +cachetools==5.3.1 +certifi==2023.7.22 +charset-normalizer==3.2.0 +google-api-core==2.11.1 +google-api-python-client==2.95.0 +google-auth==2.22.0 +google-auth-httplib2==0.1.0 +googleapis-common-protos==1.60.0 +httplib2==0.22.0 +idna==3.4 +protobuf==4.23.4 +pyasn1==0.5.0 +pyasn1-modules==0.3.0 +pyparsing==3.1.1 +requests==2.31.0 +rsa==4.9 +six==1.16.0 +uritemplate==4.1.1 +urllib3==1.26.16 From 31f7c2f65d6d645778f4191b7159c9b899f9195b Mon Sep 17 00:00:00 2001 From: thiagotrabach Date: Tue, 1 Aug 2023 18:48:51 -0300 Subject: [PATCH 2/4] add read and delete methods --- fhir_utils/healthcare_api.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/fhir_utils/healthcare_api.py b/fhir_utils/healthcare_api.py index a2bb4da..70fcb9a 100644 --- a/fhir_utils/healthcare_api.py +++ b/fhir_utils/healthcare_api.py @@ -30,15 +30,30 @@ def update(self, resource: str, resource_id: str, body: dict): body = body) request.headers["content-type"] = self.header + response = request.execute() - print(f"Updated {resource} resource with ID {resource_id}:\n {json.dumps(response, indent=2)}") return response - def read(self): - return None + def read(self, resource: str, resource_id: str): + fhir_resource_path = f"{self.fhir_store_name}/fhir/{resource}/{resource_id}" + + request = self.client.projects().locations().datasets().fhirStores().fhir().read(name = fhir_resource_path) + + response = request.execute() + print(f"Got contents of {resource} resource with ID {resource_id}:\n", json.dumps(response, indent=2),) + + return response + - def delete(self): - return None + def delete(self, resource: str, resource_id: str): + fhir_resource_path = f"{self.fhir_store_name}/fhir/{resource}/{resource_id}" + + request = self.client.projects().locations().datasets().fhirStores().fhir().delete(name = fhir_resource_path) + + response = request.execute() + print(f"Deleted {resource} resource with ID {resource_id}.") + + return response From 7e481bb88dddb75e8ba61324b6c4d85b0b80d2c6 Mon Sep 17 00:00:00 2001 From: thiagotrabach Date: Wed, 2 Aug 2023 04:50:52 -0300 Subject: [PATCH 3/4] add xml and json importer --- fhir_utils/utils.py | 32 ++++++++++++++++++++++++++++++++ requirements.txt | 1 + 2 files changed, 33 insertions(+) create mode 100644 fhir_utils/utils.py diff --git a/fhir_utils/utils.py b/fhir_utils/utils.py new file mode 100644 index 0000000..de53398 --- /dev/null +++ b/fhir_utils/utils.py @@ -0,0 +1,32 @@ +import json +import xmltodict + +def json_to_dict(json_file_path): + try: + with open(json_file_path, 'r') as file: + data = json.load(file) + return data + except FileNotFoundError: + print(f"File '{json_file_path}' not found.") + except json.JSONDecodeError: + print(f"Unable to parse JSON file: '{json_file_path}'.") + except Exception as e: + print(f"An error occurred while importing JSON: {e}") + + +def xml_to_dict(xml_file_path): + try: + with open(xml_file_path, 'r') as file: + xml_data = file.read() + return xmltodict.parse(xml_data) + except FileNotFoundError: + print(f"Error: XML file not found at '{xml_file_path}'") + return None + except Exception as e: + print(f"Error: Unable to parse XML file. Reason: {str(e)}") + return None + + +def save_to_json(dict, json_path,): + with open(json_path, "w") as file: + json.dump(dict, file) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e1f1b55..8062816 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,3 +17,4 @@ rsa==4.9 six==1.16.0 uritemplate==4.1.1 urllib3==1.26.16 +xmltodict==0.13.0 From 8f452463fbf8c3252317ef85e5b005a1dbdff20a Mon Sep 17 00:00:00 2001 From: thiagotrabach Date: Wed, 2 Aug 2023 05:30:15 -0300 Subject: [PATCH 4/4] update init and requirements --- fhir_utils/__init__.py | 1 + requirements.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/fhir_utils/__init__.py b/fhir_utils/__init__.py index 45ba86e..f9e5126 100644 --- a/fhir_utils/__init__.py +++ b/fhir_utils/__init__.py @@ -1,3 +1,4 @@ # -*- coding: utf-8 -*- from .conf import settings # noqa from .version import __version__ # noqa +from healthcare_api import HealthcareApi \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 8062816..3331790 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ google-auth-httplib2==0.1.0 googleapis-common-protos==1.60.0 httplib2==0.22.0 idna==3.4 +loguru==0.7.0 protobuf==4.23.4 pyasn1==0.5.0 pyasn1-modules==0.3.0