Skip to content

Commit aed8ddd

Browse files
docs(middleware-factory): Fix and improve typing (#3569)
* Fix typing in middleware examples * Update highlighted lines * Reduce highlighting to improve readability --------- Co-authored-by: Leandro Damascena <[email protected]>
1 parent 9f1e42d commit aed8ddd

7 files changed

+39
-17
lines changed

docs/utilities/middleware_factory.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ You can create your own middleware using `lambda_handler_decorator`. The decorat
3030
### Middleware with before logic
3131

3232
=== "getting_started_middleware_before_logic_function.py"
33-
```python hl_lines="5 26 27 32 33 35 40 41"
33+
```python hl_lines="5 26 27 36 37 39 44 45"
3434
--8<-- "examples/middleware_factory/src/getting_started_middleware_before_logic_function.py"
3535
```
3636

@@ -43,7 +43,7 @@ You can create your own middleware using `lambda_handler_decorator`. The decorat
4343
### Middleware with after logic
4444

4545
=== "getting_started_middleware_after_logic_function.py"
46-
```python hl_lines="7 14 15 21-23 37"
46+
```python hl_lines="8 14 15 24-26 40 41"
4747
--8<-- "examples/middleware_factory/src/getting_started_middleware_after_logic_function.py"
4848
```
4949

@@ -58,7 +58,7 @@ You can create your own middleware using `lambda_handler_decorator`. The decorat
5858
You can also have your own keyword arguments after the mandatory arguments.
5959

6060
=== "getting_started_middleware_with_params_function.py"
61-
```python hl_lines="6 30 31 32 36 52"
61+
```python hl_lines="6 30 31 41 56 57"
6262
--8<-- "examples/middleware_factory/src/getting_started_middleware_with_params_function.py"
6363
```
6464

@@ -83,7 +83,7 @@ You can also use [`POWERTOOLS_TRACE_MIDDLEWARES`](#tracing-middleware-execution)
8383
For advanced use cases, you can instantiate [Tracer](../core/tracer.md){target="_blank"} inside your middleware, and add annotations as well as metadata for additional operational insights.
8484

8585
=== "advanced_middleware_tracer_function.py"
86-
```python hl_lines="7 9 12 16 17 19 25 42"
86+
```python hl_lines="7 9 12 16 17 22 28 45 46"
8787
--8<-- "examples/middleware_factory/src/advanced_middleware_tracer_function.py"
8888
```
8989

@@ -105,7 +105,7 @@ This makes use of an existing Tracer instance that you may have initialized anyw
105105
You must [enable Active Tracing](../core/tracer.md#permissions){target="_blank"} in your Lambda function when using this feature, otherwise Lambda cannot send traces to XRay.
106106

107107
=== "getting_started_middleware_tracer_function.py"
108-
```python hl_lines="8 14 15 36"
108+
```python hl_lines="8 14 15 39 40"
109109
--8<-- "examples/middleware_factory/src/getting_started_middleware_tracer_function.py"
110110
```
111111

@@ -134,7 +134,7 @@ In the example below, we create a Middleware with the following features:
134134
* Save execution history to a DynamoDB table
135135

136136
=== "combining_powertools_utilities_function.py"
137-
```python hl_lines="11 28 29 119 52 61 73"
137+
```python hl_lines="11 28 29 56 64 77 123"
138138
--8<-- "examples/middleware_factory/src/combining_powertools_utilities_function.py"
139139
```
140140

examples/middleware_factory/src/advanced_middleware_tracer_function.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414

1515

1616
@lambda_handler_decorator(trace_execution=True)
17-
def middleware_with_advanced_tracing(handler, event, context) -> Callable:
18-
17+
def middleware_with_advanced_tracing(
18+
handler: Callable[[dict, LambdaContext], dict],
19+
event: dict,
20+
context: LambdaContext,
21+
) -> dict:
1922
tracer.put_metadata(key="resource", value=event.get("resource"))
2023

2124
start_time = time.time()

examples/middleware_factory/src/combining_powertools_utilities_function.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626

2727

2828
@lambda_handler_decorator(trace_execution=True)
29-
def middleware_custom(handler: Callable, event: dict, context: LambdaContext):
29+
def middleware_custom(
30+
handler: Callable[[dict, LambdaContext], dict],
31+
event: dict,
32+
context: LambdaContext,
33+
) -> dict:
3034
# validating the INPUT with the given schema
3135
# X-Customer-Id header must be informed in all requests
3236
try:

examples/middleware_factory/src/getting_started_middleware_after_logic_function.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212

1313

1414
@lambda_handler_decorator
15-
def middleware_after(handler, event, context) -> Callable:
16-
15+
def middleware_after(
16+
handler: Callable[[dict, LambdaContext], dict],
17+
event: dict,
18+
context: LambdaContext,
19+
) -> dict:
1720
start_time = time.time()
1821
response = handler(event, context)
1922
execution_time = time.time() - start_time

examples/middleware_factory/src/getting_started_middleware_before_logic_function.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ class PaymentError(Exception):
2424

2525

2626
@lambda_handler_decorator
27-
def middleware_before(handler, event, context) -> Callable:
27+
def middleware_before(
28+
handler: Callable[[dict, LambdaContext], dict],
29+
event: dict,
30+
context: LambdaContext,
31+
) -> dict:
2832
# extract payload from a EventBridge event
2933
detail: dict = extract_data_from_envelope(data=event, envelope=envelopes.EVENTBRIDGE)
3034

@@ -38,7 +42,7 @@ def middleware_before(handler, event, context) -> Callable:
3842

3943

4044
@middleware_before
41-
def lambda_handler(event, context: LambdaContext) -> dict:
45+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
4246
try:
4347
payment_payload: dict = extract_data_from_envelope(data=event, envelope=envelopes.EVENTBRIDGE)
4448
return {

examples/middleware_factory/src/getting_started_middleware_tracer_function.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212

1313

1414
@lambda_handler_decorator(trace_execution=True)
15-
def middleware_with_tracing(handler, event, context) -> Callable:
16-
15+
def middleware_with_tracing(
16+
handler: Callable[[dict, LambdaContext], dict],
17+
event: dict,
18+
context: LambdaContext,
19+
) -> dict:
1720
start_time = time.time()
1821
response = handler(event, context)
1922
execution_time = time.time() - start_time

examples/middleware_factory/src/getting_started_middleware_with_params_function.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ class BookingError(Exception):
2828

2929

3030
@lambda_handler_decorator
31-
def obfuscate_sensitive_data(handler, event, context, fields: List) -> Callable:
31+
def obfuscate_sensitive_data(
32+
handler: Callable[[dict, LambdaContext], dict],
33+
event: dict,
34+
context: LambdaContext,
35+
fields: List,
36+
) -> dict:
3237
# extracting payload from a EventBridge event
3338
detail: dict = extract_data_from_envelope(data=event, envelope=envelopes.EVENTBRIDGE)
3439
guest_data: Any = detail.get("guest")
@@ -49,7 +54,7 @@ def obfuscate_data(value: str) -> bytes:
4954

5055

5156
@obfuscate_sensitive_data(fields=["email", "passport", "vat"])
52-
def lambda_handler(event, context: LambdaContext) -> dict:
57+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
5358
try:
5459
booking_payload: dict = extract_data_from_envelope(data=event, envelope=envelopes.EVENTBRIDGE)
5560
return {

0 commit comments

Comments
 (0)