Skip to content

Commit 88357f1

Browse files
committed
feat: working examples
1 parent 0fb2aeb commit 88357f1

File tree

7 files changed

+72
-53
lines changed

7 files changed

+72
-53
lines changed

sandbox/api/app.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def get_related_persons() -> Union[dict, tuple]:
6767

6868
try:
6969
# Check Headers
70-
if errors := check_for_errors(request):
70+
if errors := check_for_errors(request, "identifier"):
7171
return errors
7272

7373
identifier = remove_system(request.args.get("identifier"))
@@ -137,51 +137,42 @@ def get_consent() -> Union[dict, tuple]:
137137
Union[dict, tuple]: Response for GET /Consent
138138
"""
139139
try:
140+
# Check Headers
141+
if errors := check_for_errors(request, "performer:identifier"):
142+
return errors
143+
140144
performer_identifier = remove_system(request.args.get("performer:identifier"))
141145
status = request.args.get("status")
142146
_include = request.args.get("_include")
143147

144-
print(performer_identifier);
145-
146-
# Invalid status params
147-
if (status != "active" and status != None):
148-
return generate_response(load_json_file(CONSENT__STATUS_PARAM_INVALID), 400)
149-
150-
# Invalid include params
151-
if (
152-
_include != CONSENT_PERFORMER
153-
and _include != CONSENT_PATIENT
154-
and _include != f"{CONSENT_PATIENT},{CONSENT_PERFORMER}"
155-
and _include != f"{CONSENT_PERFORMER},{CONSENT_PATIENT}"
156-
and _include != None
157-
):
158-
return generate_response(load_json_file(BAD_REQUEST_INCLUDE_PARAM_INVALID), 400)
159-
160148
# Single consenting adult relationship
161149
if (performer_identifier == "9000000010"):
162-
check_for_consent_include_params(
150+
return check_for_consent_include_params(
163151
_include,
152+
logger,
164153
CONSENT__SINGLE_CONSENTING_ADULT_RELATIONSHIP,
165154
CONSENT__SINGLE_CONSENTING_ADULT_RELATIONSHIP_INCLUDE_BOTH,
166155
)
167156
# Single mother child relationship
168157
elif (performer_identifier == "9000000019"):
169-
check_for_consent_include_params(
158+
return check_for_consent_include_params(
170159
_include,
160+
logger,
171161
CONSENT__SINGLE_MOTHER_CHILD_RELATIONSHIP,
172-
CONSENT__SINGLE_MOTHER_CHILD_RELATIONSHIP_INCLUDE_BOTH,
162+
CONSENT__SINGLE_MOTHER_CHILD_RELATIONSHIP_INCLUDE_BOTH
173163
)
174164
# Filtering
175165
elif (performer_identifier == "9000000017"):
176-
check_for_consent_filtering_params(
166+
return check_for_consent_filtering_params(
177167
status,
178168
CONSENT__FILTERED_RELATIONSHIPS_STATUS_ACTIVE,
179169
CONSENT__FILTERED_RELATIONSHIPS_STATUS_INACTIVE,
180170
CONSENT__FILTERED_RELATIONSHIPS_STATUS_PROPOSED_ACTIVE
181171
)
182172
elif (performer_identifier == "9000000022"):
183-
check_for_consent_include_params(
173+
return check_for_consent_include_params(
184174
_include,
175+
logger,
185176
CONSENT__MULTIPLE_RELATIONSHIPS,
186177
CONSENT__MULTIPLE_RELATIONSHIPS_INCLUDE_BOTH,
187178
CONSENT__MULTIPLE_RELATIONSHIPS_INCLUDE_PATIENT,
@@ -191,7 +182,8 @@ def get_consent() -> Union[dict, tuple]:
191182
elif (performer_identifier == "9000000025"):
192183
return generate_response_from_example(CONSENT__NO_RELATIONSHIPS, 200)
193184
else:
194-
return generate_response(load_json_file(NOT_FOUND), 400)
185+
logger.error(f"Performer identifier {performer_identifier} not does not match examples")
186+
return generate_response_from_example(NOT_FOUND, 404)
195187

196188
except Exception as e:
197189
logger.error(e)

sandbox/api/constants.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
NOT_FOUND = "./api/responses/not_found.json"
21
EMPTY_RESPONSE = "./api/responses/GET_RelatedPerson/no_relationships.json"
32
LIST_RELATIONSHIP = (
43
"./api/responses/GET_RelatedPerson/list_relationship_9000000017.json"
@@ -35,7 +34,11 @@
3534

3635
# Common examples
3736
INTERNAL_SERVER_ERROR_EXAMPLE = "./api/examples/errors/internal-server-error.yaml"
38-
BAD_REQUEST_INCLUDE_PARAM_INVALID = "./api/examples/errors/bad_request_include_param_invalid.json"
37+
BAD_REQUEST_INCLUDE_PARAM_INVALID = "./api/examples/errors/invalid-include-parameter.yaml"
38+
NOT_FOUND = "./api/examples/errors/not-found.yaml"
39+
MISSING_IDENTIFIER = "./api/examples/errors/missing-identifier.yaml"
40+
INVALID_IDENTIFIER = "./api/examples/errors/invalid-identifier.yaml"
41+
3942
# Consent examples
4043
consent_dir = "./api/examples/GET_Consent/"
4144
CONSENT__FILTERED_RELATIONSHIPS_STATUS_ACTIVE = consent_dir + "filtered-relationships-status-active.yaml"
@@ -56,4 +59,4 @@
5659
CONSENT__SINGLE_MOTHER_CHILD_RELATIONSHIP_INCLUDE_BOTH = (
5760
consent_dir + "single-mother-child-relationship-include-performer-patient.yaml"
5861
)
59-
CONSENT__STATUS_PARAM_INVALID = consent_dir + "bad_request_status_param_invalid.json"
62+
CONSENT__STATUS_PARAM_INVALID = consent_dir + "/errors/invalid-status-parameter.yaml"

sandbox/api/utils.py

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1+
import logging
12
from json import dumps, load
23
from typing import Any, Optional
34

45
from flask import Response, Request
56
from yaml import CLoader as Loader
67
from yaml import load as yaml_load
8+
79
from .constants import (
810
EMPTY_RESPONSE,
911
PATIENT_IDENTIFIERS,
1012
NOT_FOUND,
1113
INCLUDE_FLAG,
1214
RELATED_IDENTIFIERS,
1315
CONSENT_PERFORMER,
14-
CONSENT_PATIENT, INTERNAL_SERVER_ERROR_EXAMPLE
16+
CONSENT_PATIENT, INTERNAL_SERVER_ERROR_EXAMPLE, CONSENT__STATUS_PARAM_INVALID, BAD_REQUEST_INCLUDE_PARAM_INVALID,
17+
MISSING_IDENTIFIER, INVALID_IDENTIFIER
1518
)
1619

17-
1820
FHIR_MIMETYPE = "application/fhir+json"
1921

2022

@@ -24,32 +26,36 @@ def load_json_file(file_name: str) -> dict:
2426
return load(file)
2527

2628

27-
def check_for_errors(request: Request) -> Optional[tuple]:
29+
def check_for_errors(request: Request, identifier_key : str) -> Optional[tuple]:
2830
"""Check for shared in the request headers and arguments
2931
3032
Args:
3133
request (Request): Flask request object
34+
identifier_key (str) : The key to get the identifier by (e.g. "identifier")
3235
3336
Returns:
3437
Optional[tuple]: Tuple with response and status code if error is found
3538
"""
36-
identifier = request.args.get("identifier")
37-
identifier_without_system = remove_system(request.args.get("identifier"))
39+
identifier = request.args.get(identifier_key)
40+
identifier_without_system = remove_system(request.args.get(identifier_key))
41+
42+
print(f"Identifier = {identifier}")
43+
print(f"Identifier_without_system = {identifier_without_system}")
3844

3945
if not identifier:
4046
return (
41-
load_json_file(
42-
"./api/responses/bad_request_identifier_missing.json"
43-
),
44-
400,
47+
generate_response_from_example(
48+
"./api/examples/errors/missing-identifier.yaml"
49+
,
50+
400)
4551
)
4652
elif identifier and len(identifier_without_system) != 10:
4753
# invalid identifier
4854
return (
49-
load_json_file(
50-
"./api/responses/bad_request_identifier_invalid.json"
51-
),
52-
400,
55+
generate_response_from_example(
56+
"./api/examples/errors/invalid-identifier.yaml"
57+
,
58+
400)
5359
)
5460
elif (
5561
isinstance(identifier, str)
@@ -58,10 +64,10 @@ def check_for_errors(request: Request) -> Optional[tuple]:
5864
):
5965
# invalid identifier system
6066
return (
61-
load_json_file(
62-
"./api/responses/bad_request_identifier_invalid_system.json"
63-
),
64-
400,
67+
generate_response_from_example(
68+
"./api/examples/errors/invalid-identifier-system.yaml"
69+
,
70+
400)
6571
)
6672

6773

@@ -77,10 +83,10 @@ def check_for_empty(identifier: str, patient_identifier: str) -> Response:
7783
"""
7884
if identifier and identifier not in PATIENT_IDENTIFIERS:
7985
# Request with identifier - but its not in a list of identifiers
80-
return generate_response(load_json_file(NOT_FOUND), 404)
86+
return generate_response_from_example(NOT_FOUND, 404)
8187
elif patient_identifier and (patient_identifier not in RELATED_IDENTIFIERS):
8288
# Request with patient:identifier - but its not in a list of identifiers
83-
return generate_response(load_json_file(NOT_FOUND), 404)
89+
return generate_response_from_example(NOT_FOUND, 404)
8490
elif identifier == "9000000033":
8591
# Request with identifier for empty record
8692
return generate_response(load_json_file(EMPTY_RESPONSE))
@@ -187,6 +193,7 @@ def generate_response_from_example(example_path: str, status_code: int) -> Respo
187193

