-
Notifications
You must be signed in to change notification settings - Fork 436
docs(middleware-factory): snippets split, improved, and lint #1451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
heitorlessa
merged 10 commits into
aws-powertools:develop
from
leandrodamascena:chore/middleware-factory
Aug 25, 2022
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
597f2cf
docs(middleware-factory): Refactoring examples - before logic
leandrodamascena ec90c5d
docs(middleware-factory): Refactoring examples - after logic
leandrodamascena 5cdf6c6
docs(middleware-factory): Refactoring examples - params example
leandrodamascena 42f03c9
docs(middleware-factory): Refactoring examples - advanced tracer
leandrodamascena 4c2a1e7
docs(middleware-factory): Refactoring examples - fix typing
leandrodamascena b2e1945
fix: navigation and old wording
heitorlessa 54a0600
docs(middleware-factory): Refactoring examples - combining utilities
leandrodamascena fbe67c7
docs(middleware-factory): Refactoring examples - combining utilities
leandrodamascena 79b7b63
docs(middleware-factory): Refactoring examples - combining utilities …
leandrodamascena 78e7bd2
docs(middleware-factory): Refactoring examples - minor changes
leandrodamascena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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
44 changes: 44 additions & 0 deletions
44
examples/middleware_factory/src/advanced_middleware_tracer_function.py
This file contains hidden or 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,44 @@ | ||
import time | ||
from typing import Callable | ||
|
||
import requests | ||
from requests import Response | ||
|
||
from aws_lambda_powertools import Tracer | ||
from aws_lambda_powertools.event_handler import APIGatewayRestResolver | ||
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
tracer = Tracer() | ||
app = APIGatewayRestResolver() | ||
|
||
|
||
@lambda_handler_decorator(trace_execution=True) | ||
def middleware_with_advanced_tracing(handler, event, context) -> Callable: | ||
|
||
tracer.put_metadata(key="resource", value=event.get("resource")) | ||
|
||
start_time = time.time() | ||
response = handler(event, context) | ||
execution_time = time.time() - start_time | ||
|
||
tracer.put_annotation(key="TotalExecutionTime", value=str(execution_time)) | ||
|
||
# adding custom headers in response object after lambda executing | ||
response["headers"]["execution_time"] = execution_time | ||
response["headers"]["aws_request_id"] = context.aws_request_id | ||
|
||
return response | ||
|
||
|
||
@app.get("/products") | ||
def create_product() -> dict: | ||
product: Response = requests.get("https://dummyjson.com/products/1") | ||
product.raise_for_status() | ||
|
||
return {"product": product.json()} | ||
|
||
|
||
@middleware_with_advanced_tracing | ||
def lambda_handler(event: dict, context: LambdaContext) -> dict: | ||
return app.resolve(event, context) |
5 changes: 5 additions & 0 deletions
5
examples/middleware_factory/src/advanced_middleware_tracer_payload.json
This file contains hidden or 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,5 @@ | ||
{ | ||
"resource": "/products", | ||
"path": "/products", | ||
"httpMethod": "GET" | ||
} |
39 changes: 39 additions & 0 deletions
39
examples/middleware_factory/src/getting_started_middleware_after_logic_function.py
This file contains hidden or 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,39 @@ | ||
import time | ||
from typing import Callable | ||
|
||
import requests | ||
from requests import Response | ||
|
||
from aws_lambda_powertools.event_handler import APIGatewayRestResolver | ||
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
app = APIGatewayRestResolver() | ||
|
||
|
||
@lambda_handler_decorator | ||
def middleware_after(handler, event, context) -> Callable: | ||
|
||
start_time = time.time() | ||
response = handler(event, context) | ||
execution_time = time.time() - start_time | ||
|
||
# adding custom headers in response object after lambda executing | ||
response["headers"]["execution_time"] = execution_time | ||
response["headers"]["aws_request_id"] = context.aws_request_id | ||
|
||
return response | ||
|
||
|
||
@app.post("/todos") | ||
def create_todo() -> dict: | ||
todo_data: dict = app.current_event.json_body # deserialize json str to dict | ||
todo: Response = requests.post("https://jsonplaceholder.typicode.com/todos", data=todo_data) | ||
todo.raise_for_status() | ||
|
||
return {"todo": todo.json()} | ||
|
||
|
||
@middleware_after | ||
def lambda_handler(event: dict, context: LambdaContext) -> dict: | ||
return app.resolve(event, context) |
6 changes: 6 additions & 0 deletions
6
examples/middleware_factory/src/getting_started_middleware_after_logic_payload.json
This file contains hidden or 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,6 @@ | ||
{ | ||
"resource": "/todos", | ||
"path": "/todos", | ||
"httpMethod": "POST", | ||
"body": "{\"title\": \"foo\", \"userId\": 1, \"completed\": false}" | ||
} |
47 changes: 47 additions & 0 deletions
47
examples/middleware_factory/src/getting_started_middleware_before_logic_function.py
This file contains hidden or 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,47 @@ | ||
from dataclasses import dataclass, field | ||
from typing import Callable | ||
from uuid import uuid4 | ||
|
||
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator | ||
from aws_lambda_powertools.utilities.jmespath_utils import envelopes, extract_data_from_envelope | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
|
||
@dataclass | ||
class Payment: | ||
user_id: str | ||
order_id: str | ||
amount: float | ||
status_id: str | ||
payment_id: str = field(default_factory=lambda: f"{uuid4()}") | ||
|
||
|
||
class PaymentError(Exception): | ||
... | ||
|
||
|
||
@lambda_handler_decorator | ||
def middleware_before(handler, event, context) -> Callable: | ||
# extract payload from a EventBridge event | ||
detail: dict = extract_data_from_envelope(data=event, envelope=envelopes.EVENTBRIDGE) | ||
|
||
# check if status_id exists in payload, otherwise add default state before processing payment | ||
if not detail.get("status_id"): | ||
leandrodamascena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
event["detail"]["status_id"] = "pending" | ||
|
||
response = handler(event, context) | ||
|
||
return response | ||
|
||
|
||
@middleware_before | ||
def lambda_handler(event, context: LambdaContext) -> dict: | ||
try: | ||
payment_payload: dict = extract_data_from_envelope(data=event, envelope=envelopes.EVENTBRIDGE) | ||
return { | ||
"order": Payment(**payment_payload).__dict__, | ||
"message": "payment created", | ||
"success": True, | ||
} | ||
except Exception as e: | ||
raise PaymentError("Unable to create payment") from e |
14 changes: 14 additions & 0 deletions
14
examples/middleware_factory/src/getting_started_middleware_before_logic_payload.json
This file contains hidden or 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,14 @@ | ||
{ | ||
"version": "0", | ||
"id": "9c95e8e4-96a4-ef3f-b739-b6aa5b193afb", | ||
"detail-type": "PaymentCreated", | ||
"source": "app.payment", | ||
"account": "0123456789012", | ||
"time": "2022-08-08T20:41:53Z", | ||
"region": "eu-east-1", | ||
"detail": { | ||
"amount": "150.00", | ||
"order_id": "8f1f1710-1b30-48a5-a6bd-153fd23b866b", | ||
"user_id": "f80e3c51-5b8c-49d5-af7d-c7804966235f" | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
examples/middleware_factory/src/getting_started_middleware_tracer_function.py
This file contains hidden or 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,38 @@ | ||
import time | ||
from typing import Callable | ||
|
||
import requests | ||
from requests import Response | ||
|
||
from aws_lambda_powertools.event_handler import APIGatewayRestResolver | ||
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
app = APIGatewayRestResolver() | ||
|
||
|
||
@lambda_handler_decorator(trace_execution=True) | ||
def middleware_with_tracing(handler, event, context) -> Callable: | ||
|
||
start_time = time.time() | ||
response = handler(event, context) | ||
execution_time = time.time() - start_time | ||
|
||
# adding custom headers in response object after lambda executing | ||
response["headers"]["execution_time"] = execution_time | ||
response["headers"]["aws_request_id"] = context.aws_request_id | ||
|
||
return response | ||
|
||
|
||
@app.get("/products") | ||
def create_product() -> dict: | ||
product: Response = requests.get("https://dummyjson.com/products/1") | ||
product.raise_for_status() | ||
|
||
return {"product": product.json()} | ||
|
||
|
||
@middleware_with_tracing | ||
def lambda_handler(event: dict, context: LambdaContext) -> dict: | ||
return app.resolve(event, context) |
5 changes: 5 additions & 0 deletions
5
examples/middleware_factory/src/getting_started_middleware_tracer_payload.json
This file contains hidden or 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,5 @@ | ||
{ | ||
"resource": "/products", | ||
"path": "/products", | ||
"httpMethod": "GET" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.