diff --git a/Changelog.rst b/Changelog.rst index 4363663e..952df062 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -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) ------------------ diff --git a/elasticsearch/connection/http_requests.py b/elasticsearch/connection/http_requests.py index 99da09f3..2ca7002a 100644 --- a/elasticsearch/connection/http_requests.py +++ b/elasticsearch/connection/http_requests.py @@ -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) diff --git a/elasticsearch/connection/http_urllib3.py b/elasticsearch/connection/http_urllib3.py index 866635f4..31857383 100644 --- a/elasticsearch/connection/http_urllib3.py +++ b/elasticsearch/connection/http_urllib3.py @@ -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)