-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(event_handler): add ability to expose a Swagger UI (#3254)
Co-authored-by: Leandro Damascena <[email protected]>
- Loading branch information
1 parent
8329153
commit 8a09adc
Showing
17 changed files
with
671 additions
and
63 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DEFAULT_API_VERSION = "1.0.0" | ||
DEFAULT_OPENAPI_VERSION = "3.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
52 changes: 52 additions & 0 deletions
52
aws_lambda_powertools/event_handler/openapi/swagger_ui/html.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
def generate_swagger_html(spec: str, js_url: str, css_url: str) -> str: | ||
""" | ||
Generate Swagger UI HTML page | ||
Parameters | ||
---------- | ||
spec: str | ||
The OpenAPI spec in the JSON format | ||
js_url: str | ||
The URL to the Swagger UI JavaScript file | ||
css_url: str | ||
The URL to the Swagger UI CSS file | ||
""" | ||
return f""" | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Swagger UI</title> | ||
<link rel="stylesheet" type="text/css" href="{css_url}"> | ||
</head> | ||
<body> | ||
<div id="swagger-ui"> | ||
Loading... | ||
</div> | ||
</body> | ||
<script src="{js_url}"></script> | ||
<script> | ||
var swaggerUIOptions = {{ | ||
dom_id: "#swagger-ui", | ||
docExpansion: "list", | ||
deepLinking: true, | ||
filter: true, | ||
spec: JSON.parse(` | ||
{spec} | ||
`.trim()), | ||
presets: [ | ||
SwaggerUIBundle.presets.apis, | ||
SwaggerUIBundle.SwaggerUIStandalonePreset | ||
], | ||
plugins: [ | ||
SwaggerUIBundle.plugins.DownloadUrl | ||
] | ||
}} | ||
var ui = SwaggerUIBundle(swaggerUIOptions) | ||
</script> | ||
</html> | ||
""".strip() |
12 changes: 12 additions & 0 deletions
12
aws_lambda_powertools/event_handler/openapi/swagger_ui/swagger-ui-bundle.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
aws_lambda_powertools/event_handler/openapi/swagger_ui/swagger-ui.min.css
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from aws_lambda_powertools.event_handler import ( | ||
ALBResolver, | ||
APIGatewayHttpResolver, | ||
APIGatewayRestResolver, | ||
LambdaFunctionUrlResolver, | ||
VPCLatticeResolver, | ||
VPCLatticeV2Resolver, | ||
) | ||
from tests.functional.utils import load_event | ||
|
||
|
||
def test_base_path_api_gateway_rest(): | ||
app = APIGatewayRestResolver(enable_validation=True) | ||
|
||
@app.get("/") | ||
def handle(): | ||
return app._get_base_path() | ||
|
||
event = load_event("apiGatewayProxyEvent.json") | ||
event["path"] = "/" | ||
|
||
result = app(event, {}) | ||
assert result["statusCode"] == 200 | ||
assert result["body"] == "" | ||
|
||
|
||
def test_base_path_api_gateway_http(): | ||
app = APIGatewayHttpResolver(enable_validation=True) | ||
|
||
@app.get("/") | ||
def handle(): | ||
return app._get_base_path() | ||
|
||
event = load_event("apiGatewayProxyV2Event.json") | ||
event["rawPath"] = "/" | ||
event["requestContext"]["http"]["path"] = "/" | ||
event["requestContext"]["http"]["method"] = "GET" | ||
|
||
result = app(event, {}) | ||
assert result["statusCode"] == 200 | ||
assert result["body"] == "" | ||
|
||
|
||
def test_base_path_alb(): | ||
app = ALBResolver(enable_validation=True) | ||
|
||
@app.get("/") | ||
def handle(): | ||
return app._get_base_path() | ||
|
||
event = load_event("albEvent.json") | ||
event["path"] = "/" | ||
|
||
result = app(event, {}) | ||
assert result["statusCode"] == 200 | ||
assert result["body"] == "" | ||
|
||
|
||
def test_base_path_lambda_function_url(): | ||
app = LambdaFunctionUrlResolver(enable_validation=True) | ||
|
||
@app.get("/") | ||
def handle(): | ||
return app._get_base_path() | ||
|
||
event = load_event("lambdaFunctionUrlIAMEvent.json") | ||
event["rawPath"] = "/" | ||
event["requestContext"]["http"]["path"] = "/" | ||
event["requestContext"]["http"]["method"] = "GET" | ||
|
||
result = app(event, {}) | ||
assert result["statusCode"] == 200 | ||
assert result["body"] == "" | ||
|
||
|
||
def test_vpc_lattice(): | ||
app = VPCLatticeResolver(enable_validation=True) | ||
|
||
@app.get("/") | ||
def handle(): | ||
return app._get_base_path() | ||
|
||
event = load_event("vpcLatticeEvent.json") | ||
event["raw_path"] = "/" | ||
|
||
result = app(event, {}) | ||
assert result["statusCode"] == 200 | ||
assert result["body"] == "" | ||
|
||
|
||
def test_vpc_latticev2(): | ||
app = VPCLatticeV2Resolver(enable_validation=True) | ||
|
||
@app.get("/") | ||
def handle(): | ||
return app._get_base_path() | ||
|
||
event = load_event("vpcLatticeV2Event.json") | ||
event["path"] = "/" | ||
|
||
result = app(event, {}) | ||
assert result["statusCode"] == 200 | ||
assert result["body"] == "" |
Oops, something went wrong.