From f44ca7939105c6436af2bdc71ca52c7f83ef8e40 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 29 Sep 2015 15:53:38 -0400 Subject: [PATCH] Add _catalog endpoint as specified by V2 API Fixes #391 --- endpoints/v2/__init__.py | 2 ++ endpoints/v2/catalog.py | 32 ++++++++++++++++++++++++++++++++ endpoints/v2/tag.py | 19 +++---------------- endpoints/v2/v2util.py | 19 +++++++++++++++++++ 4 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 endpoints/v2/catalog.py create mode 100644 endpoints/v2/v2util.py diff --git a/endpoints/v2/__init__.py b/endpoints/v2/__init__.py index e74e034ad..837b4ac51 100644 --- a/endpoints/v2/__init__.py +++ b/endpoints/v2/__init__.py @@ -85,3 +85,5 @@ from endpoints.v2 import v2auth from endpoints.v2 import manifest from endpoints.v2 import blob from endpoints.v2 import tag +from endpoints.v2 import catalog + diff --git a/endpoints/v2/catalog.py b/endpoints/v2/catalog.py new file mode 100644 index 000000000..150791710 --- /dev/null +++ b/endpoints/v2/catalog.py @@ -0,0 +1,32 @@ +# XXX This code is not yet ready to be run in production, and should remain disabled until such +# XXX time as this notice is removed. + +from flask import jsonify, url_for + +from endpoints.v2 import v2_bp +from auth.auth import process_auth +from endpoints.decorators import anon_protect +from data import model +from endpoints.v2.v2util import add_pagination +from auth.auth_context import get_authenticated_user + +@v2_bp.route('/_catalog', methods=['GET']) +@process_auth +@anon_protect +def catalog_search(): + url = url_for('v2.catalog_search') + + username = get_authenticated_user().username if get_authenticated_user() else None + query = model.repository.get_visible_repositories(username, include_public=(username is None), + limit=50) + + link, query = add_pagination(query, url) + + response = jsonify({ + 'repositories': ['%s/%s' % (repo.namespace_user.username, repo.name) for repo in query], + }) + + if link is not None: + response.headers['Link'] = link + + return response diff --git a/endpoints/v2/tag.py b/endpoints/v2/tag.py index 375c48bb3..aedcb3fb8 100644 --- a/endpoints/v2/tag.py +++ b/endpoints/v2/tag.py @@ -1,28 +1,15 @@ # XXX This code is not yet ready to be run in production, and should remain disabled until such # XXX time as this notice is removed. -from flask import jsonify, request, url_for +from flask import jsonify, url_for -from app import get_app_url from endpoints.v2 import v2_bp, require_repo_read from endpoints.v2.errors import NameUnknown +from endpoints.v2.v2util import add_pagination from auth.jwt_auth import process_jwt_auth from endpoints.decorators import anon_protect from data import model -def _add_pagination(query, url): - limit = request.args.get('n', None) - page = request.args.get('page', 1) - - if limit is None: - return None, query - - url = get_app_url() + url - query = query.paginate(page, limit) - link = url + '?n=%s&last=%s; rel="next"' % (limit, page + 1) - return link, query - - @v2_bp.route('///tags/list', methods=['GET']) @process_jwt_auth @require_repo_read @@ -35,7 +22,7 @@ def list_all_tags(namespace, repo_name): query = model.tag.list_repository_tags(namespace, repo_name) url = url_for('v2.list_all_tags', namespace=namespace, repo_name=repo_name) - link, query = _add_pagination(query, url) + link, query = add_pagination(query, url) response = jsonify({ 'name': '{0}/{1}'.format(namespace, repo_name), diff --git a/endpoints/v2/v2util.py b/endpoints/v2/v2util.py new file mode 100644 index 000000000..78ffdd756 --- /dev/null +++ b/endpoints/v2/v2util.py @@ -0,0 +1,19 @@ +from flask import request +from app import get_app_url + +_MAX_RESULTS_PER_PAGE = 100 + +def add_pagination(query, url): + """ Adds optional pagination to the given query by looking for the Docker V2 pagination request + args. """ + limit = request.args.get('n', None) + page = request.args.get('page', 1) + + if limit is None: + return None, query + + limit = max(limit, _MAX_RESULTS_PER_PAGE) + url = get_app_url() + url + query = query.paginate(page, limit) + link = url + '?n=%s&last=%s; rel="next"' % (limit, page + 1) + return link, query