1111from models .errors import Severity , Code , create_operation_outcome
1212from constants import GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE
1313from log_structure import function_info
14- import base64
15- import urllib .parse
14+ from search_parameter_validator import (
15+ is_immunization_by_identifier ,
16+ get_parsed_body
17+ )
1618
1719logging .basicConfig (level = "INFO" )
1820logger = logging .getLogger ()
@@ -24,33 +26,13 @@ def search_imms_handler(event: events.APIGatewayProxyEventV1, _context: context_
2426
2527def search_imms (event : events .APIGatewayProxyEventV1 , controller : FhirController ):
2628 try :
29+
2730 query_params = event .get ("queryStringParameters" , {})
2831 body = event .get ("body" )
29- body_has_immunization_identifier = False
30- query_string_has_immunization_identifier = False
31- query_string_has_element = False
32- body_has_immunization_element = False
33- if not (query_params == None and body == None ):
34- if query_params :
35- query_string_has_immunization_identifier = "identifier" in event .get (
36- "queryStringParameters" , {}
37- )
38- query_string_has_element = "_elements" in event .get ("queryStringParameters" , {})
39- # Decode body from base64
40- if body :
41- decoded_body = base64 .b64decode (body ).decode ("utf-8" )
42- # Parse the URL encoded body
43- parsed_body = urllib .parse .parse_qs (decoded_body )
44-
45- # Check for 'identifier' in body
46- body_has_immunization_identifier = "identifier" in parsed_body
47- body_has_immunization_element = "_elements" in parsed_body
48- if (
49- query_string_has_immunization_identifier
50- or body_has_immunization_identifier
51- or query_string_has_element
52- or body_has_immunization_element
53- ):
32+ has_body = body is not None
33+ has_query_params = query_params is not None and query_params != {}
34+ if has_query_params or has_body :
35+ if is_immunization_by_identifier (query_params , get_parsed_body (body )):
5436 return controller .get_immunization_by_identifier (event )
5537 response = controller .search_immunizations (event )
5638
@@ -66,6 +48,16 @@ def search_imms(event: events.APIGatewayProxyEventV1, controller: FhirController
6648 )
6749 return FhirController .create_response (400 , exp_error )
6850 return response
51+
52+ except ValueError as ve :
53+ logger .exception ("ValueError occurred" )
54+ exp_error = create_operation_outcome (
55+ resource_id = str (uuid .uuid4 ()),
56+ severity = Severity .error ,
57+ code = Code .invalid ,
58+ diagnostics = str (ve )
59+ )
60+ return FhirController .create_response (400 , exp_error )
6961 except Exception : # pylint: disable = broad-exception-caught
7062 logger .exception ("Unhandled exception" )
7163 exp_error = create_operation_outcome (
0 commit comments