77from urllib .parse import parse_qs
88
99from aws_lambda_typing .events import APIGatewayProxyEventV1
10+ from fhir .resources .R4B .bundle import Bundle
1011from fhir .resources .R4B .identifier import Identifier
1112
1213from constants import MAX_RESPONSE_SIZE_BYTES
@@ -147,7 +148,7 @@ def _get_immunization_by_identifier(self, search_params: dict[str, list[str]], s
147148 identifier = Identifier .construct (system = identifier_components [0 ], value = identifier_components [1 ])
148149
149150 search_bundle = self .fhir_service .get_immunization_by_identifier (identifier , supplier_system , element )
150- prepared_search_bundle = self ._prepare_search_bundle (search_bundle . dict () )
151+ prepared_search_bundle = self ._prepare_search_bundle (search_bundle )
151152
152153 return create_response (200 , prepared_search_bundle )
153154
@@ -162,12 +163,11 @@ def _search_immunizations(self, search_params: dict[str, list[str]], supplier_sy
162163 validated_search_params .date_to ,
163164 validated_search_params .include ,
164165 )
165- search_bundle_dict : dict = search_bundle .dict ()
166166
167- if self ._has_too_many_search_results (search_bundle_dict ):
167+ if self ._has_too_many_search_results (search_bundle ):
168168 raise TooManyResultsError ("Search returned too many results. Please narrow down the search" )
169169
170- prepared_search_bundle = self ._prepare_search_bundle (search_bundle . dict () )
170+ prepared_search_bundle = self ._prepare_search_bundle (search_bundle )
171171
172172 return create_response (200 , prepared_search_bundle )
173173
@@ -176,15 +176,17 @@ def _is_valid_immunization_id(self, immunization_id: str) -> bool:
176176 return False if not re .match (self ._IMMUNIZATION_ID_PATTERN , immunization_id ) else True
177177
178178 @staticmethod
179- def _prepare_search_bundle (search_response : dict ) -> dict :
179+ def _prepare_search_bundle (search_response : Bundle ) -> dict :
180180 """Workaround for fhir.resources dict() or json() removing the empty "entry" list. Team also specified that
181181 total should be the final key in the object. Should investigate if this can be resolved with later version of
182182 the library."""
183- if "entry" not in search_response :
184- search_response ["entry" ] = []
183+ search_response_dict = json .loads (search_response .json ())
185184
186- search_response ["total" ] = search_response .pop ("total" )
187- return search_response
185+ if "entry" not in search_response_dict :
186+ search_response_dict ["entry" ] = []
187+
188+ search_response_dict ["total" ] = search_response_dict .pop ("total" )
189+ return search_response_dict
188190
189191 @staticmethod
190192 def _is_valid_resource_version (resource_version : str ) -> bool :
@@ -199,11 +201,11 @@ def _is_identifier_search(search_params: dict[str, list[str]]) -> bool:
199201 )
200202
201203 @staticmethod
202- def _has_too_many_search_results (search_response : dict ) -> bool :
204+ def _has_too_many_search_results (search_response : Bundle ) -> bool :
203205 """Checks whether the response is too large - 6MB Lambda limit. Note: this condition should never happen as it
204206 would require a very large number of vaccs for a single patient. It is also very rudimentary and all it does is
205207 ensure we can raise and return a nice looking error. Consider using pagination as a more robust approach."""
206- return len (json . dumps ( search_response )) > MAX_RESPONSE_SIZE_BYTES
208+ return len (search_response . json ( use_decimal = True )) > MAX_RESPONSE_SIZE_BYTES
207209
208210 @staticmethod
209211 def _get_search_params_from_request (
0 commit comments