Skip to content

Commit 278bd52

Browse files
authored
Merge pull request #8 from beda-software/feat-uri-to-reference
feat: convert uri to reference for watch
2 parents 94e7a1d + ef87899 commit 278bd52

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

fhirsnake/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,20 @@ def replacer(match):
1818
return re.sub(r"\$\{(\w+)\}", replacer, obj)
1919
else:
2020
return obj
21+
22+
23+
def convert_uri_to_reference(uri: str) -> str:
24+
if uri.startswith("urn:uuid:"):
25+
split = uri.split(":")
26+
return f"{split[2]}/{split[3]}"
27+
return uri
28+
29+
30+
def replace_urn_uuid_with_reference(obj):
31+
if isinstance(obj, str):
32+
return convert_uri_to_reference(obj)
33+
elif isinstance(obj, dict):
34+
return {k: replace_urn_uuid_with_reference(v) for k, v in obj.items()}
35+
elif isinstance(obj, list):
36+
return [replace_urn_uuid_with_reference(item) for item in obj]
37+
return obj

fhirsnake/watch.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from converter import convert_questionnaire_fce_to_fhir
77
from files import load_resource
88
from initial_resources import RESOURCE_DIR
9+
from utils import replace_urn_uuid_with_reference
910
from watchdog.events import FileSystemEventHandler
1011
from watchdog.observers import Observer
1112

@@ -25,7 +26,9 @@ def __init__(
2526
self.target_dir = target_dir
2627
self.external_fhir_server_url = external_fhir_server_url
2728
self.external_fhir_server_headers = external_fhir_server_headers
28-
self.external_questionnaire_fce_fhir_converter_url = external_questionnaire_fce_fhir_converter_url
29+
self.external_questionnaire_fce_fhir_converter_url = (
30+
external_questionnaire_fce_fhir_converter_url
31+
)
2932
super().__init__(*args, **kwargs)
3033

3134
def on_modified(self, event):
@@ -43,7 +46,10 @@ def process_file(self, file_path):
4346
logging.error("Unable to load resource %s:\a\n%s", file_path, exc)
4447
return
4548

46-
if self.external_questionnaire_fce_fhir_converter_url:
49+
if (
50+
self.external_questionnaire_fce_fhir_converter_url
51+
and resource["resourceType"] == "Questionnaire"
52+
):
4753
try:
4854
resource = convert_questionnaire_fce_to_fhir(
4955
resource, self.external_questionnaire_fce_fhir_converter_url
@@ -59,9 +65,16 @@ def process_file(self, file_path):
5965
resource_id = resource["id"]
6066
url = f"{self.external_fhir_server_url}/{resource_type}/{resource_id}"
6167

68+
resource = replace_urn_uuid_with_reference(resource)
69+
6270
try:
6371
response = requests.put(
64-
url, json=resource, headers={"Content-Type": "application/json", **self.external_fhir_server_headers}
72+
url,
73+
json=resource,
74+
headers={
75+
"Content-Type": "application/json",
76+
**self.external_fhir_server_headers,
77+
},
6578
)
6679

6780
formatted_error = response.text
@@ -79,7 +92,9 @@ def process_file(self, file_path):
7992
formatted_error,
8093
)
8194
else:
82-
logging.info("Updated %s via %s (%s)", file_path, url, response.status_code)
95+
logging.info(
96+
"Updated %s via %s (%s)", file_path, url, response.status_code
97+
)
8398
except requests.RequestException:
8499
logging.exception("Failed to PUT %s via %s", file_path, url)
85100

0 commit comments

Comments
 (0)