Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/healthcare api #1

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fhir_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from .conf import settings # noqa
from .version import __version__ # noqa
from healthcare_api import HealthcareApi
59 changes: 59 additions & 0 deletions fhir_utils/healthcare_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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, 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, 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
32 changes: 32 additions & 0 deletions fhir_utils/utils.py
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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
loguru==0.7.0
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
xmltodict==0.13.0
Loading