diff --git a/pagure/api/__init__.py b/pagure/api/__init__.py index e014020..baf70f3 100644 --- a/pagure/api/__init__.py +++ b/pagure/api/__init__.py @@ -248,6 +248,52 @@ def api_method(function): return wrapper +def get_page(): + """ Returns the page value specified in the request. + Defaults to 1. + raises APIERROR.EINVALIDREQ if the page provided isn't an integer + raises APIERROR.EINVALIDREQ if the page provided is lower than 1 + """ + + page = flask.request.values.get('page', None) + if not page: + page = 1 + else: + try: + page = int(page) + except (TypeError, ValueError): + raise pagure.exceptions.APIError( + 400, error_code=APIERROR.EINVALIDREQ) + + if page < 1: + raise pagure.exceptions.APIError( + 400, error_code=APIERROR.EINVALIDREQ) + + return page + + +def get_per_page(): + """ Returns the per_page value specified in the request. + Defaults to 20. + raises APIERROR.EINVALIDREQ if the page provided isn't an integer + raises APIERROR.EINVALIDPERPAGEVALUE if the page provided is lower + than 1 or greater than 100 + """ + per_page = flask.request.values.get('per_page', None) or 20 + if per_page: + try: + per_page = int(per_page) + except (TypeError, ValueError): + raise pagure.exceptions.APIError( + 400, error_code=APIERROR.EINVALIDREQ) + + if per_page < 1 or per_page > 100: + raise pagure.exceptions.APIError( + 400, error_code=APIERROR.EINVALIDPERPAGEVALUE) + + return per_page + + if pagure_config.get('ENABLE_TICKETS', True): from pagure.api import issue # noqa: E402 from pagure.api import fork # noqa: E402 diff --git a/pagure/api/project.py b/pagure/api/project.py index 59afe5a..f787d38 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -25,7 +25,7 @@ import pagure.lib.git import pagure.utils from pagure.api import (API, api_method, APIERROR, api_login_required, get_authorized_api_project, api_login_optional, - get_request_data) + get_request_data, get_page, get_per_page) from pagure.config import config as pagure_config @@ -454,8 +454,6 @@ def api_projects(): pattern = flask.request.values.get('pattern', None) short = pagure.utils.is_true( flask.request.values.get('short', False)) - page = flask.request.values.get('page', None) - per_page = flask.request.values.get('per_page', None) if fork is not None: fork = pagure.utils.is_true(fork) @@ -469,36 +467,10 @@ def api_projects(): flask.g.session, username=username, fork=fork, tags=tags, pattern=pattern, private=private, namespace=namespace, owner=owner, count=True) - # Pagination code inspired by Flask-SQLAlchemy - pagination_metadata = None - query_start = None - query_limit = None - if not page: - page = 1 - else: - try: - page = int(page) - except (TypeError, ValueError): - raise pagure.exceptions.APIError( - 400, error_code=APIERROR.EINVALIDREQ) - - if page < 1: - raise pagure.exceptions.APIError( - 400, error_code=APIERROR.EINVALIDREQ) - - if per_page: - try: - per_page = int(per_page) - except (TypeError, ValueError): - raise pagure.exceptions.APIError( - 400, error_code=APIERROR.EINVALIDREQ) - - if per_page < 1 or per_page > 100: - raise pagure.exceptions.APIError( - 400, error_code=APIERROR.EINVALIDPERPAGEVALUE) - else: - per_page = 20 + # Pagination code inspired by Flask-SQLAlchemy + page = get_page() + per_page = get_per_page() pagination_metadata = pagure.lib.get_pagination_metadata( flask.request, page, per_page, project_count) query_start = (page - 1) * per_page