Skip to content
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

fix(docs): extract code snippets into dedicated python files and fix examples #1065

Closed
wants to merge 66 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
5bd027b
doc(data-classes): code snippets into dedicated python files
michaelbrewer Mar 7, 2022
c0df2a3
docs(app-sync): move snippers out for appsync
michaelbrewer Mar 8, 2022
e7918fe
doc(api-gw): Move snippets and fix template.yml
michaelbrewer Mar 8, 2022
be8ec32
doc(api-gw): extract more examples
michaelbrewer Mar 8, 2022
6c6ee63
fix(docs): Correct response and add more examples
michaelbrewer Mar 8, 2022
f7a067b
docs(rest-api): Add more examples
michaelbrewer Mar 8, 2022
b66a2d9
docs(rest-api): Add routes examples
michaelbrewer Mar 8, 2022
ab246cc
feat(rest-api): Complete examples
michaelbrewer Mar 8, 2022
0cadb7d
docs(tutorial): Extract examples
michaelbrewer Mar 9, 2022
029a45d
docs(tutorial): Extract examples
michaelbrewer Mar 9, 2022
0945547
Merge branch 'awslabs:develop' into docs/1064
michaelbrewer Mar 9, 2022
eeb7c04
docs: Refactor to ease develop
michaelbrewer Mar 9, 2022
4571402
docs(validator): Fix name for example and move examples out to files
michaelbrewer Mar 9, 2022
7d745fe
doc(jmespath): Fix examples and extract
michaelbrewer Mar 10, 2022
d1940e1
docs(typing): Extract examples
michaelbrewer Mar 10, 2022
c5a1207
docs(middleware_factory): Extract examples
michaelbrewer Mar 10, 2022
dcd3daf
chore: correct line highlights
michaelbrewer Mar 10, 2022
cd71280
docs(tracer): Extract code examples
michaelbrewer Mar 10, 2022
b987c5a
docs(metrics): Extract examples
michaelbrewer Mar 10, 2022
41ec5e5
Merge branch 'awslabs:develop' into docs/1064
michaelbrewer Mar 10, 2022
e982eda
docs(metrics): Extract python examples
michaelbrewer Mar 11, 2022
91918e1
docs(logger): Extract examples
michaelbrewer Mar 11, 2022
99c9ac8
docs(logger): Extract examples
michaelbrewer Mar 11, 2022
ed45b4f
docs(logger): Extract examples
michaelbrewer Mar 11, 2022
7997c92
docs(logger): Extract remaining examples
michaelbrewer Mar 11, 2022
f5476e4
docs(general): correct directory structure
michaelbrewer Mar 12, 2022
21f8376
docs(logger): better filename for extracted examples
michaelbrewer Mar 13, 2022
c25ceeb
docs(logger): Better shared logger file names
michaelbrewer Mar 13, 2022
48f45c7
docs(batch): Extract code examples
michaelbrewer Mar 13, 2022
a1d5e5c
fix(batch): Correct Pydantic integration examples
michaelbrewer Mar 13, 2022
d57aff6
docs(batch): Correct Accessing processed messages examples
michaelbrewer Mar 13, 2022
e25f427
fix(batch): Fix Extending BatchProcessor example
michaelbrewer Mar 13, 2022
51d4e85
fix(docs): fix Creating a custom batch processor
michaelbrewer Mar 13, 2022
37372f4
fix(docs): batch Testing your code
michaelbrewer Mar 13, 2022
279780f
fix(docs): Fix Customizing boto configuration
michaelbrewer Mar 13, 2022
45b2491
docs(parameters): Extract examples and fixes
michaelbrewer Mar 13, 2022
9b96fc3
docs(parameter): Extract more examples
michaelbrewer Mar 13, 2022
e17d0a3
doc(parameter): extract examples
michaelbrewer Mar 13, 2022
bb2ee28
fix(docs): Fix template yml
michaelbrewer Mar 13, 2022
aa2292e
docs(idempotency): Add bring your own examples
michaelbrewer Mar 13, 2022
361fd7d
fix(idempotency): Python syntax error
michaelbrewer Mar 13, 2022
6215b20
fix(docs): Fix syntax error
michaelbrewer Mar 13, 2022
10c1cd5
fix(docs): Fix syntax error
michaelbrewer Mar 13, 2022
bd86cec
docs(idempotency): Extract code examples
michaelbrewer Mar 13, 2022
1d73f9b
docs(feature_flags): Extract code examples
michaelbrewer Mar 14, 2022
8a62e55
docs(index): Extract and fix examples
michaelbrewer Mar 14, 2022
2c8e7a7
docs(parser): Extract examples and fixes
michaelbrewer Mar 14, 2022
78c1d7d
docs(parser): Extract examples
michaelbrewer Mar 14, 2022
4e00e14
docs(parser): Extract examples
michaelbrewer Mar 14, 2022
b1bf128
docs(index): Extract and fix sam templates
michaelbrewer Mar 14, 2022
2055d66
Merge branch 'awslabs:develop' into docs/1064
michaelbrewer Mar 16, 2022
edb858b
Merge branch 'develop' into docs/1064
michaelbrewer Mar 18, 2022
65d4ea3
chore: sync up with latest version
michaelbrewer Mar 18, 2022
670d24f
Merge branch 'develop' into docs/1064
michaelbrewer Apr 2, 2022
5bd7668
chore: synup lambda version
michaelbrewer Apr 2, 2022
c8379a6
chore: bump powertools version
michaelbrewer Apr 2, 2022
041af09
Merge branch 'awslabs:develop' into docs/1064
michaelbrewer Apr 5, 2022
99f2bca
Merge branch 'awslabs:develop' into docs/1064
michaelbrewer Apr 5, 2022
342ce63
Merge branch 'awslabs:develop' into docs/1064
michaelbrewer Apr 8, 2022
146577e
Merge branch 'develop' into docs/1064
michaelbrewer Apr 9, 2022
6f9ab90
chore: update layer version
michaelbrewer Apr 9, 2022
49329f8
Merge branch 'awslabs:develop' into docs/1064
michaelbrewer Apr 12, 2022
51ddc84
Merge branch 'awslabs:develop' into docs/1064
michaelbrewer Apr 12, 2022
9d0f3ee
fix(docs): Add final missing code and fixes
michaelbrewer Apr 12, 2022
44f01ba
chore: update hl_lines
michaelbrewer Apr 12, 2022
8c54499
chore: remove extra ws
michaelbrewer Apr 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
739 changes: 53 additions & 686 deletions docs/core/event_handler/api_gateway.md

