-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
235 lines (214 loc) · 9.71 KB
/
app.py
File metadata and controls
235 lines (214 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
Proxy server for Stoplight Prism with response example selection logic.
Adapted from https://stackoverflow.com/a/36601467
"""
import logging
import os
import sys
from http import HTTPStatus
import requests # pyright: ignore [reportMissingModuleSource]
from flask import Flask, Request, Response, make_response, request # pyright: ignore [reportMissingImports]
# Configure logging to output to stdout
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
# HTTP proxy to Prism
UPSTREAM_HOST = os.environ.get("UPSTREAM_HOST")
if not UPSTREAM_HOST:
NO_UPSTREAM_HOST = "UPSTREAM_HOST environment variable not set"
raise ValueError(NO_UPSTREAM_HOST)
app = Flask(__name__)
app.logger.setLevel("INFO")
session = requests.Session()
HOP_BY_HOP_HEADERS = [
"connection",
"content-encoding",
"content-length",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailers",
"transfer-encoding",
"upgrade",
]
PATIENT_EXAMPLES = {
"patient-check/5000000001": "example_5000000001",
"patient-check/5000000002": "example_5000000002",
"patient-check/5000000003": "example_5000000003",
"patient-check/5000000004": "example_5000000004",
"patient-check/5000000005": "example_5000000005",
"patient-check/5000000006": "example_5000000006",
"patient-check/5000000007": "example_5000000007",
"patient-check/5000000008": "example_5000000008",
"patient-check/5000000009": "example_5000000009",
"patient-check/5000000010": "example_5000000010",
"patient-check/5000000011": "example_5000000011",
"patient-check/5000000012": "example_5000000012",
"patient-check/5000000013": "example_5000000013",
"patient-check/5000000014": "example_5000000014",
"patient-check/5000000015": "example_5000000015",
"patient-check/5000000016": "example_5000000016",
"patient-check/5000000017": "example_5000000017",
"patient-check/5000000018": "example_5000000018",
"patient-check/5000000019": "example_5000000019",
"patient-check/5000000020": "example_5000000020",
"patient-check/5000000021": "example_5000000021",
"patient-check/5000000022": "example_5000000022",
"patient-check/5000000023": "example_5000000023",
"patient-check/5000000024": "example_5000000024",
"patient-check/5000000101": "example_5000000101",
"patient-check/5000000102": "example_5000000102",
"patient-check/5000000103": "example_5000000103",
"patient-check/5000000104": "example_5000000104",
"patient-check/5000000105": "example_5000000105",
"patient-check/5000000106": "example_5000000106",
"patient-check/5000000107": "example_5000000107",
"patient-check/5000000108": "example_5000000108",
"patient-check/5000000110": "example_5000000110",
"patient-check/5000000111": "example_5000000111",
"patient-check/5000000114": "example_5000000114",
# Incorrectly sized mock NHS Numbers (retained for backward compatabliity)
"patient-check/50000000001": "example_5000000001",
"patient-check/50000000002": "example_5000000002",
"patient-check/50000000003": "example_5000000003",
"patient-check/50000000004": "example_5000000004",
"patient-check/50000000005": "example_5000000005",
"patient-check/50000000006": "example_5000000006",
"patient-check/50000000007": "example_5000000007",
"patient-check/50000000008": "example_5000000008",
"patient-check/50000000009": "example_5000000009",
"patient-check/50000000010": "example_5000000010",
"patient-check/50000000011": "example_5000000011",
"patient-check/50000000012": "example_5000000012",
"patient-check/50000000013": "example_5000000013",
"patient-check/50000000014": "example_5000000014",
"patient-check/50000000015": "example_5000000015",
"patient-check/50000000016": "example_5000000016",
"patient-check/50000000017": "example_5000000017",
"patient-check/50000000018": "example_5000000018",
"patient-check/50000000019": "example_5000000019",
"patient-check/50000000020": "example_5000000020",
"patient-check/50000000021": "example_5000000021",
"patient-check/50000000022": "example_5000000022",
"patient-check/50000000023": "example_5000000023",
"patient-check/50000000024": "example_5000000024",
"patient-check/50000000101": "example_5000000101",
"patient-check/50000000102": "example_5000000102",
"patient-check/50000000103": "example_5000000103",
"patient-check/50000000104": "example_5000000104",
"patient-check/50000000105": "example_5000000105",
"patient-check/50000000106": "example_5000000106",
"patient-check/50000000107": "example_5000000107",
"patient-check/50000000108": "example_5000000108",
"patient-check/50000000110": "example_5000000110",
"patient-check/50000000111": "example_5000000111",
"patient-check/50000000114": "example_5000000114",
# Support error scenario invocation
"patient-check/90000000400": "code400",
"patient-check/90000000404": "code404",
"patient-check/90000000422": "code422",
"patient-check/90000000500": "code500",
# VitA Specific NHS Number Mapping
"patient-check/9686368973": "example_5000000001",
"patient-check/9735548852": "example_5000000001",
"patient-check/9686368906": "example_5000000002",
"patient-check/9658218873": "example_5000000003",
"patient-check/9658218881": "example_5000000004",
"patient-check/9735548844": "example_5000000004",
"patient-check/9658218903": "example_5000000005",
"patient-check/9658218989": "example_5000000006",
"patient-check/9658218997": "example_5000000007",
"patient-check/9658219004": "example_5000000008",
"patient-check/9658219012": "example_5000000009",
"patient-check/9658220142": "example_5000000010",
"patient-check/9658220150": "example_5000000011",
"patient-check/9450114080": "example_5000000012",
"patient-check/9466447939": "example_5000000013",
"patient-check/9657933617": "example_5000000014",
"patient-check/9735549018": "example_5000000015",
"patient-check/9735549026": "example_5000000016",
"patient-check/9735549034": "example_5000000017",
"patient-check/9735549042": "example_5000000018",
"patient-check/9735549050": "example_5000000019",
"patient-check/9735549069": "example_5000000020",
"patient-check/9735549077": "example_5000000021",
"patient-check/9735549085": "example_5000000022",
"patient-check/9735549093": "example_5000000023",
"patient-check/9735549107": "example_5000000024",
"patient-check/9800878378": "code400",
"patient-check/9661033404": "code404",
"patient-check/9451019030": "code422",
"patient-check/9436793375": "code500",
}
def exclude_hop_by_hop(headers: dict) -> list[tuple[str, str]]:
"""
Exclude hop-by-hop headers, which are meaningful only for a single
transport-level connection, and are not stored by caches or forwarded by
proxies. See https://www.rfc-editor.org/rfc/rfc2616#section-13.5.1.
"""
return [(k, v) for k, v in headers.items() if k.lower() not in HOP_BY_HOP_HEADERS]
def get_prism_prompt_for_example(patient_examples: dict, request: Request) -> str | None:
"""
Given the whole request, return the `Prefer:` header value if a specific
example is desired. Otherwise, return `None`.
"""
sorted_examples = sorted(patient_examples.items(), key=lambda x: len(x[0]), reverse=True)
for patient_id, example in sorted_examples:
if patient_id+"?" in request.full_path:
return example
if request.full_path.find("patient-check/") > -1:
return ("example_" + request.full_path[request.full_path.find("patient-check/")+14:]).rstrip("?")
return None
def parse_prefer_header_value(prefer_header_value: str) -> str:
"""
Parse the Prefer header value to extract the example name.
"""
if prefer_header_value.startswith("example"):
return f"example={prefer_header_value}"
if prefer_header_value.startswith("code"):
return f"code={prefer_header_value[4:]}"
return ""
@app.route("/patient-check/_status", methods=["GET"])
def health_check() -> Response:
"""
Health-check endpoint to verify the application is running.
Returns a 200 OK status with a simple JSON response.
"""
status_json = {
"status": "pass",
"version": "",
"revision": "",
"releaseId": "",
"commitId": "",
"checks": {
"healthcheckService:status": [
{
"status": "pass",
"timeout": False,
"responseCode": 200,
"outcome": "<html><h1>Ok</h1></html>",
"links": {"self": "https://default-eligibility-signposting-api-live/patient-check/_status"},
}
]
},
}
return make_response(status_json, HTTPStatus.OK, {"Content-Type": "application/json"})
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def proxy_to_upstream(path: str) -> Response: # noqa: ARG001
headers_to_upstream = {k: v for k, v in request.headers if k.lower() != "host"}
prefer_header_value = get_prism_prompt_for_example(PATIENT_EXAMPLES, request)
if prefer_header_value:
headers_to_upstream["prefer"] = parse_prefer_header_value(prefer_header_value)
request_to_upstream = requests.Request(
method=request.method,
url=request.url.replace(request.host_url, UPSTREAM_HOST + "/"), # pyright: ignore [reportOptionalOperand]
headers=headers_to_upstream,
data=request.get_data(),
cookies=request.cookies,
).prepare()
response_from_upstream = session.send(request_to_upstream)
return Response(
response_from_upstream.content,
response_from_upstream.status_code,
exclude_hop_by_hop(dict(response_from_upstream.raw.headers)),
)