11"""Generic utilities"""
22
33import datetime
4-
5- from typing import Literal , Union , Optional
4+ import json
5+ from typing import Literal , Union , Optional , Dict , Any
6+ from fhir .resources .R4B .bundle import (
7+ Bundle as FhirBundle ,
8+ BundleEntry ,
9+ BundleLink ,
10+ BundleEntrySearch ,
11+ )
12+ from fhir .resources .R4B .immunization import Immunization
613from models .constants import Constants
714import urllib .parse
815import base64
@@ -123,7 +130,6 @@ def create_diagnostics():
123130 exp_error = {"diagnostics" : diagnostics }
124131 return exp_error
125132
126-
127133def create_diagnostics_error (value ):
128134 if value == "Both" :
129135 diagnostics = (
@@ -134,46 +140,49 @@ def create_diagnostics_error(value):
134140 exp_error = {"diagnostics" : diagnostics }
135141 return exp_error
136142
137-
138- def form_json (response , _element , identifier , baseurl ):
139- self_url = f"{ baseurl } ?identifier={ identifier } " + (f"&_elements={ _element } " if _element else "" )
140- json = {
141- "resourceType" : "Bundle" ,
142- "type" : "searchset" ,
143- "link" : [
144- {"relation" : "self" , "url" : self_url }
145- ]
143+ def make_empty_bundle (self_url : str ) -> Dict [str , Any ]:
144+ return {
145+ "resourceType" : "Bundle" ,
146+ "type" : "searchset" ,
147+ "link" : [{"relation" : "self" , "url" : self_url }],
148+ "entry" : [],
149+ "total" : 0 ,
146150 }
151+
152+ def form_json (response , _elements , identifier , baseurl ):
153+ self_url = f"{ baseurl } ?identifier={ identifier } " + (f"&_elements={ _elements } " if _elements else "" )
154+
147155 if not response :
148- json ["entry" ] = []
149- json ["total" ] = 0
150- return json
156+ return make_empty_bundle (self_url )
151157
152- # Full Immunization payload to be returned if only the identifier parameter was provided
153- if identifier and not _element :
154- resource = response ["resource" ]
158+ meta = {"versionId" : response ["version" ]} if "version" in response else {}
155159
156- elif identifier and _element :
157- element = {e .strip ().lower () for e in _element .split ("," ) if e .strip ()}
160+ # Full Immunization payload to be returned if only the identifier parameter was provided and truncated when _elements is used
161+ if _elements :
162+ elements = {e .strip ().lower () for e in _elements .split ("," ) if e .strip ()}
158163 resource = {"resourceType" : "Immunization" }
164+ if "id" in elements : resource ["id" ] = response ["id" ]
165+ if "meta" in elements and meta : resource ["meta" ] = meta
159166
160- # Add 'id' if specified
161- if "id" in element :
162- resource ["id " ] = response [ "id" ]
167+ else :
168+ resource = response [ "resource" ]
169+ resource ["meta " ] = meta
163170
164- # Add 'meta' if specified
165- if "meta" in element :
166- resource [ "id" ] = response [ "id" ]
167- resource [ "meta" ] = { "versionId" : response [ "version" ]}
171+ entry = BundleEntry ( fullUrl = f" { baseurl } / { response [ 'id' ] } " ,
172+ resource = Immunization . construct ( ** resource ) if _elements else Immunization . parse_obj ( resource ),
173+ search = BundleEntrySearch . construct ( mode = "match" ) if not _elements else None ,
174+ )
168175
169- json ["entry" ] = [{
170- "fullUrl" : f"https://api.service.nhs.uk/immunisation-fhir-api/Immunization/{ response ['id' ]} " ,
171- "resource" : resource ,
172- }
173- ]
174- json ["total" ] = 1
175- return json
176+ fhir_bundle = FhirBundle (
177+ resourceType = "Bundle" , type = "searchset" ,
178+ link = [BundleLink (relation = "self" , url = self_url )],
179+ entry = [entry ],
180+ total = 1 )
176181
182+ # Reassigned total to ensure it appears last in the response to match expected output
183+ data = json .loads (fhir_bundle .json (by_alias = True ))
184+ data ["total" ] = data .pop ("total" )
185+ return data
177186
178187def check_keys_in_sources (event , not_required_keys ):
179188 # Decode and parse the body, assuming it is JSON and base64-encoded
0 commit comments