From e2ab7ea80ddc67fc682c7587a1cb62c1cd3cae47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Fri, 13 Dec 2013 19:36:21 +0100 Subject: [PATCH] Adding a third return value from connection.perform_request - headers This will be used to implement deserializing based on mimetype --- Changelog.rst | 3 +++ elasticsearch/connection/http_requests.py | 2 +- elasticsearch/connection/http_urllib3.py | 4 +--- elasticsearch/connection/memcached.py | 2 +- elasticsearch/connection/thrift.py | 4 +--- elasticsearch/transport.py | 4 ++-- test_elasticsearch/test_connection.py | 4 ++-- test_elasticsearch/test_transport.py | 3 ++- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Changelog.rst b/Changelog.rst index 6373ed97..19a9826c 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -13,6 +13,9 @@ Changelog * `helpers.bulk` and `helpers.streaming_bulk` are no longer limitted to just index operations. * unicode body (for `incices.analyze` for example) is now handled correctly + * changed `perform_request` on `Connection` classes to return headers as well. + This is a backwards incompatible change for people who have developed their own + connection class. 0.4.3 ----- diff --git a/elasticsearch/connection/http_requests.py b/elasticsearch/connection/http_requests.py index 7993c998..53a75496 100644 --- a/elasticsearch/connection/http_requests.py +++ b/elasticsearch/connection/http_requests.py @@ -58,4 +58,4 @@ class RequestsHttpConnection(Connection): self.log_request_success(method, url, response.request.path_url, body, response.status_code, raw_data, duration) - return response.status_code, raw_data + return response.status_code, response.headers, raw_data diff --git a/elasticsearch/connection/http_urllib3.py b/elasticsearch/connection/http_urllib3.py index 4a245c2b..7196004d 100644 --- a/elasticsearch/connection/http_urllib3.py +++ b/elasticsearch/connection/http_urllib3.py @@ -57,7 +57,5 @@ class Urllib3HttpConnection(Connection): self.log_request_success(method, full_url, url, body, response.status, raw_data, duration) - return response.status, raw_data - - + return response.status, response.getheaders(), raw_data diff --git a/elasticsearch/connection/memcached.py b/elasticsearch/connection/memcached.py index ac6289b1..9f2ef236 100644 --- a/elasticsearch/connection/memcached.py +++ b/elasticsearch/connection/memcached.py @@ -77,7 +77,7 @@ class MemcachedConnection(PoolingConnection): self.log_request_success(method, full_url, url, body, status, response, duration) - return status, response + return status, {}, response diff --git a/elasticsearch/connection/thrift.py b/elasticsearch/connection/thrift.py index 627105b1..cce12f9f 100644 --- a/elasticsearch/connection/thrift.py +++ b/elasticsearch/connection/thrift.py @@ -73,7 +73,5 @@ class ThriftConnection(PoolingConnection): self.log_request_success(method, url, url, body, response.status, response.body, duration) - return response.status, response.body - - + return response.status, response.headers or {}, response.body diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index 79819f1d..10591d15 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -154,7 +154,7 @@ class Transport(object): for c in self.connection_pool.connections + self.seed_connections: try: # use small timeout for the sniffing request, should be a fast api call - _, node_info = c.perform_request('GET', '/_cluster/nodes', timeout=.1) + _, headers, node_info = c.perform_request('GET', '/_cluster/nodes', timeout=.1) node_info = self.serializer.loads(node_info) break except (ConnectionError, SerializationError): @@ -251,7 +251,7 @@ class Transport(object): connection = self.get_connection() try: - status, raw_data = connection.perform_request(method, url, params, body, ignore=ignore) + status, headers, raw_data = connection.perform_request(method, url, params, body, ignore=ignore) except ConnectionError: self.mark_dead(connection) diff --git a/test_elasticsearch/test_connection.py b/test_elasticsearch/test_connection.py index e940f34e..4f776327 100644 --- a/test_elasticsearch/test_connection.py +++ b/test_elasticsearch/test_connection.py @@ -62,7 +62,7 @@ class TestRequestsConnection(TestCase): return con def _get_request(self, connection, *args, **kwargs): - status, data = connection.perform_request(*args, **kwargs) + status, headers, data = connection.perform_request(*args, **kwargs) self.assertEquals(200, status) self.assertEquals(u'{}', data) @@ -129,7 +129,7 @@ class TestRequestsConnection(TestCase): @patch('elasticsearch.connection.base.logger') def test_success_logs_and_traces(self, logger, tracer): con = self._get_mock_connection(response_body='''{"answer": "that's it!"}''') - status, data = con.perform_request('GET', '/', {'param': 42}, '''{"question": "what's that?"}''') + status, headers, data = con.perform_request('GET', '/', {'param': 42}, '''{"question": "what's that?"}''') # trace request self.assertEquals(1, tracer.info.call_count) diff --git a/test_elasticsearch/test_transport.py b/test_elasticsearch/test_transport.py index 73f0fd1b..6e406370 100644 --- a/test_elasticsearch/test_transport.py +++ b/test_elasticsearch/test_transport.py @@ -11,6 +11,7 @@ class DummyConnection(Connection): def __init__(self, **kwargs): self.exception = kwargs.pop('exception', None) self.status, self.data = kwargs.pop('status', 200), kwargs.pop('data', '{}') + self.headers = kwargs.pop('headers', {}) self.calls = [] super(DummyConnection, self).__init__(**kwargs) @@ -18,7 +19,7 @@ class DummyConnection(Connection): self.calls.append((args, kwargs)) if self.exception: raise self.exception - return self.status, self.data + return self.status, self.headers, self.data CLUSTER_NODES = '''{ "ok" : true,