11
11
class AIOHttpConnection (Connection ):
12
12
def __init__ (self , host = 'localhost' , port = 9200 , http_auth = None ,
13
13
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 ):
15
15
super ().__init__ (host = host , port = port , ** kwargs )
16
16
17
17
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,
23
23
if isinstance (http_auth , (tuple , list )):
24
24
http_auth = aiohttp .BasicAuth (* http_auth )
25
25
26
+ headers = headers or {}
27
+ headers .setdefault ('content-type' , 'application/json' )
28
+
26
29
self .session = aiohttp .ClientSession (
27
30
auth = http_auth ,
28
31
conn_timeout = self .timeout ,
29
32
connector = aiohttp .TCPConnector (
30
33
loop = self .loop ,
31
34
verify_ssl = verify_certs ,
32
35
use_dns_cache = use_dns_cache ,
33
- )
36
+ ),
37
+ headers = headers
34
38
)
35
39
36
40
self .base_url = 'http%s://%s:%d%s' % (
@@ -42,7 +46,7 @@ def close(self):
42
46
return self .session .close ()
43
47
44
48
@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 ):
46
50
url_path = url
47
51
if params :
48
52
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
52
56
response = None
53
57
try :
54
58
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 )
56
60
raw_data = yield from response .text ()
57
61
duration = self .loop .time () - start
58
62
0 commit comments