Skip to content

Commit 3c99c8b

Browse files
Refactoring examples
1 parent db19aa9 commit 3c99c8b

6 files changed

+58
-62
lines changed

docs/core/event_handler/_openapi_customization_metadata.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- markdownlint-disable MD041 MD043 -->
22

3-
Defining and customizing OpenAPI metadata gives detailed, top-level information about your API. Here's the method to set and tailor this metadata:
3+
Defining and customizing OpenAPI metadata gives detailed, top-level information about your API. Use the method `app.configure_openapi` to set and tailor this metadata:
44

55
| Field Name | Type | Description |
66
| ------------------ | -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

docs/core/event_handler/api_gateway.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ Include extra parameters when exporting your OpenAPI specification to apply thes
10531053

10541054
=== "customizing_api_metadata.py"
10551055

1056-
```python hl_lines="25-31"
1056+
```python hl_lines="8-16"
10571057
--8<-- "examples/event_handler_rest/src/customizing_api_metadata.py"
10581058
```
10591059

@@ -1089,23 +1089,23 @@ Security schemes are declared at the top-level first. You can reference them glo
10891089

10901090
=== "Global OpenAPI security schemes"
10911091

1092-
```python title="security_schemes_global.py" hl_lines="32-42"
1092+
```python title="security_schemes_global.py" hl_lines="17-27"
10931093
--8<-- "examples/event_handler_rest/src/security_schemes_global.py"
10941094
```
10951095

10961096
1. Using the oauth security scheme defined earlier, scoped to the "admin" role.
10971097

10981098
=== "Per Operation security"
10991099

1100-
```python title="security_schemes_per_operation.py" hl_lines="17 32-41"
1100+
```python title="security_schemes_per_operation.py" hl_lines="17-26 30"
11011101
--8<-- "examples/event_handler_rest/src/security_schemes_per_operation.py"
11021102
```
11031103

11041104
1. Using the oauth security scheme defined bellow, scoped to the "admin" role.
11051105

11061106
=== "Global security schemes and optional security per route"
11071107

1108-
```python title="security_schemes_global_and_optional.py" hl_lines="22 37-46"
1108+
```python title="security_schemes_global_and_optional.py" hl_lines="17-26 35"
11091109
--8<-- "examples/event_handler_rest/src/security_schemes_global_and_optional.py"
11101110
```
11111111

examples/event_handler_rest/src/customizing_api_metadata.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
from aws_lambda_powertools.utilities.typing import LambdaContext
66

77
app = APIGatewayRestResolver(enable_validation=True)
8+
app.configure_openapi(
9+
title="TODO's API",
10+
version="1.21.3",
11+
summary="API to manage TODOs",
12+
description="This API implements all the CRUD operations for the TODO app",
13+
tags=["todos"],
14+
servers=[Server(url="https://stg.example.org/orders", description="Staging server")],
15+
contact=Contact(name="John Smith", email="john@smith.com"),
16+
)
817

918

1019
@app.get("/todos/<todo_id>")
@@ -20,14 +29,4 @@ def lambda_handler(event: dict, context: LambdaContext) -> dict:
2029

2130

2231
if __name__ == "__main__":
23-
print(
24-
app.get_openapi_json_schema(
25-
title="TODO's API",
26-
version="1.21.3",
27-
summary="API to manage TODOs",
28-
description="This API implements all the CRUD operations for the TODO app",
29-
tags=["todos"],
30-
servers=[Server(url="https://stg.example.org/orders", description="Staging server")],
31-
contact=Contact(name="John Smith", email="john@smith.com"),
32-
),
33-
)
32+
print(app.get_openapi_json_schema())

examples/event_handler_rest/src/security_schemes_global.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212
logger = Logger()
1313

