Skip to content

Commit cf8388c

Browse files
committed
fix: address comments
1 parent 99659bf commit cf8388c

File tree

3 files changed

+64
-49
lines changed

3 files changed

+64
-49
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+10-49
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
from aws_lambda_powertools.event_handler import content_types
3131
from aws_lambda_powertools.event_handler.exceptions import NotFoundError, ServiceError
32+
from aws_lambda_powertools.event_handler.openapi.constants import DEFAULT_API_VERSION, DEFAULT_OPENAPI_VERSION
33+
from aws_lambda_powertools.event_handler.openapi.swagger_ui.html import generate_swagger_html
3234
from aws_lambda_powertools.event_handler.openapi.types import (
3335
COMPONENT_REF_PREFIX,
3436
METHODS_WITH_BODY,
@@ -1334,8 +1336,8 @@ def get_openapi_schema(
13341336
self,
13351337
*,
13361338
title: str = "Powertools API",
1337-
version: str = "1.0.0",
1338-
openapi_version: str = "3.1.0",
1339+
version: str = DEFAULT_API_VERSION,
1340+
openapi_version: str = DEFAULT_OPENAPI_VERSION,
13391341
summary: Optional[str] = None,
13401342
description: Optional[str] = None,
13411343
tags: Optional[List[str]] = None,
@@ -1456,8 +1458,8 @@ def get_openapi_json_schema(
14561458
self,
14571459
*,
14581460
title: str = "Powertools API",
1459-
version: str = "1.0.0",
1460-
openapi_version: str = "3.1.0",
1461+
version: str = DEFAULT_API_VERSION,
1462+
openapi_version: str = DEFAULT_OPENAPI_VERSION,
14611463
summary: Optional[str] = None,
14621464
description: Optional[str] = None,
14631465
tags: Optional[List[str]] = None,
@@ -1521,9 +1523,9 @@ def enable_swagger(
15211523
self,
15221524
*,
15231525
path: str = "/swagger",
1524-
title: str = "Powertools API",
1525-
version: str = "1.0.0",
1526-
openapi_version: str = "3.1.0",
1526+
title: str = "Powertools for AWS Lambda (Python) API",
1527+
version: str = DEFAULT_API_VERSION,
1528+
openapi_version: str = DEFAULT_OPENAPI_VERSION,
15271529
summary: Optional[str] = None,
15281530
description: Optional[str] = None,
15291531
tags: Optional[List[str]] = None,
@@ -1577,7 +1579,6 @@ def swagger_js():
15771579
status_code=200,
15781580
content_type="text/javascript",
15791581
body=body,
1580-
compress=True,
15811582
)
15821583

15831584
@self.get("/swagger.css", include_in_schema=False)
@@ -1587,7 +1588,6 @@ def swagger_css():
15871588
status_code=200,
15881589
content_type="text/css",
15891590
body=body,
1590-
compress=True,
15911591
)
15921592

15931593
@self.get(path, middlewares=middlewares, include_in_schema=False)
@@ -1616,51 +1616,12 @@ def swagger_handler():
16161616
license_info=license_info,
16171617
)
16181618

1619-
body = f"""
1620-
<!DOCTYPE html>
1621-
<html>
1622-
<head>
1623-
<meta charset="UTF-8">
1624-
<title>Swagger UI</title>
1625-
<link rel="stylesheet" type="text/css" href="{swagger_css}">
1626-
</head>
1627-
1628-
<body>
1629-
<div id="swagger-ui">
1630-
Loading...
1631-
</div>
1632-
</body>
1633-
1634-
<script src="{swagger_js}"></script>
1635-
1636-
<script>
1637-
var swaggerUIOptions = {{
1638-
dom_id: "#swagger-ui",
1639-
docExpansion: "list",
1640-
deepLinking: true,
1641-
filter: true,
1642-
spec: JSON.parse(`
1643-
{spec}
1644-
`.trim()),
1645-
presets: [
1646-
SwaggerUIBundle.presets.apis,
1647-
SwaggerUIBundle.SwaggerUIStandalonePreset
1648-
],
1649-
plugins: [
1650-
SwaggerUIBundle.plugins.DownloadUrl
1651-
]
1652-
}}
1653-
1654-
var ui = SwaggerUIBundle(swaggerUIOptions)
1655-
</script>
1656-
</html>
1657-
""".strip()
1619+
body = generate_swagger_html(spec, swagger_js, swagger_css)
16581620

16591621
return Response(
16601622
status_code=200,
16611623
content_type="text/html",
16621624
body=body,
1663-
compress=True,
16641625
)
16651626

16661627
def route(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DEFAULT_API_VERSION = "1.0.0"
2+
DEFAULT_OPENAPI_VERSION = "3.1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
def generate_swagger_html(spec: str, js_url: str, css_url: str) -> str:
2+
"""
3+
Generate Swagger UI HTML page
4+
5+
Parameters
6+
----------
7+
spec: str
8+
The OpenAPI spec in the JSON format
9+
js_url: str
10+
The URL to the Swagger UI JavaScript file
11+
css_url: str
12+
The URL to the Swagger UI CSS file
13+
"""
14+
return f"""
15+
<!DOCTYPE html>
16+
<html>
17+
<head>
18+
<meta charset="UTF-8">
19+
<title>Swagger UI</title>
20+
<link rel="stylesheet" type="text/css" href="{css_url}">
21+
</head>
22+
23+
<body>
24+
<div id="swagger-ui">
25+
Loading...
26+
</div>
27+
</body>
28+
29+
<script src="{js_url}"></script>
30+
31+
<script>
32+
var swaggerUIOptions = {{
33+
dom_id: "#swagger-ui",
34+
docExpansion: "list",
35+
deepLinking: true,
36+
filter: true,
37+
spec: JSON.parse(`
38+
{spec}
39+
`.trim()),
40+
presets: [
41+
SwaggerUIBundle.presets.apis,
42+
SwaggerUIBundle.SwaggerUIStandalonePreset
43+
],
44+
plugins: [
45+
SwaggerUIBundle.plugins.DownloadUrl
46+
]
47+
}}
48+
49+
var ui = SwaggerUIBundle(swaggerUIOptions)
50+
</script>
51+
</html>
52+
""".strip()

0 commit comments

Comments
 (0)