bedrock.endpoints.decorators.query_params.pagination

 1import functools  # pragma: unit
 2from bedrock.endpoints.decorators.query_params.common import extract_query_parameter  # pragma: unit
 3from bedrock.endpoints.dto.bedrock_response import resolve_response  # pragma: unit
 4from bedrock.meta.function import get_argument_defaults  # pragma: unit
 5
 6
 7def paginated():  # pragma: unit
 8    """
 9    Decorator to add pagination to the endpoint's function's kwargs.
10    This essentially does the same as:
11    ```python
12    @with_query_param("sort_order", str)
13    @with_query_param("sort_column", str)
14    @with_query_param("offset", int)
15    @with_query_param("limit", int)
16    ```
17
18    Example usage: to get the "2nd page" of an endpoint being limited to 10 items: `GET` to `/countries/limit=10&offset=10`:
19    ```python
20    class Countries(Endpoint):
21        # ...
22        @paginated()
23        def get_global(self, event, sort_order, sort_column, offset, limit):
24            # do some stuff...
25            return self.get_global_generic(event, Country,
26                order_by=sort_column,
27                order=sort_order,
28                offset=offset,
29                limit=limit
30            )
31    ```
32    """
33    params = [("sort_order", str), ("sort_column", str), ("offset", int), ("limit", int)]
34
35    def decorator(func):
36        @functools.wraps(func)
37        def wrapper(*args, **kwargs):
38            arguments = {}
39            event = args[1]
40            for param_name, param_type in params:
41                arguments.update(extract_query_parameter(event, param_name, param_type, param_name))
42            code, response = func(*args, **{**kwargs, **arguments})
43            response = resolve_response(response, event, get_argument_defaults(func), arguments)
44            return code, response
45
46        return wrapper
47
48    return decorator
def paginated():
 8def paginated():  # pragma: unit
 9    """
10    Decorator to add pagination to the endpoint's function's kwargs.
11    This essentially does the same as:
12    ```python
13    @with_query_param("sort_order", str)
14    @with_query_param("sort_column", str)
15    @with_query_param("offset", int)
16    @with_query_param("limit", int)
17    ```
18
19    Example usage: to get the "2nd page" of an endpoint being limited to 10 items: `GET` to `/countries/limit=10&offset=10`:
20    ```python
21    class Countries(Endpoint):
22        # ...
23        @paginated()
24        def get_global(self, event, sort_order, sort_column, offset, limit):
25            # do some stuff...
26            return self.get_global_generic(event, Country,
27                order_by=sort_column,
28                order=sort_order,
29                offset=offset,
30                limit=limit
31            )
32    ```
33    """
34    params = [("sort_order", str), ("sort_column", str), ("offset", int), ("limit", int)]
35
36    def decorator(func):
37        @functools.wraps(func)
38        def wrapper(*args, **kwargs):
39            arguments = {}
40            event = args[1]
41            for param_name, param_type in params:
42                arguments.update(extract_query_parameter(event, param_name, param_type, param_name))
43            code, response = func(*args, **{**kwargs, **arguments})
44            response = resolve_response(response, event, get_argument_defaults(func), arguments)
45            return code, response
46
47        return wrapper
48
49    return decorator

Decorator to add pagination to the endpoint's function's kwargs. This essentially does the same as:

@with_query_param("sort_order", str)
@with_query_param("sort_column", str)
@with_query_param("offset", int)
@with_query_param("limit", int)

Example usage: to get the "2nd page" of an endpoint being limited to 10 items: GET to /countries/limit=10&offset=10:

class Countries(Endpoint):
    # ...
    @paginated()
    def get_global(self, event, sort_order, sort_column, offset, limit):
        # do some stuff...
        return self.get_global_generic(event, Country,
            order_by=sort_column,
            order=sort_order,
            offset=offset,
            limit=limit
        )