def wrapped(*args, **kwargs): try: requested_limit = int(request.args.get('n', _MAX_RESULTS_PER_PAGE)) except ValueError: requested_limit = 0 limit = max(min(requested_limit, _MAX_RESULTS_PER_PAGE), 1) next_page_token = request.args.get('next_page', request.args.get('last', None)) # Decrypt the next page token, if any. start_id = None page_info = decrypt_page_token(next_page_token) if page_info is not None: start_id = page_info.get('start_id', None) def callback(results, response): if len(results) <= limit: return next_page_token = encrypt_page_token({'start_id': max([obj.id for obj in results])}) link_url = os.path.join(get_app_url(), url_for(request.endpoint, **request.view_args)) link_param = urlencode({'n': limit, 'next_page': next_page_token}) link = '<%s?%s>; rel="next"' % (link_url, link_param) response.headers['Link'] = link kwargs[limit_kwarg_name] = limit kwargs[start_id_kwarg_name] = start_id kwargs[callback_kwarg_name] = callback return func(*args, **kwargs)
def wrapper(self, *args, **kwargs): # Note: if page_token is None, we'll receive the first page of results back. page_token = decrypt_page_token(kwargs[parsed_args_kwarg]["next_page"]) kwargs[page_token_kwarg] = page_token (result, next_page_token) = func(self, *args, **kwargs) if next_page_token is not None: result["next_page"] = encrypt_page_token(next_page_token) return result