bedrock.endpoints.decorators.query_params.with_query_param
1import functools # pragma: unit 2from bedrock.endpoints.decorators.query_params.common import extract_query_parameter # pragma: unit 3 4 5def with_query_param(param_name: str, param_type: type, param_kwarg: str = None, allow_multi_value: bool = False): # pragma: unit 6 """ 7 Decorator to add a query parameter to the endpoint's function's kwargs 8 :param param_name: The name of the query parameter 9 :param param_type: The type of data the query parameter should be 10 :param param_kwarg: The name the query parameter should have when passed as a keyword argument to the function. Defaults to the same as `param_name`. 11 :param allow_multi_value: Whether multiple of the same query parameters should be allowed (e.g. ?param1[in]="abc"¶m1[in]="def". Defaults to `False`. 12 13 Example usage: for a `GET` to `/countries/?show_regions=true&sort=desc`: 14 ```python 15 class Countries(Endpoint): 16 # ... 17 @with_query_param("show_regions", bool) 18 @with_query_param("sort", str) 19 @with_query_param("example[weird]param", str, "example_weird_param") 20 def get_global(self, event, show_regions, sort, example_weird_param): 21 # ... 22 ``` 23 """ 24 keyword_argument = param_kwarg if param_kwarg is not None else param_name 25 26 def decorator(func): 27 @functools.wraps(func) 28 def wrapper(*args, **kwargs): 29 event = args[1] 30 arguments = extract_query_parameter(event, param_name, param_type, keyword_argument, allow_multi_value) 31 return func(*args, **{**kwargs, **arguments}) 32 33 return wrapper 34 35 return decorator
def
with_query_param( param_name: str, param_type: type, param_kwarg: str = None, allow_multi_value: bool = False):
6def with_query_param(param_name: str, param_type: type, param_kwarg: str = None, allow_multi_value: bool = False): # pragma: unit 7 """ 8 Decorator to add a query parameter to the endpoint's function's kwargs 9 :param param_name: The name of the query parameter 10 :param param_type: The type of data the query parameter should be 11 :param param_kwarg: The name the query parameter should have when passed as a keyword argument to the function. Defaults to the same as `param_name`. 12 :param allow_multi_value: Whether multiple of the same query parameters should be allowed (e.g. ?param1[in]="abc"¶m1[in]="def". Defaults to `False`. 13 14 Example usage: for a `GET` to `/countries/?show_regions=true&sort=desc`: 15 ```python 16 class Countries(Endpoint): 17 # ... 18 @with_query_param("show_regions", bool) 19 @with_query_param("sort", str) 20 @with_query_param("example[weird]param", str, "example_weird_param") 21 def get_global(self, event, show_regions, sort, example_weird_param): 22 # ... 23 ``` 24 """ 25 keyword_argument = param_kwarg if param_kwarg is not None else param_name 26 27 def decorator(func): 28 @functools.wraps(func) 29 def wrapper(*args, **kwargs): 30 event = args[1] 31 arguments = extract_query_parameter(event, param_name, param_type, keyword_argument, allow_multi_value) 32 return func(*args, **{**kwargs, **arguments}) 33 34 return wrapper 35 36 return decorator
Decorator to add a query parameter to the endpoint's function's kwargs
Parameters
- param_name: The name of the query parameter
- param_type: The type of data the query parameter should be
- param_kwarg: The name the query parameter should have when passed as a keyword argument to the function. Defaults to the same as
param_name. - allow_multi_value: Whether multiple of the same query parameters should be allowed (e.g. ?param1[in]="abc"¶m1[in]="def". Defaults to
False.
Example usage: for a GET to /countries/?show_regions=true&sort=desc:
class Countries(Endpoint):
# ...
@with_query_param("show_regions", bool)
@with_query_param("sort", str)
@with_query_param("example[weird]param", str, "example_weird_param")
def get_global(self, event, show_regions, sort, example_weird_param):
# ...