From 45403c9f2dafff5bb7341d31236545e80ec0713d Mon Sep 17 00:00:00 2001 From: Honza Kral Date: Fri, 4 Oct 2013 15:46:41 +0200 Subject: [PATCH] Moved Urllib3HttpConnection to it's own module --- elasticsearch/connection/__init__.py | 3 +- elasticsearch/connection/http.py | 52 -------------------- elasticsearch/connection/http_urllib3.py | 61 ++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 53 deletions(-) create mode 100644 elasticsearch/connection/http_urllib3.py diff --git a/elasticsearch/connection/__init__.py b/elasticsearch/connection/__init__.py index daf4ed15..aebf5ebb 100644 --- a/elasticsearch/connection/__init__.py +++ b/elasticsearch/connection/__init__.py @@ -1,4 +1,5 @@ from .base import Connection -from .http import RequestsHttpConnection, Urllib3HttpConnection +from .http import RequestsHttpConnection +from .http_urllib3 import Urllib3HttpConnection from .memcached import MemcachedConnection from .thrift import ThriftConnection, THRIFT_AVAILABLE diff --git a/elasticsearch/connection/http.py b/elasticsearch/connection/http.py index d537cabe..d667b2b8 100644 --- a/elasticsearch/connection/http.py +++ b/elasticsearch/connection/http.py @@ -1,6 +1,5 @@ import time import requests -import urllib3 try: from urllib import urlencode except ImportError: @@ -53,54 +52,3 @@ class RequestsHttpConnection(Connection): self.log_request_success(method, url, response.request.path_url, body, response.status_code, raw_data, duration) return response.status_code, raw_data - -class Urllib3HttpConnection(Connection): - """ - Default connection class using the `urllib3` library and the http protocol. - - :arg http_auth: optional http auth information as either ':' separated - string or a tuple - :arg use_ssl: use ssl for the connection if `True` - """ - def __init__(self, host='localhost', port=9200, http_auth=None, use_ssl=False, **kwargs): - super(Urllib3HttpConnection, self).__init__(host=host, port=port, **kwargs) - headers = {} - if http_auth is not None: - if isinstance(http_auth, (tuple, list)): - http_auth = ':'.join(http_auth) - headers = urllib3.make_headers(basic_auth=http_auth) - - pool_class = urllib3.HTTPConnectionPool - if use_ssl: - pool_class = urllib3.HTTPSConnectionPool - - self.pool = pool_class(host, port=port, timeout=kwargs.get('timeout', None), headers=headers) - - def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()): - url = self.url_prefix + url - if params: - url = '%s?%s' % (url, urlencode(params or {})) - full_url = self.host + url - - start = time.time() - try: - kw = {} - if timeout: - kw['timeout'] = timeout - response = self.pool.urlopen(method, url, body, **kw) - duration = time.time() - start - raw_data = response.data.decode('utf-8') - except Exception as e: - self.log_request_fail(method, full_url, time.time() - start, exception=e) - raise ConnectionError('N/A', str(e), e) - - if not (200 <= response.status < 300) and response.status not in ignore: - self.log_request_fail(method, url, duration, response.status) - self._raise_error(response.status, raw_data) - - self.log_request_success(method, full_url, url, body, response.status, - raw_data, duration) - - return response.status, raw_data - - diff --git a/elasticsearch/connection/http_urllib3.py b/elasticsearch/connection/http_urllib3.py new file mode 100644 index 00000000..c4ecb632 --- /dev/null +++ b/elasticsearch/connection/http_urllib3.py @@ -0,0 +1,61 @@ +import time +import urllib3 +try: + from urllib import urlencode +except ImportError: + from urllib.parse import urlencode + +from .base import Connection +from ..exceptions import ConnectionError + +class Urllib3HttpConnection(Connection): + """ + Default connection class using the `urllib3` library and the http protocol. + + :arg http_auth: optional http auth information as either ':' separated + string or a tuple + :arg use_ssl: use ssl for the connection if `True` + """ + def __init__(self, host='localhost', port=9200, http_auth=None, use_ssl=False, **kwargs): + super(Urllib3HttpConnection, self).__init__(host=host, port=port, **kwargs) + headers = {} + if http_auth is not None: + if isinstance(http_auth, (tuple, list)): + http_auth = ':'.join(http_auth) + headers = urllib3.make_headers(basic_auth=http_auth) + + pool_class = urllib3.HTTPConnectionPool + if use_ssl: + pool_class = urllib3.HTTPSConnectionPool + + self.pool = pool_class(host, port=port, timeout=kwargs.get('timeout', None), headers=headers) + + def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()): + url = self.url_prefix + url + if params: + url = '%s?%s' % (url, urlencode(params or {})) + full_url = self.host + url + + start = time.time() + try: + kw = {} + if timeout: + kw['timeout'] = timeout + response = self.pool.urlopen(method, url, body, **kw) + duration = time.time() - start + raw_data = response.data.decode('utf-8') + except Exception as e: + self.log_request_fail(method, full_url, time.time() - start, exception=e) + raise ConnectionError('N/A', str(e), e) + + if not (200 <= response.status < 300) and response.status not in ignore: + self.log_request_fail(method, url, duration, response.status) + self._raise_error(response.status, raw_data) + + self.log_request_success(method, full_url, url, body, response.status, + raw_data, duration) + + return response.status, raw_data + + +