Large diffs are not rendered by default.

407 changes: 23 additions & 384 deletions docs/core/event_handler/appsync.md

Large diffs are not rendered by default.

538 changes: 30 additions & 508 deletions docs/tutorial/index.md

Large diffs are not rendered by default.

509 changes: 37 additions & 472 deletions docs/utilities/data_classes.md

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions docs_examples/core/api_gateway/app_alb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import ALBResolver
from aws_lambda_powertools.logging import correlation_paths

tracer = Tracer()
logger = Logger()
app = ALBResolver()


@app.get("/hello")
@tracer.capture_method
def get_hello_universe():
return {"message": "hello universe"}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPLICATION_LOAD_BALANCER)
@tracer.capture_lambda_handler
def lambda_handler(event, context):
return app.resolve(event, context)
16 changes: 16 additions & 0 deletions docs_examples/core/api_gateway/app_binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
from pathlib import Path

from aws_lambda_powertools.event_handler.api_gateway import APIGatewayRestResolver, Response

app = APIGatewayRestResolver()
logo_file: bytes = Path(os.getenv("LAMBDA_TASK_ROOT") + "/logo.svg").read_bytes()


@app.get("/logo")
def get_logo():
return Response(status_code=200, content_type="image/svg+xml", body=logo_file)