1414
app = APIGatewayRestResolver(enable_validation=True)
15+
app.configure_openapi(
16+
title="My API",
17+
security_schemes={
18+
"oauth": OAuth2(
19+
flows=OAuthFlows(
20+
authorizationCode=OAuthFlowAuthorizationCode(
21+
authorizationUrl="https://xxx.amazoncognito.com/oauth2/authorize",
22+
tokenUrl="https://xxx.amazoncognito.com/oauth2/token",
23+
),
24+
),
25+
),
26+
},
27+
security=[{"oauth": ["admin"]}], # (1)!)
28+
)
1529

1630

1731
@app.get("/")
@@ -26,19 +40,4 @@ def lambda_handler(event, context):
2640

2741

2842
if __name__ == "__main__":
29-
print(
30-
app.get_openapi_json_schema(
31-
title="My API",
32-
security_schemes={
33-
"oauth": OAuth2(
34-
flows=OAuthFlows(
35-
authorizationCode=OAuthFlowAuthorizationCode(
36-
authorizationUrl="https://xxx.amazoncognito.com/oauth2/authorize",
37-
tokenUrl="https://xxx.amazoncognito.com/oauth2/token",
38-
),
39-
),
40-
),
41-
},
42-
security=[{"oauth": ["admin"]}], # (1)!
43-
),
44-
)
43+
print(app.get_openapi_json_schema())

examples/event_handler_rest/src/security_schemes_global_and_optional.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
logger = Logger()
1313

1414
app = APIGatewayRestResolver(enable_validation=True)
15+
app.configure_openapi(
16+
title="My API",
17+
security_schemes={
18+
"oauth": OAuth2(
19+
flows=OAuthFlows(
20+
authorizationCode=OAuthFlowAuthorizationCode(
21+
authorizationUrl="https://xxx.amazoncognito.com/oauth2/authorize",
22+
tokenUrl="https://xxx.amazoncognito.com/oauth2/token",
23+
),
24+
),
25+
),
26+
},
27+
)
1528

1629

1730
@app.get("/protected", security=[{"oauth": ["admin"]}])
@@ -31,18 +44,4 @@ def lambda_handler(event, context):
3144

3245

3346
if __name__ == "__main__":
34-
print(
35-
app.get_openapi_json_schema(
36-
title="My API",
37-
security_schemes={
38-
"oauth": OAuth2(
39-
flows=OAuthFlows(
40-
authorizationCode=OAuthFlowAuthorizationCode(
41-
authorizationUrl="https://xxx.amazoncognito.com/oauth2/authorize",
42-
tokenUrl="https://xxx.amazoncognito.com/oauth2/token",
43-
),
44-
),
45-
),
46-
},
47-
),
48-
)
47+
print(app.get_openapi_json_schema())

examples/event_handler_rest/src/security_schemes_per_operation.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
logger = Logger()
1313

1414
app = APIGatewayRestResolver(enable_validation=True)
15+
app.configure_openapi(
16+
title="My API",
17+
security_schemes={
18+
"oauth": OAuth2(
19+
flows=OAuthFlows(
20+
authorizationCode=OAuthFlowAuthorizationCode(
21+
authorizationUrl="https://xxx.amazoncognito.com/oauth2/authorize",
22+
tokenUrl="https://xxx.amazoncognito.com/oauth2/token",
23+
),
24+
),
25+
),
26+
},
27+
)
1528

1629

1730
@app.get("/", security=[{"oauth": ["admin"]}]) # (1)!
@@ -26,18 +39,4 @@ def lambda_handler(event, context):
2639

2740

2841
if __name__ == "__main__":
29-
print(
30-
app.get_openapi_json_schema(
31-
title="My API",
32-
security_schemes={
33-
"oauth": OAuth2(
34-
flows=OAuthFlows(
35-
authorizationCode=OAuthFlowAuthorizationCode(
36-
authorizationUrl="https://xxx.amazoncognito.com/oauth2/authorize",
37-
tokenUrl="https://xxx.amazoncognito.com/oauth2/token",
38-
),
39-
),
40-
),
41-
},
42-
),
43-
)
42+
print(app.get_openapi_json_schema())

0 commit comments

Comments
 (0)