Skip to content

Commit a6a6e2c

Browse files
committed
update openapi response to add html parameters
1 parent 3f731eb commit a6a6e2c

File tree

1 file changed

+57
-16
lines changed

1 file changed

+57
-16
lines changed

stac_fastapi/html/middleware.py

+57-16
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
DEFAULT_TEMPLATES = Jinja2Templates(env=jinja2_env)
2626

2727
ENDPOINT_TEMPLATES = {
28-
# endpoint Name: template name
29-
"Landing Page": "landing",
30-
"Conformance Classes": "conformances",
31-
"Get Collections": "collections",
32-
"Get Collection": "collection",
33-
"Get ItemCollection": "items",
34-
"Get Item": "item",
35-
"Search": "search",
28+
# endpoint Name (lower case): template name
29+
"landing page": "landing",
30+
"conformance classes": "conformances",
31+
"get collections": "collections",
32+
"get collection": "collection",
33+
"get itemcollection": "items",
34+
"get item": "item",
35+
"search": "search",
3636
# Extensions
37-
"Queryables": "queryables",
38-
"Collection Queryables": "queryables",
37+
"queryables": "queryables",
38+
"collection queryables": "queryables",
3939
}
4040

4141

@@ -153,7 +153,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send): # noqa: C
153153
start_message: Message
154154
body = b""
155155

156-
async def send_as_html(message: Message):
156+
async def send_as_html(message: Message): # noqa: C901
157157
nonlocal start_message
158158
nonlocal body
159159

@@ -182,19 +182,60 @@ async def send_as_html(message: Message):
182182
) and not request.query_params.get("f", ""):
183183
encode_to_html = True
184184

185-
if start_message["status"] == 200 and encode_to_html:
185+
response_headers = MutableHeaders(scope=start_message)
186+
if (
187+
response_headers.get("Content-Type")
188+
== "application/vnd.oai.openapi+json;version=3.0"
189+
):
190+
openapi_doc = json.loads(body.decode())
191+
for _, path in openapi_doc.get("paths").items():
192+
if (
193+
path.get("get", {}).get("summary", "").lower()
194+
in self.endpoints_names
195+
):
196+
if "parameters" not in path["get"]:
197+
path["get"]["parameters"] = []
198+
199+
path["get"]["parameters"].append(
200+
{
201+
"name": "f",
202+
"in": "query",
203+
"required": False,
204+
"schema": {
205+
"anyOf": [
206+
{
207+
"enum": [
208+
"html",
209+
],
210+
"type": "string",
211+
},
212+
{"type": "null"},
213+
],
214+
"description": "Response MediaType.",
215+
"title": "F",
216+
},
217+
"description": "Response MediaType.",
218+
}
219+
)
220+
path["get"]["responses"]["200"]["content"].update(
221+
{"text/html": {}}
222+
)
223+
224+
body = json.dumps(openapi_doc).encode("utf-8")
225+
response_headers["Content-Length"] = str(len(body))
226+
227+
elif start_message["status"] == 200 and encode_to_html:
186228
# NOTE: `scope["route"]` seems to be specific to FastAPI application
187229
if route := scope.get("route"):
188-
if tpl := self.endpoints_names.get(route.name):
230+
if tpl := self.endpoints_names.get(route.name.lower()):
189231
body = self.create_html_response(
190232
request,
191233
json.loads(body.decode()),
192234
template_name=tpl,
193235
title=route.name,
194236
)
195-
headers = MutableHeaders(scope=start_message)
196-
headers["Content-Type"] = "text/html"
197-
headers["Content-Length"] = str(len(body))
237+
response_headers["Content-Type"] = "text/html"
238+
response_headers["Content-Length"] = str(len(body))
198239

199240
# Send http.response.start
200241
await send(start_message)

0 commit comments

Comments
 (0)