11from json import load , dumps
2- from typing import Optional
2+ from typing import Optional , Any
33
44from flask import request , Response
55
@@ -49,17 +49,33 @@ def check_for_errors(request: request) -> Optional[tuple]:
4949 Returns:
5050 Optional[tuple]: Tuple with response and status code if error is found
5151 """
52- if not request .args .get ("identifier" ):
52+ identifier = request .args .get ("identifier" )
53+ identifier_without_system = remove_system (request .args .get ("identifier" ))
54+
55+ if not identifier :
5356 return (
5457 load_json_file (
5558 "./api/responses/GET_RelatedPerson/bad_request_identifier_missing.json"
5659 ),
5760 400 ,
5861 )
59- elif request .args .get ("identifier" ) and len (request .args .get ("identifier" )) != 10 :
62+ elif identifier and len (identifier_without_system ) != 10 :
63+ # invalid identifier
64+ return (
65+ load_json_file (
66+ "./api/responses/GET_RelatedPerson/bad_request_identifier_invalid.json"
67+ ),
68+ 400 ,
69+ )
70+ elif (
71+ isinstance (identifier , str )
72+ and "|" in identifier
73+ and "https://fhir.nhs.uk/Id/nhs-number" != identifier .split ("|" , maxsplit = 2 )[0 ]
74+ ):
75+ # invalid identifier system
6076 return (
6177 load_json_file (
62- "./api/responses/GET_RelatedPerson/bad_request_identifier_not_as_expected .json"
78+ "./api/responses/GET_RelatedPerson/bad_request_identifier_invalid_system .json"
6379 ),
6480 400 ,
6581 )
@@ -91,8 +107,8 @@ def check_for_validate(
91107 identifier : str ,
92108 patient_identifier : str ,
93109 include : str ,
94- basefile : str ,
95- incfile : str ,
110+ base_file : str ,
111+ inc_file : str ,
96112) -> Response :
97113 """Checks for validate request responses for a given relationship record
98114
@@ -101,41 +117,41 @@ def check_for_validate(
101117 identifier (str): The identifier supplied to the request
102118 patient_identifier (str): The patient:identifier supplied to the request
103119 include (str): The include parameter supplied to the request
104- basefile (str): The file to return when record matches but does not request includeded data
105- incfile (str): The file to return when record matches and does request included data
120+ base_file (str): The file to return when record matches but does not request included data
121+ inc_file (str): The file to return when record matches and does request included data
106122
107123 Returns:
108124 Response: Resultant response or None
109125 """
110126 if identifier and patient_identifier == value and include == INCLUDE_FLAG :
111127 # Request with identifier, patient and _include=patient
112- return generate_response (load_json_file (incfile ))
128+ return generate_response (load_json_file (inc_file ))
113129 elif identifier and patient_identifier == value :
114130 # Request with identifier and patient
115- return generate_response (load_json_file (basefile ))
131+ return generate_response (load_json_file (base_file ))
116132
117133
118134def check_for_list (
119- value : str , identifier : str , include : str , basefile : str , incfile : str
135+ value : str , identifier : str , include : str , base_file : str , inc_file : str
120136) -> Response :
121137 """Check for a list relationship response for a given NHS number
122138
123139 Args:
124140 value (str): NHS number of the relationship to look for
125141 identifier (str): The identifier supplied to the request
126142 include (str): The include parameter supplied to the request
127- basefile (str): The file to return when record matches but does not request includeded data
128- incfile (str): The file to return when record matches and does request included data
143+ base_file (str): The file to return when record matches but does not request included data
144+ inc_file (str): The file to return when record matches and does request included data
129145
130146 Returns:
131147 Response: Resultant response or None
132148 """
133149 if identifier == value and include == INCLUDE_FLAG :
134150 # Request with identifier and _include=patient
135- return generate_response (load_json_file (incfile ))
151+ return generate_response (load_json_file (inc_file ))
136152 elif identifier :
137153 # Request with identifier
138- return generate_response (load_json_file (basefile ))
154+ return generate_response (load_json_file (base_file ))
139155
140156
141157def generate_response (content : str , status : int = 200 ):
@@ -149,3 +165,12 @@ def generate_response(content: str, status: int = 200):
149165 Response: Resultant Response object based on input.
150166 """
151167 return Response (dumps (content ), status = status , mimetype = "application/fhir+json" )
168+
169+
170+ def remove_system (identifier : Any ) -> str :
171+ if isinstance (identifier , str ):
172+ if "|" in identifier :
173+ # Identifier includes system
174+ return identifier .split ("|" , maxsplit = 1 )[1 ]
175+ return identifier
176+ return ""
0 commit comments