bedrock.endpoints.decorators.apply_path_param_to_body
1import functools # pragma: unit 2import json # pragma: unit 3 4 5def apply_path_param_to_body(param_name: str, body_key: str = None): # pragma: unit 6 """ 7 Automatically add a path parameter to the body of the request. 8 9 :param param_name: The name of the path parameter 10 :param body_key: The name of the key that the path parameter should have 11 12 Example usage: for a `POST` to `/countries/{countryUuid}/regions` where the payload must include the country's UUID: 13 ```python 14 class Regions(Endpoint): 15 # ... 16 @apply_path_param_to_body("countryUuid") 17 def post_global(self, event): 18 body = json.loads(event["body"]) 19 assert body["countryUuid"] === event["pathParameters"]["countryUuid"] 20 # ... 21 ``` 22 """ 23 key = body_key if body_key else param_name 24 25 def decorator(func): 26 @functools.wraps(func) 27 def wrapper(*args, **kwargs): 28 event = args[1] 29 body = json.loads(event["body"]) 30 body[key] = event["pathParameters"][param_name] if param_name in event["pathParameters"] else None 31 event["body"] = json.dumps(body) 32 return func(*[args[0], event, *args[2:]], **kwargs) 33 34 return wrapper 35 36 return decorator
def
apply_path_param_to_body(param_name: str, body_key: str = None):
6def apply_path_param_to_body(param_name: str, body_key: str = None): # pragma: unit 7 """ 8 Automatically add a path parameter to the body of the request. 9 10 :param param_name: The name of the path parameter 11 :param body_key: The name of the key that the path parameter should have 12 13 Example usage: for a `POST` to `/countries/{countryUuid}/regions` where the payload must include the country's UUID: 14 ```python 15 class Regions(Endpoint): 16 # ... 17 @apply_path_param_to_body("countryUuid") 18 def post_global(self, event): 19 body = json.loads(event["body"]) 20 assert body["countryUuid"] === event["pathParameters"]["countryUuid"] 21 # ... 22 ``` 23 """ 24 key = body_key if body_key else param_name 25 26 def decorator(func): 27 @functools.wraps(func) 28 def wrapper(*args, **kwargs): 29 event = args[1] 30 body = json.loads(event["body"]) 31 body[key] = event["pathParameters"][param_name] if param_name in event["pathParameters"] else None 32 event["body"] = json.dumps(body) 33 return func(*[args[0], event, *args[2:]], **kwargs) 34 35 return wrapper 36 37 return decorator
Automatically add a path parameter to the body of the request.
Parameters
- param_name: The name of the path parameter
- body_key: The name of the key that the path parameter should have
Example usage: for a POST to /countries/{countryUuid}/regions where the payload must include the country's UUID:
class Regions(Endpoint):
# ...
@apply_path_param_to_body("countryUuid")
def post_global(self, event):
body = json.loads(event["body"])
assert body["countryUuid"] === event["pathParameters"]["countryUuid"]
# ...