1- from json import load , dumps
2- from typing import Optional , Any
3-
4- from flask import request , Response
5-
6- NOT_FOUND = "./api/responses/not_found.json"
7- EMPTY_RESPONSE = "./api/responses/GET_RelatedPerson/empty_response_9000000033.json"
8- LIST_RELATIONSHIP = (
9- "./api/responses/GET_RelatedPerson/list_relationship_9000000017.json"
10- )
11- LIST_RELATIONSHIP_INCLUDE = (
12- "./api/responses/GET_RelatedPerson/list_relationship_include_9000000017.json"
13- )
14- VALIDATE_RELATIONSHIP_009 = (
15- "./api/responses/GET_RelatedPerson/verify_relationship_9000000009.json"
16- )
17- VALIDATE_RELATIONSHIP_INCLUDE_009 = (
18- "./api/responses/GET_RelatedPerson/verify_relationship_include_9000000009.json"
19- )
20- VALIDATE_RELATIONSHIP_025 = (
21- "./api/responses/GET_RelatedPerson/verify_relationship_9000000025.json"
1+ from json import dumps , load
2+ from typing import Any , Optional
3+
4+ from flask import Response , Request
5+ from yaml import load as yaml_load
6+ from .constants import (
7+ EMPTY_RESPONSE ,
8+ PATIENT_IDENTIFIERS ,
9+ NOT_FOUND ,
10+ INCLUDE_FLAG ,
11+ RELATED_IDENTIFIERS ,
2212)
23- VALIDATE_RELATIONSHIP_INCLUDE_025 = (
24- "./api/responses/GET_RelatedPerson/verify_relationship_include_9000000025.json"
25- )
26- ERROR_RESPONSE = "./api/responses/internal_server_error.json"
27- INCLUDE_FLAG = "RelatedPerson:patient"
2813
29- QUESTIONNAIRE_RESPONSE_SUCCESS = (
30- "./api/responses/POST_QuestionnaireResponse/questionnaire_response_success.json"
31- )
3214
33- PATIENT_IDENTIFIERS = ["9000000017" , "9000000033" ]
34- RELATED_IDENTIFIERS = ["9000000009" , "9000000025" ]
15+ FHIR_MIMETYPE = "application/fhir+json"
3516
3617
3718def load_json_file (file_name : str ) -> dict :
@@ -40,11 +21,11 @@ def load_json_file(file_name: str) -> dict:
4021 return load (file )
4122
4223
43- def check_for_errors (request : request ) -> Optional [tuple ]:
24+ def check_for_errors (request : Request ) -> Optional [tuple ]:
4425 """Check for errors in the request headers and arguments
4526
4627 Args:
47- request (request ): Flask request object
28+ request (Request ): Flask request object
4829
4930 Returns:
5031 Optional[tuple]: Tuple with response and status code if error is found
@@ -164,13 +145,36 @@ def generate_response(content: str, status: int = 200):
164145 Returns:
165146 Response: Resultant Response object based on input.
166147 """
167- return Response (dumps (content ), status = status , mimetype = "application/fhir+json" )
148+ return Response (dumps (content ), status = status , mimetype = FHIR_MIMETYPE )
168149
169150
170151def remove_system (identifier : Any ) -> str :
152+ """Removes the system from an identifier if it exists
153+
154+ Args:
155+ identifier (Any): Identifier to remove system from
156+
157+ Returns:
158+ str: Identifier without system
159+ """
171160 if isinstance (identifier , str ):
172161 if "|" in identifier :
173162 # Identifier includes system
174163 return identifier .split ("|" , maxsplit = 1 )[1 ]
175164 return identifier
176165 return ""
166+
167+
168+ def generate_response_from_example (example_path : str , status_code : int ) -> dict :
169+ """Converts an example file (yaml) to a response
170+
171+ Args:
172+ example_path (str): Path to the example file
173+ status_code (int): Status code for the response
174+
175+ Returns:
176+ Response: Resultant Response object based on input.
177+ """
178+ with open (example_path , "r" ) as file :
179+ content = yaml_load (file )
180+ return Response (dumps (content ), status = status_code , mimetype = FHIR_MIMETYPE )
0 commit comments