Skip to content

Commit 241c587

Browse files
committed
docs: fix relative imports, spacing
Signed-off-by: heitorlessa <[email protected]>
1 parent dcab235 commit 241c587

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

docs/core/event_handler/api_gateway.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -872,16 +872,16 @@ Let's assume you have `app.py` as your Lambda function entrypoint and routes in
872872

873873
logger = Logger(child=True)
874874
router = Router()
875-
876875
USERS = {"user1": "details_here", "user2": "details_here", "user3": "details_here"}
877876

877+
878878
@router.get("/users")
879879
def get_users() -> Dict:
880-
# get query string ?limit=10
880+
# /users?limit=1
881881
pagination_limit = router.current_event.get_query_string_value(name="limit", default_value=10)
882882

883883
logger.info(f"Fetching the first {pagination_limit} users...")
884-
ret = dict(itertools.islice(USERS.items(), pagination_limit))
884+
ret = dict(itertools.islice(USERS.items(), int(pagination_limit)))
885885
return {"items": [ret]}
886886

887887
@router.get("/users/<username>")
@@ -909,14 +909,14 @@ Let's assume you have `app.py` as your Lambda function entrypoint and routes in
909909

910910

911911
def lambda_handler(event: Dict, context: LambdaContext):
912-
app.resolve(event, context)
912+
return app.resolve(event, context)
913913
```
914914

915915
#### Route prefix
916916

917-
As routes are now split in their own files, you can optionally instruct `Router` to inject a prefix for all routes during registration.
917+
In the previous example, `users.py` routes had a `/users` prefix. This might grow over time and become repetitive.
918918

919-
In the previous example, `users.py` routes had a `/users` prefix. We could remove `/users` from all route definitions, and then set `include_router(users.router, prefix="/users")` in the `app.py`.
919+
When necessary, you can set a prefix when including a router object. This means you could remove `/users` prefix in `users.py` altogether.
920920

921921
=== "app.py"
922922

@@ -931,8 +931,9 @@ In the previous example, `users.py` routes had a `/users` prefix. We could remov
931931
app = ApiGatewayResolver()
932932
app.include_router(users.router, prefix="/users") # prefix '/users' to any route in `users.router`
933933

934+
934935
def lambda_handler(event: Dict, context: LambdaContext):
935-
app.resolve(event, context)
936+
return app.resolve(event, context)
936937
```
937938

938939
=== "users.py"
@@ -945,8 +946,8 @@ In the previous example, `users.py` routes had a `/users` prefix. We could remov
945946

946947
logger = Logger(child=True)
947948
router = Router()
949+
USERS = {"user1": "details", "user2": "details", "user3": "details"}
948950

949-
USERS = {"user1": "details_here", "user2": "details_here", "user3": "details_here"}
950951

951952
@router.get("/") # /users, when we set the prefix in app.py
952953
def get_users() -> Dict:
@@ -961,7 +962,9 @@ In the previous example, `users.py` routes had a `/users` prefix. We could remov
961962

962963
#### Sample larger layout
963964

964-
Below is an example project layout where we have Users routes similar to the previous example, and health check route - We use ALB to demonstrate the UX remains the same.
965+
!!! info "We use ALB to demonstrate that the UX remains the same"
966+
967+
Below is an example project layout where we have Users routes similar to the previous example, and health check route.
965968

966969
Note that this layout optimizes for code sharing and for those familiar with Python modules. This means multiple functions will share the same `CodeUri` and package, though they are only built once.
967970

@@ -1026,7 +1029,7 @@ Note that this layout optimizes for code sharing and for those familiar with Pyt
10261029
HealthPath:
10271030
Type: Api
10281031
Properties:
1029-
Path: /health/status
1032+
Path: /status
10301033
Method: GET
10311034
UserPath:
10321035
Type: Api
@@ -1055,22 +1058,22 @@ Note that this layout optimizes for code sharing and for those familiar with Pyt
10551058
from aws_lambda_powertools import Logger, Tracer
10561059
from aws_lambda_powertools.event_handler import ApiGatewayResolver
10571060
from aws_lambda_powertools.event_handler.api_gateway import ProxyEventType
1058-
from aws_lambda_powertools.logging.correlation_paths import API_GATEWAY_HTTP
1061+
from aws_lambda_powertools.logging.correlation_paths import ALB
10591062
from aws_lambda_powertools.utilities.typing import LambdaContext
10601063

1061-
from .routers import health, users
1064+
from routers import health, users
10621065

10631066
tracer = Tracer()
10641067
logger = Logger()
10651068
app = ApiGatewayResolver(proxy_type=ProxyEventType.ALBEvent)
1066-
app.include_router(health.router, prefix="/health")
1069+
app.include_router(health.router)
10671070
app.include_router(users.router)
10681071

10691072

1070-
@logger.inject_lambda_context(correlation_id_path=API_GATEWAY_HTTP)
1073+
@logger.inject_lambda_context(correlation_id_path=ALB)
10711074
@tracer.capture_lambda_handler
10721075
def lambda_handler(event: Dict, context: LambdaContext):
1073-
app.resolve(event, context)
1076+
return app.resolve(event, context)
10741077
```
10751078

10761079
=== "src/app/routers/health.py"

0 commit comments

Comments
 (0)