@@ -227,75 +227,75 @@ A first attempt at the routing logic might look similar to the following code sn
227227=== "app.py"
228228
229229 ```python hl_lines="4 9 13 27-29 35-36"
230- import json
230+ import json
231231
232232
233- def hello_name(event, **kargs):
234- username = event["pathParameters"]["name"]
235- return {"statusCode": 200, "body": json.dumps({"message": f"hello {username}!"})}
233+ def hello_name(event, **kargs):
234+ username = event["pathParameters"]["name"]
235+ return {"statusCode": 200, "body": json.dumps({"message": f"hello {username}!"})}
236236
237237
238- def hello(**kargs):
239- return {"statusCode": 200, "body": json.dumps({"message": "hello unknown!"})}
238+ def hello(**kargs):
239+ return {"statusCode": 200, "body": json.dumps({"message": "hello unknown!"})}
240240
241241
242- class Router:
243- def __init__(self):
244- self.routes = {}
242+ class Router:
243+ def __init__(self):
244+ self.routes = {}
245245
246- def set(self, path, method, handler):
247- self.routes[f"{path}-{method}"] = handler
246+ def set(self, path, method, handler):
247+ self.routes[f"{path}-{method}"] = handler
248248
249- def get(self, path, method):
250- try:
251- route = self.routes[f"{path}-{method}"]
252- except KeyError:
253- raise RuntimeError(f"Cannot route request to the correct method. path={path}, method={method}")
254- return route
249+ def get(self, path, method):
250+ try:
251+ route = self.routes[f"{path}-{method}"]
252+ except KeyError:
253+ raise RuntimeError(f"Cannot route request to the correct method. path={path}, method={method}")
254+ return route
255255
256- router = Router()
257- router.set(path="/hello", method="GET", handler=hello)
258- router.set(path="/hello/{name}", method="GET", handler=hello_name)
256+ router = Router()
257+ router.set(path="/hello", method="GET", handler=hello)
258+ router.set(path="/hello/{name}", method="GET", handler=hello_name)
259259
260260
261- def lambda_handler(event, context):
262- path = event["resource"]
263- http_method = event["httpMethod"]
264- method = router.get(path=path, method=http_method)
265- return method(event=event)
261+ def lambda_handler(event, context):
262+ path = event["resource"]
263+ http_method = event["httpMethod"]
264+ method = router.get(path=path, method=http_method)
265+ return method(event=event)
266266 ```
267267
268268=== "template.yaml"
269269
270270 ```yaml hl_lines="15-24"
271- AWSTemplateFormatVersion: "2010-09-09"
272- Transform: AWS::Serverless-2016-10-31
273- Description: Sample SAM Template for powertools-quickstart
274- Globals:
275- Function:
276- Timeout: 3
277- Resources:
278- HelloWorldFunction:
279- Type: AWS::Serverless::Function
280- Properties:
281- CodeUri: hello_world/
282- Handler: app.lambda_handler
283- Runtime: python3.9
284- Events:
285- HelloWorld:
286- Type: Api
287- Properties:
288- Path: /hello
289- Method: get
290- HelloWorldName:
291- Type: Api
292- Properties:
293- Path: /hello/{name}
294- Method: get
295- Outputs:
296- HelloWorldApi:
297- Description: "API Gateway endpoint URL for Prod stage for Hello World function"
298- Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
271+ AWSTemplateFormatVersion: "2010-09-09"
272+ Transform: AWS::Serverless-2016-10-31
273+ Description: Sample SAM Template for powertools-quickstart
274+ Globals:
275+ Function:
276+ Timeout: 3
277+ Resources:
278+ HelloWorldFunction:
279+ Type: AWS::Serverless::Function
280+ Properties:
281+ CodeUri: hello_world/
282+ Handler: app.lambda_handler
283+ Runtime: python3.9
284+ Events:
285+ HelloWorld:
286+ Type: Api
287+ Properties:
288+ Path: /hello
289+ Method: get
290+ HelloWorldName:
291+ Type: Api
292+ Properties:
293+ Path: /hello/{name}
294+ Method: get
295+ Outputs:
296+ HelloWorldApi:
297+ Description: "API Gateway endpoint URL for Prod stage for Hello World function"
298+ Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
299299 ```
300300
301301Let's break this down:
0 commit comments