Refactor how parsed_args are passed to methods
This commit is contained in:
parent
daab1b3964
commit
018bf8c5ad
13 changed files with 142 additions and 137 deletions
|
@ -212,52 +212,55 @@ def query_param(name, help_str, type=reqparse.text_type, default=None,
|
|||
return func
|
||||
return add_param
|
||||
|
||||
def page_support(page_token_kwarg='page_token', parsed_args_kwarg='parsed_args'):
|
||||
def inner(func):
|
||||
""" Adds pagination support to an API endpoint. The decorated API will have an
|
||||
added query parameter named 'next_page'. Works in tandem with the
|
||||
modelutil paginate method.
|
||||
"""
|
||||
@wraps(func)
|
||||
@query_param('next_page', 'The page token for the next page', type=str)
|
||||
def wrapper(self, *args, **kwargs):
|
||||
page_token = None
|
||||
|
||||
def page_support(func):
|
||||
""" Adds pagination support to an API endpoint. The decorated API will have an
|
||||
added query parameter named 'next_page'. Works in tandem with the
|
||||
modelutil paginate method.
|
||||
"""
|
||||
@wraps(func)
|
||||
@query_param('next_page', 'The page token for the next page', type=str)
|
||||
def wrapper(self, query_args, *args, **kwargs):
|
||||
page_token = None
|
||||
if kwargs[parsed_args_kwarg]['next_page']:
|
||||
# Decrypt the page token.
|
||||
unencrypted = decrypt_string(kwargs[parsed_args_kwarg]['next_page'],
|
||||
app.config['PAGE_TOKEN_KEY'],
|
||||
ttl=_PAGE_TOKEN_TTL)
|
||||
if unencrypted is not None:
|
||||
try:
|
||||
page_token = json.loads(unencrypted)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if query_args['next_page']:
|
||||
# Decrypt the page token.
|
||||
unencrypted = decrypt_string(query_args['next_page'], app.config['PAGE_TOKEN_KEY'],
|
||||
ttl=_PAGE_TOKEN_TTL)
|
||||
if unencrypted is not None:
|
||||
try:
|
||||
page_token = json.loads(unencrypted)
|
||||
except ValueError:
|
||||
pass
|
||||
# Note: if page_token is None, we'll receive the first page of results back.
|
||||
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_string(json.dumps(next_page_token),
|
||||
app.config['PAGE_TOKEN_KEY'])
|
||||
|
||||
# Note: if page_token is None, we'll receive the first page of results back.
|
||||
(result, next_page_token) = func(self, query_args, page_token=page_token, *args, **kwargs)
|
||||
if next_page_token is not None:
|
||||
result['next_page'] = encrypt_string(json.dumps(next_page_token),
|
||||
app.config['PAGE_TOKEN_KEY'])
|
||||
return result
|
||||
|
||||
return result
|
||||
return wrapper
|
||||
return inner
|
||||
|
||||
return wrapper
|
||||
def parse_args(kwarg_name='parsed_args'):
|
||||
def inner(func):
|
||||
@wraps(func)
|
||||
def wrapper(self, *args, **kwargs):
|
||||
if '__api_query_params' not in dir(func):
|
||||
abort(500)
|
||||
|
||||
parser = reqparse.RequestParser()
|
||||
for arg_spec in func.__api_query_params:
|
||||
parser.add_argument(**arg_spec)
|
||||
kwargs[kwarg_name] = parser.parse_args()
|
||||
|
||||
def parse_args(func):
|
||||
@wraps(func)
|
||||
def wrapper(self, *args, **kwargs):
|
||||
if '__api_query_params' not in dir(func):
|
||||
abort(500)
|
||||
|
||||
parser = reqparse.RequestParser()
|
||||
for arg_spec in func.__api_query_params:
|
||||
parser.add_argument(**arg_spec)
|
||||
parsed_args = parser.parse_args()
|
||||
|
||||
return func(self, parsed_args, *args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
return func(self, *args, **kwargs)
|
||||
return wrapper
|
||||
return inner
|
||||
|
||||
def parse_repository_name(func):
|
||||
@wraps(func)
|
||||
|
|
Reference in a new issue