Skip to content

Commit 5a28f8b

Browse files
Changing examples
1 parent 9db63ef commit 5a28f8b

8 files changed

+28
-20
lines changed

aws_lambda_powertools/middleware_factory/factory.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import os
77
from typing import Any, Callable
88

9-
from ..shared import constants
10-
from ..shared.functions import resolve_truthy_env_var_choice
11-
from ..tracing import Tracer
12-
from .exceptions import MiddlewareInvalidArgumentError
9+
from aws_lambda_powertools.middleware_factory.exceptions import MiddlewareInvalidArgumentError
10+
from aws_lambda_powertools.shared import constants
11+
from aws_lambda_powertools.shared.functions import resolve_truthy_env_var_choice
12+
from aws_lambda_powertools.tracing import Tracer
1313

1414
logger = logging.getLogger(__name__)
1515

docs/utilities/middleware_factory.md

+6-6
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 36 37 39 44 45"
33+
```python hl_lines="7 27 28 37 38 40 43 44"
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="8 14 15 24-26 40 41"
46+
```python hl_lines="9 16 17 26-28 42 43"
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 41 56 57"
61+
```python hl_lines="8 31 32 42 55 56"
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 22 28 45 46"
86+
```python hl_lines="9 11 18 19 28 30 47 48"
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 39 40"
108+
```python hl_lines="10 16 17 41 42"
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 56 64 77 123"
137+
```python hl_lines="13 30 31 58 66 79 125"
138138
--8<-- "examples/middleware_factory/src/combining_powertools_utilities_function.py"
139139
```
140140

examples/middleware_factory/src/advanced_middleware_tracer_function.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import time
24
from typing import Callable
35

examples/middleware_factory/src/combining_powertools_utilities_function.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import json
24
from typing import Callable
35

@@ -103,7 +105,7 @@ def get_comments():
103105

104106
return {"comments": comments.json()[:10]}
105107
except Exception as exc:
106-
raise InternalServerError(str(exc))
108+
raise InternalServerError(str(exc)) from exc
107109

108110

109111
@app.get("/comments/<comment_id>")
@@ -115,7 +117,7 @@ def get_comments_by_id(comment_id: str):
115117

116118
return {"comments": comments.json()}
117119
except Exception as exc:
118-
raise InternalServerError(str(exc))
120+
raise InternalServerError(str(exc)) from exc
119121

120122

121123
@middleware_custom

examples/middleware_factory/src/getting_started_middleware_after_logic_function.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import time
24
from typing import Callable
35

examples/middleware_factory/src/getting_started_middleware_before_logic_function.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from dataclasses import dataclass, field
24
from typing import Callable
35
from uuid import uuid4
@@ -35,9 +37,7 @@ def middleware_before(
3537
if "status_id" not in detail:
3638
event["detail"]["status_id"] = "pending"
3739

38-
response = handler(event, context)
39-
40-
return response
40+
return handler(event, context)
4141

4242

4343
@middleware_before

examples/middleware_factory/src/getting_started_middleware_tracer_function.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import time
24
from typing import Callable
35

examples/middleware_factory/src/getting_started_middleware_with_params_function.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from __future__ import annotations
2+
13
import base64
24
from dataclasses import dataclass, field
3-
from typing import Any, Callable, List
5+
from typing import Any, Callable
46
from uuid import uuid4
57

68
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
@@ -31,7 +33,7 @@ def obfuscate_sensitive_data(
3133
handler: Callable[[dict, LambdaContext], dict],
3234
event: dict,
3335
context: LambdaContext,
34-
fields: List,
36+
fields: list,
3537
) -> dict:
3638
# extracting payload from a EventBridge event
3739
detail: dict = query(data=event, envelope=envelopes.EVENTBRIDGE)
@@ -42,9 +44,7 @@ def obfuscate_sensitive_data(
4244
if guest_data.get(guest_field):
4345
event["detail"]["guest"][guest_field] = obfuscate_data(str(guest_data.get(guest_field)))
4446

45-
response = handler(event, context)
46-
47-
return response
47+
return handler(event, context)
4848

4949

5050
def obfuscate_data(value: str) -> bytes:

0 commit comments

Comments
 (0)