188194
def check_for_consent_include_params(
189195
_include : str,
196+
logger: logging.Logger,
190197
include_none_response_yaml: str,
191198
include_both_response_yaml : str,
192199
include_patient_response_yaml: str = None,
@@ -196,6 +203,7 @@ def check_for_consent_include_params(
196203
197204
Args:
198205
_include (str): The include parameter supplied to the request
206+
logger (logging.Logger): Logger instance to use for error logging
199207
include_none_response_yaml (str): The file to return when include params are empty
200208
include_both_response_yaml (str): The file to return when include param matches with Consent:performer,Consent:patient
201209
include_patient_response_yaml (str): (optional) The file to return when include param matches with Consent:patient
@@ -204,14 +212,30 @@ def check_for_consent_include_params(
204212
Returns:
205213
response: Resultant Response object based on input.
206214
"""
207-
if (_include == CONSENT_PERFORMER):
208-
return generate_response_from_example(include_performer_response_yaml, 200)
215+
if (
216+
_include != CONSENT_PERFORMER
217+
and _include != CONSENT_PATIENT
218+
and _include != f"{CONSENT_PATIENT},{CONSENT_PERFORMER}"
219+
and _include != f"{CONSENT_PERFORMER},{CONSENT_PATIENT}"
220+
and _include != None
221+
):
222+
return generate_response_from_example(BAD_REQUEST_INCLUDE_PARAM_INVALID, 400)
223+
elif (_include == CONSENT_PERFORMER):
224+
if include_performer_response_yaml:
225+
return generate_response_from_example(include_performer_response_yaml, 200)
226+
else:
227+
logger.error("No consent performer example provided")
228+
return generate_response_from_example(INTERNAL_SERVER_ERROR_EXAMPLE, 500)
209229
elif (_include == CONSENT_PATIENT):
210-
return generate_response_from_example(include_patient_response_yaml, 200)
211-
elif (_include == f"{CONSENT_PATIENT},{CONSENT_PERFORMER}" or f"{CONSENT_PERFORMER},{CONSENT_PATIENT}"):
230+
if include_performer_response_yaml:
231+
return generate_response_from_example(include_patient_response_yaml, 200)
232+
else:
233+
logger.error("No consent:patient example provided")
234+
return generate_response_from_example(INTERNAL_SERVER_ERROR_EXAMPLE, 500)
235+
elif (_include == f"{CONSENT_PATIENT},{CONSENT_PERFORMER}" or _include == f"{CONSENT_PERFORMER},{CONSENT_PATIENT}"):
212236
return generate_response_from_example(include_both_response_yaml, 200)
213237
else:
214-
return generate_response(load_json_file(include_none_response_yaml), 200)
238+
return generate_response_from_example(include_none_response_yaml, 200)
215239

216240

217241
def check_for_consent_filtering_params(
@@ -232,10 +256,10 @@ def check_for_consent_filtering_params(
232256
response: Resultant Response object based on input.
233257
"""
234258
if (status == "active"):
235-
return generate_response_from_example( status_active_response_yaml, 200)
259+
return generate_response_from_example(status_active_response_yaml, 200)
236260
elif (status == "inactive"):
237261
return generate_response_from_example(status_inactive_response_yaml, 200)
238-
elif (status == "proposed,active" or status == "active,proposed"):
262+
elif (status == "proposed,inactive" or status == "inactive,proposed"):
239263
return generate_response_from_example(status_proposed_and_inactive_response_yaml, 200)
240264
else:
241-
return generate_response_from_example(INTERNAL_SERVER_ERROR_EXAMPLE, 500)
265+
return generate_response_from_example(CONSENT__STATUS_PARAM_INVALID, 400)

specification/examples/responses/GET_Consent/errors/invalid-identifier.yaml renamed to specification/examples/responses/errors/invalid-identifier.yaml

File renamed without changes.

specification/examples/responses/GET_Consent/errors/invalid-include-parameter.yaml renamed to specification/examples/responses/errors/invalid-include-parameter.yaml

File renamed without changes.

specification/examples/responses/GET_Consent/errors/missing-identifier.yaml renamed to specification/examples/responses/errors/missing-identifier.yaml

File renamed without changes.

specification/examples/responses/GET_Consent/errors/not-found.yaml renamed to specification/examples/responses/errors/not-found.yaml

File renamed without changes.

0 commit comments

Comments
 (0)