@@ -48,21 +48,21 @@ Here's an example on how we can handle the `/todos` path.
48
48
???+ info
49
49
We automatically serialize ` Dict ` responses as JSON, trim whitespace for compact responses, and set content-type to ` application/json ` .
50
50
51
- === "app .py"
51
+ === "getting_started_rest_api_resolver .py"
52
52
53
53
```python hl_lines="5 11 14 28"
54
54
--8<-- "examples/event_handler_rest/src/getting_started_rest_api_resolver.py"
55
55
```
56
56
57
- === "Request "
57
+ === "getting_started_rest_api_resolver.json "
58
58
59
59
This utility uses `path` and `httpMethod` to route to the right function. This helps make unit tests and local invocation easier too.
60
60
61
61
```json hl_lines="4-5"
62
62
--8<-- "examples/event_handler_rest/src/getting_started_rest_api_resolver.json"
63
63
```
64
64
65
- === "Response "
65
+ === "getting_started_rest_api_resolver_output.json "
66
66
67
67
```json
68
68
--8<-- "examples/event_handler_rest/src/getting_started_rest_api_resolver_output.json"
@@ -96,13 +96,13 @@ Each dynamic route you set must be part of your function signature. This allows
96
96
???+ note
97
97
For brevity, we will only include the necessary keys for each sample request for the example to work.
98
98
99
- === "app .py"
99
+ === "dynamic_routes .py"
100
100
101
101
```python hl_lines="14 16"
102
102
--8<-- "examples/event_handler_rest/src/dynamic_routes.py"
103
103
```
104
104
105
- === "Request "
105
+ === "dynamic_routes.json "
106
106
107
107
```json
108
108
--8<-- "examples/event_handler_rest/src/dynamic_routes.json"
@@ -123,13 +123,13 @@ You can also combine nested paths with greedy regex to catch in between routes.
123
123
???+ warning
124
124
We choose the most explicit registered route that matches an incoming event.
125
125
126
- === "app .py"
126
+ === "dynamic_routes_catch_all .py"
127
127
128
128
```python hl_lines="11"
129
129
--8<-- "examples/event_handler_rest/src/dynamic_routes_catch_all.py"
130
130
```
131
131
132
- === "Request "
132
+ === "dynamic_routes_catch_all.json "
133
133
134
134
```json
135
135
--8<-- "examples/event_handler_rest/src/dynamic_routes_catch_all.json"
@@ -139,13 +139,13 @@ You can also combine nested paths with greedy regex to catch in between routes.
139
139
140
140
You can use named decorators to specify the HTTP method that should be handled in your functions. That is, ` app.<http_method> ` , where the HTTP method could be ` get ` , ` post ` , ` put ` , ` patch ` , ` delete ` , and ` options ` .
141
141
142
- === "app .py"
142
+ === "http_methods .py"
143
143
144
144
```python hl_lines="14 17"
145
145
--8<-- "examples/event_handler_rest/src/http_methods.py"
146
146
```
147
147
148
- === "Request "
148
+ === "http_methods.json "
149
149
150
150
```json
151
151
--8<-- "examples/event_handler_rest/src/http_methods.json"
@@ -225,13 +225,13 @@ When using [Custom Domain API Mappings feature](https://docs.aws.amazon.com/apig
225
225
226
226
To address this API Gateway behavior, we use ` strip_prefixes ` parameter to account for these prefixes that are now injected into the path regardless of which type of API Gateway you're using.
227
227
228
- === "app .py"
228
+ === "custom_api_mapping .py"
229
229
230
230
```python hl_lines="8"
231
231
--8<-- "examples/event_handler_rest/src/custom_api_mapping.py"
232
232
```
233
233
234
- === "Request "
234
+ === "custom_api_mapping.json "
235
235
236
236
```json
237
237
--8<-- "examples/event_handler_rest/src/custom_api_mapping.json"
@@ -253,13 +253,13 @@ This will ensure that CORS headers are always returned as part of the response w
253
253
???+ tip
254
254
Optionally disable CORS on a per path basis with ` cors=False ` parameter.
255
255
256
- === "app .py"
256
+ === "setting_cors .py"
257
257
258
258
```python hl_lines="5 11-12 34"
259
259
--8<-- "examples/event_handler_rest/src/setting_cors.py"
260
260
```
261
261
262
- === "Response "
262
+ === "setting_cors_output.json "
263
263
264
264
```json
265
265
--8<-- "examples/event_handler_rest/src/setting_cors_output.json"
@@ -290,13 +290,13 @@ For convenience, these are the default values when using `CORSConfig` to enable
290
290
291
291
You can use the ` Response ` class to have full control over the response, for example you might want to add additional headers or set a custom Content-type.
292
292
293
- === "app .py"
293
+ === "fine_grained_responses .py"
294
294
295
295
```python hl_lines="7 24-28"
296
296
--8<-- "examples/event_handler_rest/src/fine_grained_responses.py"
297
297
```
298
298
299
- === "Response "
299
+ === "fine_grained_responses_output.json "
300
300
301
301
```json
302
302
--8<-- "examples/event_handler_rest/src/fine_grained_responses_output.json"
@@ -309,19 +309,19 @@ You can compress with gzip and base64 encode your responses via `compress` param
309
309
???+ warning
310
310
The client must send the ` Accept-Encoding ` header, otherwise a normal response will be sent.
311
311
312
- === "app .py"
312
+ === "compressing_responses .py"
313
313
314
314
```python hl_lines="14"
315
315
--8<-- "examples/event_handler_rest/src/compressing_responses.py"
316
316
```
317
317
318
- === "Request "
318
+ === "compressing_responses.json "
319
319
320
320
```json
321
321
--8<-- "examples/event_handler_rest/src/compressing_responses.json"
322
322
```
323
323
324
- === "Response "
324
+ === "compressing_responses_output.json "
325
325
326
326
```json
327
327
--8<-- "examples/event_handler_rest/src/compressing_responses_output.json"
@@ -336,25 +336,25 @@ Like `compress` feature, the client must send the `Accept` header with the corre
336
336
???+ warning
337
337
This feature requires API Gateway to configure binary media types, see [ our sample infrastructure] ( #required-resources ) for reference.
338
338
339
- === "app .py"
339
+ === "binary_responses .py"
340
340
341
341
```python hl_lines="14 20"
342
342
--8<-- "examples/event_handler_rest/src/binary_responses.py"
343
343
```
344
344
345
- === "logo .svg"
345
+ === "binary_responses_logo .svg"
346
346
347
347
```xml
348
348
--8<-- "examples/event_handler_rest/src/binary_responses_logo.svg"
349
349
```
350
350
351
- === "Request "
351
+ === "binary_responses.json "
352
352
353
353
```json
354
354
--8<-- "examples/event_handler_rest/src/binary_responses.json"
355
355
```
356
356
357
- === "Response "
357
+ === "binary_responses_output.json "
358
358
359
359
```json
360
360
--8<-- "examples/event_handler_rest/src/binary_responses_output.json"
@@ -387,17 +387,17 @@ You can instruct API Gateway handler to use a custom serializer to best suit you
387
387
388
388
As you grow the number of routes a given Lambda function should handle, it is natural to split routes into separate files to ease maintenance - That's where the ` Router ` feature is useful.
389
389
390
- Let's assume you have ` app.py ` as your Lambda function entrypoint and routes in ` todos .py` , this is how you'd use the ` Router ` feature.
390
+ Let's assume you have ` app.py ` as your Lambda function entrypoint and routes in ` split_route_module .py` , this is how you'd use the ` Router ` feature.
391
391
392
- === "todos .py"
392
+ === "split_route_module .py"
393
393
394
394
We import **Router** instead of **APIGatewayRestResolver**; syntax wise is exactly the same.
395
395
396
396
```python hl_lines="5 13 16 25 28"
397
397
--8<-- "examples/event_handler_rest/src/split_route_module.py"
398
398
```
399
399
400
- === "app .py"
400
+ === "split_route .py"
401
401
402
402
We use `include_router` method and include all user routers registered in the `router` global object.
403
403
@@ -407,17 +407,17 @@ Let's assume you have `app.py` as your Lambda function entrypoint and routes in
407
407
408
408
#### Route prefix
409
409
410
- In the previous example, ` todos .py` routes had a ` /todos ` prefix. This might grow over time and become repetitive.
410
+ In the previous example, ` split_route_module .py` routes had a ` /todos ` prefix. This might grow over time and become repetitive.
411
411
412
- When necessary, you can set a prefix when including a router object. This means you could remove ` /todos ` prefix in ` todos.py ` altogether.
412
+ When necessary, you can set a prefix when including a router object. This means you could remove ` /todos ` prefix altogether.
413
413
414
- === "app .py"
414
+ === "split_route_prefix .py"
415
415
416
416
```python hl_lines="12"
417
417
--8<-- "examples/event_handler_rest/src/split_route_prefix.py"
418
418
```
419
419
420
- === "todos .py"
420
+ === "split_route_prefix_module .py"
421
421
422
422
```python hl_lines="13 25"
423
423
--8<-- "examples/event_handler_rest/src/split_route_prefix_module.py"
@@ -509,13 +509,13 @@ your development, building, deployment tooling need to accommodate the distinct
509
509
510
510
You can test your routes by passing a proxy event request where ` path ` and ` httpMethod ` .
511
511
512
- === "test_app .py"
512
+ === "assert_http_response .py"
513
513
514
514
```python hl_lines="21-24"
515
515
--8<-- "examples/event_handler_rest/src/assert_http_response.py"
516
516
```
517
517
518
- === "app .py"
518
+ === "assert_http_response_module .py"
519
519
520
520
```python
521
521
--8<-- "examples/event_handler_rest/src/assert_http_response_module.py"
0 commit comments