@@ -160,7 +160,7 @@ def _get_immunization_by_identifier(self, search_params: dict[str, list[str]], s
160160 identifier = Identifier .construct (system = identifier_components [0 ], value = identifier_components [1 ])
161161
162162 search_bundle = self .fhir_service .get_immunization_by_identifier (identifier , supplier_system , element )
163- prepared_search_bundle = self ._prepare_search_bundle (search_bundle )
163+ prepared_search_bundle = self ._prepare_search_bundle (search_bundle . json () )
164164
165165 return create_response (200 , prepared_search_bundle )
166166
@@ -177,12 +177,7 @@ def _search_immunizations(self, search_params: dict[str, list[str]], supplier_sy
177177 result .invalid_immunization_targets ,
178178 )
179179
180- if self ._has_too_many_search_results (search_bundle ):
181- raise TooManyResultsError ("Search returned too many results. Please narrow down the search" )
182-
183- prepared_search_bundle = self ._prepare_search_bundle (search_bundle )
184-
185- return create_response (200 , prepared_search_bundle )
180+ return self ._create_search_response (search_bundle )
186181
187182 def _search_immunizations_by_target_disease (self , search_params : dict [str , list [str ]], supplier_system : str ) -> dict :
188183 result = validate_and_retrieve_search_params_by_disease (search_params )
@@ -208,7 +203,15 @@ def _search_immunizations_by_target_disease(self, search_params: dict[str, list[
208203 invalid_target_diseases = result .invalid_target_diseases ,
209204 )
210205
211- prepared_search_bundle = self ._prepare_search_bundle (search_bundle )
206+ return self ._create_search_response (search_bundle )
207+
208+ def _create_search_response (self , search_bundle : Bundle ) -> dict :
209+ search_response_json = search_bundle .json (use_decimal = True )
210+
211+ if len (search_response_json ) > MAX_RESPONSE_SIZE_BYTES :
212+ raise TooManyResultsError ("Search returned too many results. Please narrow down the search" )
213+
214+ prepared_search_bundle = self ._prepare_search_bundle (search_response_json )
212215 return create_response (200 , prepared_search_bundle )
213216
214217 @staticmethod
@@ -220,11 +223,11 @@ def _is_valid_immunization_id(self, immunization_id: str) -> bool:
220223 return False if not re .match (self ._IMMUNIZATION_ID_PATTERN , immunization_id ) else True
221224
222225 @staticmethod
223- def _prepare_search_bundle (search_response : Bundle ) -> dict :
226+ def _prepare_search_bundle (search_response_json : str ) -> dict :
224227 """Workaround for fhir.resources dict() or json() removing the empty "entry" list. Team also specified that
225228 total should be the final key in the object. Should investigate if this can be resolved with later version of
226229 the library."""
227- search_response_dict = json .loads (search_response . json () )
230+ search_response_dict = json .loads (search_response_json )
228231
229232 if "entry" not in search_response_dict :
230233 search_response_dict ["entry" ] = []
@@ -244,13 +247,6 @@ def _is_identifier_search(search_params: dict[str, list[str]]) -> bool:
244247 or IdentifierSearchParameterName .ELEMENTS in search_params
245248 )
246249
247- @staticmethod
248- def _has_too_many_search_results (search_response : Bundle ) -> bool :
249- """Checks whether the response is too large - 6MB Lambda limit. Note: this condition should never happen as it
250- would require a very large number of vaccs for a single patient. It is also very rudimentary and all it does is
251- ensure we can raise and return a nice looking error. Consider using pagination as a more robust approach."""
252- return len (search_response .json (use_decimal = True )) > MAX_RESPONSE_SIZE_BYTES
253-
254250 @staticmethod
255251 def _get_search_params_from_request (
256252 aws_event : APIGatewayProxyEventV1 , is_post_endpoint_req : bool
0 commit comments