From d34f12614820416aebe5e91eab9726b556d1f973 Mon Sep 17 00:00:00 2001 From: Stanislav Vitkovskiy Date: Sat, 27 Jul 2013 17:06:28 +1000 Subject: [PATCH] Added headers argument support to get_object() With this change conditional and range GETs are possible, as documented in official API guide: http://docs.openstack.org/api/openstack-object-storage/1.0/content/retrieve-object.html Change-Id: Ib2ed1c21e8d3f1ed79c0b7e542ee022ee535835c --- swiftclient/client.py | 11 +++++++---- tests/test_swiftclient.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/swiftclient/client.py b/swiftclient/client.py index c9012be2..b9b07f2d 100644 --- a/swiftclient/client.py +++ b/swiftclient/client.py @@ -747,7 +747,7 @@ def delete_container(url, token, container, http_conn=None, def get_object(url, token, container, name, http_conn=None, resp_chunk_size=None, query_string=None, - response_dict=None): + response_dict=None, headers=None): """ Get an object @@ -764,6 +764,8 @@ def get_object(url, token, container, name, http_conn=None, :param query_string: if set will be appended with '?' to generated path :param response_dict: an optional dictionary into which to place the response - status, reason and headers + :param headers: an optional dictionary with additional headers to include + in the request :returns: a tuple of (response headers, the object's contents) The response headers will be a dict and all header names will be lowercase. :raises ClientException: HTTP GET request failed @@ -776,7 +778,8 @@ def get_object(url, token, container, name, http_conn=None, if query_string: path += '?' + query_string method = 'GET' - headers = {'X-Auth-Token': token} + headers = headers.copy() if headers else {} + headers['X-Auth-Token'] = token conn.request(method, path, '', headers) resp = conn.getresponse() @@ -1234,12 +1237,12 @@ class Connection(object): return self._retry(None, head_object, container, obj) def get_object(self, container, obj, resp_chunk_size=None, - query_string=None, response_dict=None): + query_string=None, response_dict=None, headers=None): """Wrapper for :func:`get_object`""" return self._retry(None, get_object, container, obj, resp_chunk_size=resp_chunk_size, query_string=query_string, - response_dict=response_dict) + response_dict=response_dict, headers=headers) def put_object(self, container, obj, contents, content_length=None, etag=None, chunk_size=None, content_type=None, diff --git a/tests/test_swiftclient.py b/tests/test_swiftclient.py index a0728789..a039d203 100644 --- a/tests/test_swiftclient.py +++ b/tests/test_swiftclient.py @@ -520,6 +520,26 @@ class TestGetObject(MockHttpTest): c.get_object('http://www.test.com', 'asdf', 'asdf', 'asdf', query_string="hello=20") + def test_request_headers(self): + request_args = {} + + def fake_request(method, url, body=None, headers=None): + request_args['method'] = method + request_args['url'] = url + request_args['body'] = body + request_args['headers'] = headers + return + conn = self.fake_http_connection(200)('http://www.test.com/') + conn[1].request = fake_request + headers = {'Range': 'bytes=1-2'} + c.get_object('url_is_irrelevant', 'TOKEN', 'container', 'object', + http_conn=conn, headers=headers) + self.assertFalse(request_args['headers'] is None, + "No headers in the request") + self.assertTrue('Range' in request_args['headers'], + "No Range header in the request") + self.assertEquals(request_args['headers']['Range'], 'bytes=1-2') + class TestHeadObject(MockHttpTest):