Skip to content

Commit 634b347

Browse files
author
Michael Brewer
committed
docs: add placeholder docs for new event handler
1 parent 3ffb5de commit 634b347

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
title: API Gateway
3+
description: Core utility
4+
---
5+
6+
Event handler for AWS API Gateway and Application Loader Balancers.
7+
8+
### Key Features
9+
10+
* Routes - `@app.get("/foo")`
11+
* Path expressions - `@app.delete("/delete/<uid>")`
12+
* Cors - `@app.post("/make_foo", cors=True)` or via `CORSConfig` and builtin CORS preflight route
13+
* Base64 encode binary - `@app.get("/logo.png")`
14+
* Gzip Compression - `@app.get("/large-json", compression=True)`
15+
* Cache-control - `@app.get("/foo", cache_control="max-age=600")`
16+
* Rest API simplification with function returns a Dict
17+
* Support function returns a Response object which give fine-grained control of the headers
18+
* JSON encoding of Decimals
19+
20+
21+
### All in one example
22+
23+
> TODO - Break on into smaller examples
24+
25+
=== "app.py"
26+
27+
```python
28+
from decimal import Decimal
29+
import json
30+
from typing import Dict, Tuple
31+
32+
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent
33+
from aws_lambda_powertools.event_handler.api_gateway import (
34+
ApiGatewayResolver,
35+
CORSConfig,
36+
ProxyEventType,
37+
Response,
38+
)
39+
40+
# Other supported proxy_types: "APIGatewayProxyEvent", "APIGatewayProxyEventV2", "ALBEvent"
41+
app = ApiGatewayResolver(
42+
proxy_type=ProxyEventType.http_api_v1,
43+
cors=CORSConfig(
44+
allow_origin="https://www.example.com/",
45+
expose_headers=["x-exposed-response-header"],
46+
allow_headers=["x-custom-request-header"],
47+
max_age=100,
48+
allow_credentials=True,
49+
)
50+
)
51+
52+
@app.get("/foo", compress=True)
53+
def get_foo() -> Tuple[int, str, str]:
54+
# Matches on http GET and proxy path "/foo"
55+
# and return status code: 200, content-type: text/html and body: Hello
56+
return 200, "text/html", "Hello"
57+
58+
@app.get("/logo.png")
59+
def get_logo() -> Tuple[int, str, bytes]:
60+
# Base64 encodes the return bytes body automatically
61+
logo: bytes = load_logo()
62+
return 200, "image/png", logo
63+
64+
@app.post("/make_foo", cors=True)
65+
def make_foo() -> Tuple[int, str, str]:
66+
# Matches on http POST and proxy path "/make_foo"
67+
post_data: dict = app. current_event.json_body
68+
return 200, "application/json", json.dumps(post_data["value"])
69+
70+
@app.delete("/delete/<uid>")
71+
def delete_foo(uid: str) -> Tuple[int, str, str]:
72+
# Matches on http DELETE and proxy path starting with "/delete/"
73+
assert isinstance(app.current_event, APIGatewayProxyEvent)
74+
assert app.current_event.request_context.authorizer.claims is not None
75+
assert app.current_event.request_context.authorizer.claims["username"] == "Mike"
76+
return 200, "application/json", json.dumps({"id": uid})
77+
78+
@app.get("/hello/<username>")
79+
def hello_user(username: str) -> Tuple[int, str, str]:
80+
return 200, "text/html", f"Hello {username}!"
81+
82+
@app.get("/rest")
83+
def rest_fun() -> Dict:
84+
# Returns a statusCode: 200, Content-Type: application/json and json.dumps dict
85+
# and handles the serialization of decimals to json string
86+
return {"message": "Example", "second": Decimal("100.01")}
87+
88+
@app.get("/foo3")
89+
def foo3() -> Response:
90+
return Response(
91+
status_code=200,
92+
content_type="application/json",
93+
headers={"custom-header": "value"},
94+
body=json.dumps({"message": "Foo3"}),
95+
)
96+
```

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ nav:
1414
- core/metrics.md
1515
- Event Handler:
1616
- core/event_handler/appsync.md
17+
- core/event_handler/api_gateway.md
1718
- Utilities:
1819
- utilities/middleware_factory.md
1920
- utilities/parameters.md

0 commit comments

Comments
 (0)