Skip to content

Commit 3045516

Browse files
committed
Clean up custom headers.
1 parent 073db90 commit 3045516

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

elasticsearch/client/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ def bulk(self, body, index=None, doc_type=None, params=None):
11281128
raise ValueError("Empty value passed for a required argument 'body'.")
11291129
return self.transport.perform_request('POST', _make_path(index,
11301130
doc_type, '_bulk'), params=params, body=self._bulk_body(body),
1131-
headers={'Content-Type': 'application/x-ndjson'})
1131+
headers={'content-type': 'application/x-ndjson'})
11321132

11331133
@query_params('max_concurrent_searches', 'pre_filter_shard_size',
11341134
'search_type', 'typed_keys')
@@ -1160,7 +1160,8 @@ def msearch(self, body, index=None, doc_type=None, params=None):
11601160
if body in SKIP_IN_PATH:
11611161
raise ValueError("Empty value passed for a required argument 'body'.")
11621162
return self.transport.perform_request('GET', _make_path(index,
1163-
doc_type, '_msearch'), params=params, body=self._bulk_body(body))
1163+
doc_type, '_msearch'), params=params, body=self._bulk_body(body),
1164+
headers={'content-type': 'application/x-ndjson'})
11641165

11651166
@query_params('field_statistics', 'fields', 'offsets', 'parent', 'payloads',
11661167
'positions', 'preference', 'realtime', 'routing', 'term_statistics',
@@ -1364,7 +1365,8 @@ def msearch_template(self, body, index=None, doc_type=None, params=None):
13641365
if body in SKIP_IN_PATH:
13651366
raise ValueError("Empty value passed for a required argument 'body'.")
13661367
return self.transport.perform_request('GET', _make_path(index, doc_type,
1367-
'_msearch', 'template'), params=params, body=self._bulk_body(body))
1368+
'_msearch', 'template'), params=params, body=self._bulk_body(body),
1369+
headers={'content-type': 'application/x-ndjson'})
13681370

13691371
@query_params('allow_no_indices', 'expand_wildcards', 'fields',
13701372
'ignore_unavailable')
@@ -1388,3 +1390,4 @@ def field_caps(self, index=None, body=None, params=None):
13881390
"""
13891391
return self.transport.perform_request('GET', _make_path(index,
13901392
'_field_caps'), params=params, body=body)
1393+

elasticsearch/connection/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,5 @@ def _raise_error(self, status_code, raw_data):
123123
logger.warning('Undecodable raw error response from server: %s', err)
124124

125125
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
126+
127+

elasticsearch/connection/http_urllib3.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ def perform_request(self, method, url, params=None, body=None, timeout=None, ign
111111
if not isinstance(method, str):
112112
method = method.encode('utf-8')
113113

114-
request_headers = dict(self.headers)
115-
request_headers.update(headers or {})
114+
if headers:
115+
request_headers = dict(self.headers)
116+
request_headers.update(headers or {})
116117
response = self.pool.urlopen(method, url, body, retries=False, headers=self.headers, **kw)
117118
duration = time.time() - start
118119
raw_data = response.data.decode('utf-8')

elasticsearch/transport.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def perform_request(self, method, url, headers=None, params=None, body=None):
270270
:arg method: HTTP method to use
271271
:arg url: absolute url (without host) to target
272272
:arg headers: dictionary of headers, will be handed over to the
273-
underlying :class:`~elasticsearch.Connection` class for serialization
273+
underlying :class:`~elasticsearch.Connection` class
274274
:arg params: dictionary of query parameters, will be handed over to the
275275
underlying :class:`~elasticsearch.Connection` class for serialization
276276
:arg body: body of the request, will be serializes using serializer and
@@ -294,7 +294,7 @@ def perform_request(self, method, url, headers=None, params=None, body=None):
294294

295295
if body is not None:
296296
try:
297-
body = body.encode('utf-8', 'surrogatepass')
297+
body = body.encode('utf-8', 'surrogatepass')
298298
except (UnicodeDecodeError, AttributeError):
299299
# bytes/str - no need to re-encode
300300
pass

test_elasticsearch/test_connection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ def test_uses_https_if_verify_certs_is_off(self):
104104
self.assertEquals('GET', request.method)
105105
self.assertEquals(None, request.body)
106106

107+
def test_merge_headers(self):
108+
con = self._get_mock_connection(connection_params={'headers': {'h1': 'v1', 'h2': 'v2'}})
109+
req = self._get_request(con, 'GET', '/', headers={'h2': 'v2p', 'h3': 'v3'})
110+
self.assertEquals(req.headers['h1'], 'v1')
111+
self.assertEquals(req.headers['h2'], 'v2p')
112+
self.assertEquals(req.headers['h3'], 'v3')
113+
107114
def test_http_auth(self):
108115
con = RequestsHttpConnection(http_auth='username:secret')
109116
self.assertEquals(('username', 'secret'), con.session.auth)

0 commit comments

Comments
 (0)