|
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 |
|
19 | 20 | from models.errors import ( |
20 | 21 | Code, |
21 | 22 | IdentifierDuplicationError, |
| 23 | + InvalidJsonError, |
22 | 24 | ParameterException, |
23 | 25 | ResourceNotFoundError, |
24 | 26 | Severity, |
@@ -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: |
@@ -178,7 +157,7 @@ def update_immunization(self, aws_event): |
178 | 157 | ) |
179 | 158 | return create_response(400, json.dumps(exp_error)) |
180 | 159 | # Validate the imms id in the path params and body of request - end |
181 | | - except json.decoder.JSONDecodeError as e: |
| 160 | + except JSONDecodeError as e: |
182 | 161 | return self._create_bad_request(f"Request's body contains malformed JSON: {e}") |
183 | 162 | except Exception as e: |
184 | 163 | return self._create_bad_request(f"Request's body contains string: {e}") |
@@ -407,7 +386,7 @@ def search_immunizations(self, aws_event: APIGatewayProxyEventV1) -> dict: |
407 | 386 | return create_response(200, json.dumps(result_json_dict)) |
408 | 387 |
|
409 | 388 | def _validate_id(self, _id: str) -> Optional[dict]: |
410 | | - if not re.match(self.immunization_id_pattern, _id): |
| 389 | + if not re.match(self._IMMUNIZATION_ID_PATTERN, _id): |
411 | 390 | msg = "Validation errors: the provided event ID is either missing or not in the expected format." |
412 | 391 | return create_operation_outcome( |
413 | 392 | resource_id=str(uuid.uuid4()), |
|
0 commit comments