From 2e55b5fee96a50fdd2f2cd4017f127d94fc95200 Mon Sep 17 00:00:00 2001 From: Antony Chazapis Date: Wed, 20 Apr 2011 17:52:37 +0300 Subject: [PATCH] Handle marker and limit. --- api/functions.py | 50 ++++++++++++++++++++++++++++++++++++++---- api/templates/containers.xml | 11 ++++++++++ api/util.py | 13 +++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 api/templates/containers.xml diff --git a/api/functions.py b/api/functions.py index 56f139c..908eab7 100644 --- a/api/functions.py +++ b/api/functions.py @@ -3,9 +3,11 @@ # from django.http import HttpResponse +from django.template.loader import render_to_string +from django.utils import simplejson as json from pithos.api.faults import Fault, BadRequest, Unauthorized -from pithos.api.util import api_method +from pithos.api.util import api_method, binary_search_name import logging @@ -18,8 +20,6 @@ def authenticate(request): # unauthorized (401), # badRequest (400) - logging.debug('request.META: %s' % request.META) - x_auth_user = request.META.get('HTTP_X_AUTH_USER') x_auth_key = request.META.get('HTTP_X_AUTH_KEY') @@ -76,7 +76,49 @@ def account_meta(request, v_account): @api_method('GET', format_allowed = True) def container_list(request, v_account): - return HttpResponse("container_list: %s" % v_account) + # Normal Response Codes: 200, 204 + # Error Response Codes: serviceUnavailable (503), + # unauthorized (401), + # badRequest (400) + + containers = [ + {'name': '1', 'count': 2, 'bytes': 123}, + {'name': '2', 'count': 22, 'bytes': 245}, + {'name': '3', 'count': 222, 'bytes': 83745}, + {'name': 'four', 'count': 2222, 'bytes': 274365} + ] + + if len(containers) == 0: + return HttpResponse(status = 204) + + limit = request.GET.get('limit') + marker = request.GET.get('marker') + + start = 0 + if marker: + try: + start = binary_search_name(containers, marker) + 1 + except ValueError: + pass + if limit: + try: + limit = int(limit) + except ValueError: + limit = None + if not limit or limit > 10000: + limit = 10000 + + containers = containers[start:start + limit] + if request.serialization == 'xml': + # TODO: The xml must include the account name as well. + data = render_to_string('containers.xml', {'containers': containers}) + elif request.serialization == 'json': + data = json.dumps(containers) + else: + data = '\n'.join(x['name'] for x in containers) + + # TODO: Return 404 when the account is not found. + return HttpResponse(data, status = 200) @api_method('HEAD') def container_meta(request, v_account, v_container): diff --git a/api/templates/containers.xml b/api/templates/containers.xml new file mode 100644 index 0000000..c82a1ea --- /dev/null +++ b/api/templates/containers.xml @@ -0,0 +1,11 @@ +{% spaceless %} + + + + {% for container in containers %} + + {{ container.name }} + + {% endfor %} + +{% endspaceless %} diff --git a/api/util.py b/api/util.py index 0c058e2..f2d8505 100644 --- a/api/util.py +++ b/api/util.py @@ -22,6 +22,19 @@ import datetime import dateutil.parser import logging +def binary_search_name(a, x, lo = 0, hi = None): + if hi is None: + hi = len(a) + while lo < hi: + mid = (lo + hi) // 2 + midval = a[mid]['name'] + if midval < x: + lo = mid + 1 + elif midval > x: + hi = mid + else: + return mid + raise ValueError() # class UTC(tzinfo): # def utcoffset(self, dt): -- 1.7.10.4