def lambda_handler(event, context):
return app.resolve(event, context)
12 changes: 12 additions & 0 deletions docs_examples/core/api_gateway/app_catch_all_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from aws_lambda_powertools.event_handler import APIGatewayRestResolver

app = APIGatewayRestResolver()


@app.get(".+")
def catch_any_route_after_any():
return {"path_received": app.current_event.path}


def lambda_handler(event, context):
return app.resolve(event, context)
12 changes: 12 additions & 0 deletions docs_examples/core/api_gateway/app_compress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from aws_lambda_powertools.event_handler import APIGatewayRestResolver

app = APIGatewayRestResolver()


@app.get("/hello", compress=True)
def get_hello_you():
return {"message": "hello universe"}


def lambda_handler(event, context):
return app.resolve(event, context)
28 changes: 28 additions & 0 deletions docs_examples/core/api_gateway/app_cors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler.api_gateway import APIGatewayRestResolver, CORSConfig
from aws_lambda_powertools.logging import correlation_paths

tracer = Tracer()
logger = Logger()

cors_config = CORSConfig(allow_origin="https://example.com", max_age=300)
app = APIGatewayRestResolver(cors=cors_config)


@app.get("/hello/<name>")
@tracer.capture_method
def get_hello_you(name):
return {"message": f"hello {name}"}


@app.get("/hello", cors=False) # optionally exclude CORS from response, if needed
@tracer.capture_method
def get_hello_no_cors_needed():
return {"message": "hello, no CORS needed for this path ;)"}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
def lambda_handler(event, context):
return app.resolve(event, context)
19 changes: 19 additions & 0 deletions docs_examples/core/api_gateway/app_custom_domain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging import correlation_paths

tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver(strip_prefixes=["/payment"])


@app.get("/subscriptions/<subscription>")
@tracer.capture_method
def get_subscription(subscription):
return {"subscription_id": subscription}


@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
def lambda_handler(event, context):
return app.resolve(event, context)
44 changes: 44 additions & 0 deletions docs_examples/core/api_gateway/app_custom_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import json
from enum import Enum
from json import JSONEncoder
from typing import Dict

from aws_lambda_powertools.event_handler import APIGatewayRestResolver


class CustomEncoder(JSONEncoder):
"""Your customer json encoder"""

def default(self, obj):
if isinstance(obj, Enum):
return obj.value
try:
iterable = iter(obj)
except TypeError:
pass
else:
return sorted(iterable)
return JSONEncoder.default(self, obj)


def custom_serializer(obj) -> str:
"""Your custom serializer function APIGatewayRestResolver will use"""
return json.dumps(obj, cls=CustomEncoder)


# Assigning your custom serializer
app = APIGatewayRestResolver(serializer=custom_serializer)


class Color(Enum):
RED = 1
BLUE = 2


@app.get("/colors")
def get_color() -> Dict:
return {
# Color.RED will be serialized to 1 as expected now
"color": Color.RED,
"variations": {"light", "dark"},
}
12 changes: 12 additions & 0 deletions docs_examples/core/api_gateway/app_debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from aws_lambda_powertools.event_handler import APIGatewayRestResolver

app = APIGatewayRestResolver(debug=True)


@app.get("/hello")
def get_hello_universe():
return {"message": "hello universe"}


def lambda_handler(event, context):
return app.resolve(event, context)
20 changes: 20 additions & 0 deletions docs_examples/core/api_gateway/app_dynamic_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging import correlation_paths

tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver()


@app.get("/hello/<name>")
@tracer.capture_method
def get_hello_you(name):
return {"message": f"hello {name}"}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
def lambda_handler(event, context):
return app.resolve(event, context)
35 changes: 35 additions & 0 deletions docs_examples/core/api_gateway/app_exception_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import content_types
from aws_lambda_powertools.event_handler.api_gateway import APIGatewayRestResolver, Response
from aws_lambda_powertools.logging import correlation_paths

tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver()


@app.exception_handler(ValueError)
def handle_value_error(ex: ValueError):
metadata = {"path": app.current_event.path}
logger.error(f"Malformed request: {ex}", extra=metadata)

return Response(
status_code=400,
content_type=content_types.TEXT_PLAIN,
body="Invalid request",
)


@app.get("/hello")
@tracer.capture_method
def hello_name():
name = app.current_event.get_query_string_value(name="name")
if name is not None:
raise ValueError("name query string must be present")
return {"message": f"hello {name}"}


@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
def lambda_handler(event, context):
return app.resolve(event, context)
15 changes: 15 additions & 0 deletions docs_examples/core/api_gateway/app_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from aws_lambda_powertools.event_handler import APIGatewayRestResolver

app = APIGatewayRestResolver()


@app.get("/hello")
def get_hello_you():
headers_as_dict = app.current_event.headers
name = app.current_event.get_header_value(name="X-Name", default_value="")

return {"message": f"hello {name}"}


def lambda_handler(event, context):
return app.resolve(event, context)
20 changes: 20 additions & 0 deletions docs_examples/core/api_gateway/app_http_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayHttpResolver
from aws_lambda_powertools.logging import correlation_paths

tracer = Tracer()
logger = Logger()
app = APIGatewayHttpResolver()


@app.get("/hello")
@tracer.capture_method
def get_hello_universe():
return {"message": "hello universe"}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_HTTP)
@tracer.capture_lambda_handler
def lambda_handler(event, context):
return app.resolve(event, context)
51 changes: 51 additions & 0 deletions docs_examples/core/api_gateway/app_http_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.event_handler.exceptions import (
BadRequestError,
InternalServerError,
NotFoundError,
ServiceError,
UnauthorizedError,
)
from aws_lambda_powertools.logging import correlation_paths

tracer = Tracer()
logger = Logger()

app = APIGatewayRestResolver()


@app.get(rule="/bad-request-error")
def bad_request_error():
# HTTP 400
raise BadRequestError("Missing required parameter")


@app.get(rule="/unauthorized-error")
def unauthorized_error():
# HTTP 401
raise UnauthorizedError("Unauthorized")


@app.get(rule="/not-found-error")
def not_found_error():
# HTTP 404
raise NotFoundError


@app.get(rule="/internal-server-error")
def internal_server_error():
# HTTP 500
raise InternalServerError("Internal server error")


@app.get(rule="/service-error", cors=True)
def service_error():
raise ServiceError(502, "Something went wrong!")
# alternatively
# from http import HTTPStatus
# raise ServiceError(HTTPStatus.BAD_GATEWAY.value, "Something went wrong)


def handler(event, context):
return app.resolve(event, context)
22 changes: 22 additions & 0 deletions docs_examples/core/api_gateway/app_http_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging import correlation_paths

tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver()


# Only POST HTTP requests to the path /hello will route to this function
@app.post("/hello")
@tracer.capture_method
def get_hello_you():
name = app.current_event.json_body.get("name")
return {"message": f"hello {name}"}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
def lambda_handler(event, context):
return app.resolve(event, context)
22 changes: 22 additions & 0 deletions docs_examples/core/api_gateway/app_multi_http_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging import correlation_paths

tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver()


# PUT and POST HTTP requests to the path /hello will route to this function
@app.route("/hello", method=["PUT", "POST"])
@tracer.capture_method
def get_hello_you():
name = app.current_event.json_body.get("name")
return {"message": f"hello {name}"}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
def lambda_handler(event, context):
return app.resolve(event, context)
20 changes: 20 additions & 0 deletions docs_examples/core/api_gateway/app_nested_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging import correlation_paths

tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver()


@app.get("/<message>/<name>")
@tracer.capture_method
def get_message(message, name):
return {"message": f"{message}, {name}"}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
def lambda_handler(event, context):
return app.resolve(event, context)
Loading