support text/xml

This commit is contained in:
Michael Barton 2010-11-02 16:04:15 +00:00
parent cefcd568cd
commit 5d564a98b0
3 changed files with 24 additions and 23 deletions

View File

@ -199,16 +199,15 @@ class AccountController(object):
except UnicodeDecodeError, err: except UnicodeDecodeError, err:
return HTTPBadRequest(body='parameters not utf8', return HTTPBadRequest(body='parameters not utf8',
content_type='text/plain', request=req) content_type='text/plain', request=req)
header_format = req.accept.best_match(['text/plain', if query_format:
'application/json', req.accept = 'application/%s' % query_format.lower()
'application/xml']) out_content_type = req.accept.best_match(
format = query_format or header_format or 'text/plain' ['text/plain', 'application/json',
if '/' in format: 'application/xml', 'text/xml'],
format = format.split('/')[-1] default_match='text/plain')
account_list = broker.list_containers_iter(limit, marker, prefix, account_list = broker.list_containers_iter(limit, marker, prefix,
delimiter) delimiter)
if format == 'json': if out_content_type == 'application/json':
out_content_type = 'application/json'
json_pattern = ['"name":%s', '"count":%s', '"bytes":%s'] json_pattern = ['"name":%s', '"count":%s', '"bytes":%s']
json_pattern = '{' + ','.join(json_pattern) + '}' json_pattern = '{' + ','.join(json_pattern) + '}'
json_out = [] json_out = []
@ -220,8 +219,7 @@ class AccountController(object):
json_out.append(json_pattern % json_out.append(json_pattern %
(name, object_count, bytes_used)) (name, object_count, bytes_used))
account_list = '[' + ','.join(json_out) + ']' account_list = '[' + ','.join(json_out) + ']'
elif format == 'xml': elif out_content_type.endswith('/xml'):
out_content_type = 'application/xml'
output_list = ['<?xml version="1.0" encoding="UTF-8"?>', output_list = ['<?xml version="1.0" encoding="UTF-8"?>',
'<account name="%s">' % account] '<account name="%s">' % account]
for (name, object_count, bytes_used, is_subdir) in account_list: for (name, object_count, bytes_used, is_subdir) in account_list:
@ -238,7 +236,6 @@ class AccountController(object):
else: else:
if not account_list: if not account_list:
return HTTPNoContent(request=req, headers=resp_headers) return HTTPNoContent(request=req, headers=resp_headers)
out_content_type = 'text/plain'
account_list = '\n'.join(r[0] for r in account_list) + '\n' account_list = '\n'.join(r[0] for r in account_list) + '\n'
ret = Response(body=account_list, request=req, headers=resp_headers) ret = Response(body=account_list, request=req, headers=resp_headers)
ret.content_type = out_content_type ret.content_type = out_content_type

View File

@ -278,16 +278,15 @@ class ContainerController(object):
except UnicodeDecodeError, err: except UnicodeDecodeError, err:
return HTTPBadRequest(body='parameters not utf8', return HTTPBadRequest(body='parameters not utf8',
content_type='text/plain', request=req) content_type='text/plain', request=req)
header_format = req.accept.best_match(['text/plain', if query_format:
'application/json', req.accept = 'application/%s' % query_format.lower()
'application/xml']) out_content_type = req.accept.best_match(
format = query_format or header_format or 'text/plain' ['text/plain', 'application/json',
if '/' in format: 'application/xml', 'text/xml'],
format = format.split('/')[-1] default_match='text/plain')
container_list = broker.list_objects_iter(limit, marker, prefix, container_list = broker.list_objects_iter(limit, marker, prefix,
delimiter, path) delimiter, path)
if format == 'json': if out_content_type == 'application/json':
out_content_type = 'application/json'
json_pattern = ['"name":%s', '"hash":"%s"', '"bytes":%s', json_pattern = ['"name":%s', '"hash":"%s"', '"bytes":%s',
'"content_type":%s, "last_modified":"%s"'] '"content_type":%s, "last_modified":"%s"']
json_pattern = '{' + ','.join(json_pattern) + '}' json_pattern = '{' + ','.join(json_pattern) + '}'
@ -307,8 +306,7 @@ class ContainerController(object):
content_type, content_type,
created_at)) created_at))
container_list = '[' + ','.join(json_out) + ']' container_list = '[' + ','.join(json_out) + ']'
elif format == 'xml': elif out_content_type.endswith('/xml'):
out_content_type = 'application/xml'
xml_output = [] xml_output = []
for (name, created_at, size, content_type, etag) in container_list: for (name, created_at, size, content_type, etag) in container_list:
# escape name and format date here # escape name and format date here
@ -330,7 +328,6 @@ class ContainerController(object):
else: else:
if not container_list: if not container_list:
return HTTPNoContent(request=req, headers=resp_headers) return HTTPNoContent(request=req, headers=resp_headers)
out_content_type = 'text/plain'
container_list = '\n'.join(r[0] for r in container_list) + '\n' container_list = '\n'.join(r[0] for r in container_list) + '\n'
ret = Response(body=container_list, request=req, headers=resp_headers) ret = Response(body=container_list, request=req, headers=resp_headers)
ret.content_type = out_content_type ret.content_type = out_content_type

View File

@ -619,7 +619,7 @@ class TestContainerController(unittest.TestCase):
self.assertEquals(resp.body, xml_body) self.assertEquals(resp.body, xml_body)
for xml_accept in ('application/xml', 'application/xml;q=1.0,*/*;q=0.9', for xml_accept in ('application/xml', 'application/xml;q=1.0,*/*;q=0.9',
'*/*;q=0.9,application/xml;q=1.0'): '*/*;q=0.9,application/xml;q=1.0', 'application/xml,text/xml'):
req = Request.blank('/sda1/p/a/xmlc', req = Request.blank('/sda1/p/a/xmlc',
environ={'REQUEST_METHOD': 'GET'}) environ={'REQUEST_METHOD': 'GET'})
req.accept = xml_accept req.accept = xml_accept
@ -629,6 +629,13 @@ class TestContainerController(unittest.TestCase):
self.assertEquals(resp.content_type, 'application/xml', self.assertEquals(resp.content_type, 'application/xml',
'Invalid content_type for Accept: %s' % xml_accept) 'Invalid content_type for Accept: %s' % xml_accept)
req = Request.blank('/sda1/p/a/xmlc',
environ={'REQUEST_METHOD': 'GET'})
req.accept = 'text/xml'
resp = self.controller.GET(req)
self.assertEquals(resp.content_type, 'text/xml')
self.assertEquals(resp.body, xml_body)
def test_GET_marker(self): def test_GET_marker(self):
# make a container # make a container
req = Request.blank('/sda1/p/a/c', environ={'REQUEST_METHOD': 'PUT', req = Request.blank('/sda1/p/a/c', environ={'REQUEST_METHOD': 'PUT',