Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 07bd3d8

Browse files
committed
Add default content-type headers to support ES6.0
1 parent 761f605 commit 07bd3d8

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

elasticsearch_async/connection.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class AIOHttpConnection(Connection):
1212
def __init__(self, host='localhost', port=9200, http_auth=None,
1313
use_ssl=False, verify_certs=False, ca_certs=None, client_cert=None,
14-
client_key=None, loop=None, use_dns_cache=True, **kwargs):
14+
client_key=None, loop=None, use_dns_cache=True, headers=None, **kwargs):
1515
super().__init__(host=host, port=port, **kwargs)
1616

1717
self.loop = asyncio.get_event_loop() if loop is None else loop
@@ -23,14 +23,18 @@ def __init__(self, host='localhost', port=9200, http_auth=None,
2323
if isinstance(http_auth, (tuple, list)):
2424
http_auth = aiohttp.BasicAuth(*http_auth)
2525

26+
headers = headers or {}
27+
headers.setdefault('content-type', 'application/json')
28+
2629
self.session = aiohttp.ClientSession(
2730
auth=http_auth,
2831
conn_timeout=self.timeout,
2932
connector=aiohttp.TCPConnector(
3033
loop=self.loop,
3134
verify_ssl=verify_certs,
3235
use_dns_cache=use_dns_cache,
33-
)
36+
),
37+
headers=headers
3438
)
3539

3640
self.base_url = 'http%s://%s:%d%s' % (
@@ -42,7 +46,7 @@ def close(self):
4246
return self.session.close()
4347

4448
@asyncio.coroutine
45-
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
49+
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=(), headers=None):
4650
url_path = url
4751
if params:
4852
url_path = '%s?%s' % (url, urlencode(params or {}))
@@ -52,7 +56,7 @@ def perform_request(self, method, url, params=None, body=None, timeout=None, ign
5256
response = None
5357
try:
5458
with aiohttp.Timeout(timeout or self.timeout):
55-
response = yield from self.session.request(method, url, data=body)
59+
response = yield from self.session.request(method, url, data=body, headers=headers)
5660
raw_data = yield from response.text()
5761
duration = self.loop.time() - start
5862

elasticsearch_async/transport.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ def sniff_hosts(self, initial=False):
131131
yield from c.close()
132132

133133
@asyncio.coroutine
134-
def main_loop(self, method, url, params, body, ignore=(), timeout=None):
134+
def main_loop(self, method, url, params, body, headers=None, ignore=(), timeout=None):
135135
for attempt in range(self.max_retries + 1):
136136
connection = self.get_connection()
137137

138138
try:
139139
status, headers, data = yield from connection.perform_request(
140-
method, url, params, body, ignore=ignore, timeout=timeout)
140+
method, url, params, body, headers=headers, ignore=ignore, timeout=timeout)
141141
except TransportError as e:
142142
if method == 'HEAD' and e.status_code == 404:
143143
return False
@@ -169,7 +169,7 @@ def main_loop(self, method, url, params, body, ignore=(), timeout=None):
169169
data = self.deserializer.loads(data, headers.get('content-type'))
170170
return data
171171

172-
def perform_request(self, method, url, params=None, body=None):
172+
def perform_request(self, method, url, headers=None, params=None, body=None):
173173
if body is not None:
174174
body = self.serializer.dumps(body)
175175

@@ -202,6 +202,7 @@ def perform_request(self, method, url, params=None, body=None):
202202
ignore = (ignore, )
203203

204204
return ensure_future(self.main_loop(method, url, params, body,
205+
headers=headers,
205206
ignore=ignore,
206207
timeout=timeout),
207208
loop=self.loop)

0 commit comments

Comments
 (0)