|
25 | 25 | DEFAULT_TEMPLATES = Jinja2Templates(env=jinja2_env)
|
26 | 26 |
|
27 | 27 | 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", |
36 | 36 | # Extensions
|
37 |
| - "Queryables": "queryables", |
38 |
| - "Collection Queryables": "queryables", |
| 37 | + "queryables": "queryables", |
| 38 | + "collection queryables": "queryables", |
39 | 39 | }
|
40 | 40 |
|
41 | 41 |
|
@@ -84,7 +84,7 @@ class HTMLRenderMiddleware:
|
84 | 84 |
|
85 | 85 | app: ASGIApp
|
86 | 86 | templates: Jinja2Templates = field(default_factory=lambda: DEFAULT_TEMPLATES)
|
87 |
| - endpoints_names: dict[str, str] = field(default_factory=lambda: ENDPOINT_TEMPLATES) |
| 87 | + endpoint_names: dict[str, str] = field(default_factory=lambda: ENDPOINT_TEMPLATES) |
88 | 88 |
|
89 | 89 | def create_html_response(
|
90 | 90 | self,
|
@@ -153,7 +153,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send): # noqa: C
|
153 | 153 | start_message: Message
|
154 | 154 | body = b""
|
155 | 155 |
|
156 |
| - async def send_as_html(message: Message): |
| 156 | + async def send_as_html(message: Message): # noqa: C901 |
157 | 157 | nonlocal start_message
|
158 | 158 | nonlocal body
|
159 | 159 |
|
@@ -182,19 +182,63 @@ async def send_as_html(message: Message):
|
182 | 182 | ) and not request.query_params.get("f", ""):
|
183 | 183 | encode_to_html = True
|
184 | 184 |
|
185 |
| - if start_message["status"] == 200 and encode_to_html: |
| 185 | + response_headers = MutableHeaders(scope=start_message) |
| 186 | + |
| 187 | + # stac-fastapi application overwrite the content-type for |
| 188 | + # openapi response and use "application/vnd.oai.openapi+json;version=3.0" |
| 189 | + if ( |
| 190 | + response_headers.get("Content-Type") |
| 191 | + == "application/vnd.oai.openapi+json;version=3.0" |
| 192 | + ): |
| 193 | + openapi_doc = json.loads(body.decode()) |
| 194 | + for _, path in openapi_doc.get("paths").items(): |
| 195 | + if ( |
| 196 | + path.get("get", {}).get("summary", "").lower() |
| 197 | + in self.endpoint_names |
| 198 | + ): |
| 199 | + if "parameters" not in path["get"]: |
| 200 | + path["get"]["parameters"] = [] |
| 201 | + |
| 202 | + path["get"]["parameters"].append( |
| 203 | + { |
| 204 | + "name": "f", |
| 205 | + "in": "query", |
| 206 | + "required": False, |
| 207 | + "schema": { |
| 208 | + "anyOf": [ |
| 209 | + { |
| 210 | + "enum": [ |
| 211 | + "html", |
| 212 | + ], |
| 213 | + "type": "string", |
| 214 | + }, |
| 215 | + {"type": "null"}, |
| 216 | + ], |
| 217 | + "description": "Response MediaType.", |
| 218 | + "title": "F", |
| 219 | + }, |
| 220 | + "description": "Response MediaType.", |
| 221 | + } |
| 222 | + ) |
| 223 | + path["get"]["responses"]["200"]["content"].update( |
| 224 | + {"text/html": {}} |
| 225 | + ) |
| 226 | + |
| 227 | + body = json.dumps(openapi_doc).encode("utf-8") |
| 228 | + response_headers["Content-Length"] = str(len(body)) |
| 229 | + |
| 230 | + elif start_message["status"] == 200 and encode_to_html: |
186 | 231 | # NOTE: `scope["route"]` seems to be specific to FastAPI application
|
187 | 232 | if route := scope.get("route"):
|
188 |
| - if tpl := self.endpoints_names.get(route.name): |
| 233 | + if tpl := self.endpoint_names.get(route.name.lower()): |
189 | 234 | body = self.create_html_response(
|
190 | 235 | request,
|
191 | 236 | json.loads(body.decode()),
|
192 | 237 | template_name=tpl,
|
193 | 238 | title=route.name,
|
194 | 239 | )
|
195 |
| - headers = MutableHeaders(scope=start_message) |
196 |
| - headers["Content-Type"] = "text/html" |
197 |
| - headers["Content-Length"] = str(len(body)) |
| 240 | + response_headers["Content-Type"] = "text/html" |
| 241 | + response_headers["Content-Length"] = str(len(body)) |
198 | 242 |
|
199 | 243 | # Send http.response.start
|
200 | 244 | await send(start_message)
|
|
0 commit comments