1+ import logging
12from json import dumps , load
23from typing import Any , Optional
34
45from flask import Response , Request
56from yaml import CLoader as Loader
67from yaml import load as yaml_load
8+
79from .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-
1820FHIR_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
188194def 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
217241def 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 )
0 commit comments