Skip to content

Commit c1f54bd

Browse files
test logging for APIM status endpoint
1 parent 0f8d1aa commit c1f54bd

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

pathology-api/lambda_handler.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -112,34 +112,43 @@ def status() -> Response[str]:
112112

113113
@app.post("/FHIR/R4/Bundle")
114114
def post_result() -> Response[str]:
115-
correlation_id = app.current_event.headers.get(_CORRELATION_ID_HEADER)
115+
_logger.debug("Post result endpoint called.")
116116

117-
if not correlation_id:
118-
raise ValueError(f"Missing required header: {_CORRELATION_ID_HEADER}")
119-
with set_correlation_id(correlation_id):
120-
_logger.debug("Post result endpoint called.")
121-
122-
try:
123-
payload = app.current_event.json_body
124-
except JSONDecodeError as e:
125-
raise ValidationError("Invalid payload provided.") from e
117+
try:
118+
payload = app.current_event.json_body
119+
except JSONDecodeError as e:
120+
raise ValidationError("Invalid payload provided.") from e
126121

127-
_logger.debug("Payload received: %s", payload)
122+
_logger.debug("Payload received: %s", payload)
128123

129-
if payload is None:
130-
raise ValidationError(
131-
"Resources must be provided as a bundle of type 'document'"
132-
)
124+
if payload is None:
125+
raise ValidationError(
126+
"Resources must be provided as a bundle of type 'document'"
127+
)
133128

134-
bundle = Bundle.model_validate(payload, by_alias=True)
129+
bundle = Bundle.model_validate(payload, by_alias=True)
135130

136-
response = handle_request(bundle)
131+
response = handle_request(bundle)
137132

138-
return _with_default_headers(
139-
status_code=200,
140-
body=response,
141-
)
133+
return _with_default_headers(
134+
status_code=200,
135+
body=response,
136+
)
142137

143138

144139
def handler(data: dict[str, Any], context: LambdaContext) -> dict[str, Any]:
145-
return app.resolve(data, context)
140+
headers = data.get("headers", {}) or {}
141+
correlation_id = headers.get(_CORRELATION_ID_HEADER)
142+
143+
if not correlation_id:
144+
return {
145+
"statusCode": 500,
146+
"headers": {"Content-Type": "application/fhir+json"},
147+
"body": OperationOutcome.create_server_error(
148+
f"Missing required header: {_CORRELATION_ID_HEADER}"
149+
).model_dump_json(by_alias=True, exclude_none=True),
150+
}
151+
with set_correlation_id(correlation_id):
152+
response = app.resolve(data, context)
153+
response.setdefault("headers", {})[_CORRELATION_ID_HEADER] = correlation_id
154+
return response

0 commit comments

Comments
 (0)