-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
152 lines (131 loc) · 5.7 KB
/
app.py
File metadata and controls
152 lines (131 loc) · 5.7 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
"""
Proxy server for Stoplight Prism with response example selection logic.
Adapted from https://stackoverflow.com/a/36601467
"""
import logging
import os
import sys
import requests # pyright: ignore [reportMissingModuleSource]
from flask import Flask, Request, 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_50000000001",
"patient-check/5000000002": "example_50000000002",
"patient-check/5000000003": "example_50000000003",
"patient-check/5000000004": "example_50000000004",
"patient-check/5000000005": "example_50000000005",
"patient-check/5000000006": "example_50000000006",
"patient-check/5000000007": "example_50000000007",
"patient-check/5000000008": "example_50000000008",
"patient-check/5000000009": "example_50000000009",
"patient-check/5000000010": "example_50000000010",
"patient-check/5000000011": "example_50000000011",
"patient-check/5000000012": "example_50000000012",
"patient-check/5000000013": "example_50000000013",
"patient-check/5000000014": "example_50000000014",
"patient-check/50000000001": "example_50000000001",
"patient-check/50000000002": "example_50000000002",
"patient-check/50000000003": "example_50000000003",
"patient-check/50000000004": "example_50000000004",
"patient-check/50000000005": "example_50000000005",
"patient-check/50000000006": "example_50000000006",
"patient-check/50000000007": "example_50000000007",
"patient-check/50000000008": "example_50000000008",
"patient-check/50000000009": "example_50000000009",
"patient-check/50000000010": "example_50000000010",
"patient-check/50000000011": "example_50000000011",
"patient-check/50000000012": "example_50000000012",
"patient-check/50000000013": "example_50000000013",
"patient-check/50000000014": "example_50000000014",
"patient-check/9686368973": "example_50000000001",
"patient-check/9686368906": "example_50000000002",
"patient-check/9658218873": "example_50000000003",
"patient-check/9658218881": "example_50000000004",
"patient-check/9658218903": "example_50000000005",
"patient-check/9658218989": "example_50000000006",
"patient-check/9658218997": "example_50000000007",
"patient-check/9658219004": "example_50000000008",
"patient-check/9658219012": "example_50000000009",
"patient-check/9658220142": "example_50000000010",
"patient-check/9658220150": "example_50000000011",
"patient-check/9450114080": "example_50000000012",
"patient-check/9466447939": "example_50000000013",
"patient-check/9657933617": "example_50000000014",
"patient-check/90000000400": "code400",
"patient-check/90000000404": "code404",
"patient-check/90000000422": "code422",
"patient-check/90000000500": "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`.
"""
for patient_id, example in patient_examples.items():
if patient_id in request.full_path:
return example
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("/_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.
"""
return Response('{"status": "ok"}', status=200, mimetype="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)),
)