Allow specifying custom http headers

This commit is contained in:
Honza Král
2016-07-12 18:13:48 +02:00
parent ac74778037
commit 311ac7c814
3 changed files with 10 additions and 4 deletions
+2
View File
@@ -8,6 +8,8 @@ Changelog
Version compatible with elasticsearch 5.0
* added ``headers`` arg to connections to support custom http headers
2.3.0 (2016-02-29)
------------------
+4 -2
View File
@@ -24,15 +24,17 @@ class RequestsHttpConnection(Connection):
certificate, or cert only if using client_key
:arg client_key: path to the file containing the private key if using
separate cert and key files (client_cert will contain only the cert)
:arg headers: any custom http headers to be add to requests
"""
def __init__(self, host='localhost', port=9200, http_auth=None,
use_ssl=False, verify_certs=False, ca_certs=None, client_cert=None,
client_key=None, **kwargs):
client_key=None, headers=None, **kwargs):
if not REQUESTS_AVAILABLE:
raise ImproperlyConfigured("Please install requests to use RequestsHttpConnection.")
super(RequestsHttpConnection, self).__init__(host=host, port=port, **kwargs)
self.session = requests.session()
self.session = requests.Session()
self.session.headers = headers
if http_auth is not None:
if isinstance(http_auth, (tuple, list)):
http_auth = tuple(http_auth)
+4 -2
View File
@@ -33,14 +33,16 @@ class Urllib3HttpConnection(Connection):
:arg ssl_assert_fingerprint: verify the supplied certificate fingerprint if not `None`
:arg maxsize: the maximum number of connections which will be kept open to
this host.
:arg headers: any custom http headers to be add to requests
"""
def __init__(self, host='localhost', port=9200, http_auth=None,
use_ssl=False, verify_certs=False, ca_certs=None, client_cert=None,
client_key=None, ssl_version=None, ssl_assert_hostname=None,
ssl_assert_fingerprint=None, maxsize=10, **kwargs):
ssl_assert_fingerprint=None, maxsize=10, headers=None, **kwargs):
super(Urllib3HttpConnection, self).__init__(host=host, port=port, use_ssl=use_ssl, **kwargs)
self.headers = urllib3.make_headers(keep_alive=True)
self.headers = headers.copy() if headers else {}
self.headers.update(urllib3.make_headers(keep_alive=True))
if http_auth is not None:
if isinstance(http_auth, (tuple, list)):
http_auth = ':'.join(http_auth)