|
5 | 5 | import urllib.parse |
6 | 6 | import uuid |
7 | 7 | from decimal import Decimal |
| 8 | +from json import JSONDecodeError |
8 | 9 | from typing import Optional |
9 | 10 |
|
10 | 11 | from aws_lambda_typing.events import APIGatewayProxyEventV1 |
|
20 | 21 | Code, |
21 | 22 | IdentifierDuplicationError, |
22 | 23 | InvalidImmunizationId, |
| 24 | + InvalidJsonError, |
23 | 25 | ParameterException, |
24 | 26 | Severity, |
25 | 27 | UnauthorizedError, |
@@ -48,7 +50,8 @@ def make_controller( |
48 | 50 |
|
49 | 51 |
|
50 | 52 | class FhirController: |
51 | | - immunization_id_pattern = r"^[A-Za-z0-9\-.]{1,64}$" |
| 53 | + _IMMUNIZATION_ID_PATTERN = r"^[A-Za-z0-9\-.]{1,64}$" |
| 54 | + _API_SERVICE_URL = get_service_url() |
52 | 55 |
|
53 | 56 | def __init__( |
54 | 57 | self, |
@@ -105,46 +108,22 @@ def get_immunization_by_id(self, aws_event: APIGatewayProxyEventV1) -> dict: |
105 | 108 |
|
106 | 109 | return create_response(200, resource.json(), {E_TAG_HEADER_NAME: version}) |
107 | 110 |
|
108 | | - def create_immunization(self, aws_event): |
109 | | - if not aws_event.get("headers"): |
110 | | - return create_response( |
111 | | - 403, |
112 | | - create_operation_outcome( |
113 | | - resource_id=str(uuid.uuid4()), |
114 | | - severity=Severity.error, |
115 | | - code=Code.forbidden, |
116 | | - diagnostics="Unauthorized request", |
117 | | - ), |
118 | | - ) |
119 | | - |
120 | | - supplier_system = self._identify_supplier_system(aws_event) |
| 111 | + @fhir_api_exception_handler |
| 112 | + def create_immunization(self, aws_event: APIGatewayProxyEventV1) -> dict: |
| 113 | + supplier_system = get_supplier_system_header(aws_event) |
121 | 114 |
|
122 | 115 | try: |
123 | | - immunisation = json.loads(aws_event["body"], parse_float=Decimal) |
124 | | - except json.decoder.JSONDecodeError as e: |
125 | | - return self._create_bad_request(f"Request's body contains malformed JSON: {e}") |
126 | | - try: |
127 | | - resource = self.fhir_service.create_immunization(immunisation, supplier_system) |
128 | | - if "diagnostics" in resource: |
129 | | - exp_error = create_operation_outcome( |
130 | | - resource_id=str(uuid.uuid4()), |
131 | | - severity=Severity.error, |
132 | | - code=Code.invariant, |
133 | | - diagnostics=resource["diagnostics"], |
134 | | - ) |
135 | | - return create_response(400, json.dumps(exp_error)) |
136 | | - else: |
137 | | - location = f"{get_service_url()}/Immunization/{resource.id}" |
138 | | - version = "1" |
139 | | - return create_response(201, None, {"Location": location, "E-Tag": version}) |
140 | | - except ValidationError as error: |
141 | | - return create_response(400, error.to_operation_outcome()) |
142 | | - except IdentifierDuplicationError as duplicate: |
143 | | - return create_response(422, duplicate.to_operation_outcome()) |
144 | | - except UnhandledResponseError as unhandled_error: |
145 | | - return create_response(500, unhandled_error.to_operation_outcome()) |
146 | | - except UnauthorizedVaxError as unauthorized: |
147 | | - return create_response(403, unauthorized.to_operation_outcome()) |
| 116 | + immunisation: dict = json.loads(aws_event["body"], parse_float=Decimal) |
| 117 | + except JSONDecodeError as e: |
| 118 | + raise InvalidJsonError(message=str(f"Request's body contains malformed JSON: {e}")) |
| 119 | + |
| 120 | + created_resource_id = self.fhir_service.create_immunization(immunisation, supplier_system) |
| 121 | + |
| 122 | + return create_response( |
| 123 | + status_code=201, |
| 124 | + body=None, |
| 125 | + headers={"Location": f"{self._API_SERVICE_URL}/Immunization/{created_resource_id}", "E-Tag": "1"}, |
| 126 | + ) |
148 | 127 |
|
149 | 128 | def update_immunization(self, aws_event): |
150 | 129 | try: |
@@ -188,7 +167,7 @@ def update_immunization(self, aws_event): |
188 | 167 | ) |
189 | 168 | return create_response(400, json.dumps(exp_error)) |
190 | 169 | # Validate the imms id in the path params and body of request - end |
191 | | - except json.decoder.JSONDecodeError as e: |
| 170 | + except JSONDecodeError as e: |
192 | 171 | return self._create_bad_request(f"Request's body contains malformed JSON: {e}") |
193 | 172 | except Exception as e: |
194 | 173 | return self._create_bad_request(f"Request's body contains string: {e}") |
@@ -405,7 +384,7 @@ def search_immunizations(self, aws_event: APIGatewayProxyEventV1) -> dict: |
405 | 384 |
|
406 | 385 | def _is_valid_immunization_id(self, immunization_id: str) -> bool: |
407 | 386 | """Validates if the given unique Immunization ID is valid.""" |
408 | | - return False if not re.match(self.immunization_id_pattern, immunization_id) else True |
| 387 | + return False if not re.match(self._IMMUNIZATION_ID_PATTERN, immunization_id) else True |
409 | 388 |
|
410 | 389 | def _validate_identifier_system(self, _id: str, _elements: str) -> Optional[dict]: |
411 | 390 | if not _id: |
|
0 commit comments