2013-08-25 16:39:56 +02:00
|
|
|
import time
|
2013-10-11 10:37:04 +02:00
|
|
|
try:
|
|
|
|
|
import requests
|
|
|
|
|
REQUESTS_AVAILABLE = True
|
|
|
|
|
except ImportError:
|
|
|
|
|
REQUESTS_AVAILABLE = False
|
2013-08-25 16:39:56 +02:00
|
|
|
|
|
|
|
|
from .base import Connection
|
2013-10-11 10:37:04 +02:00
|
|
|
from ..exceptions import ConnectionError, ImproperlyConfigured
|
2014-02-21 16:53:56 +01:00
|
|
|
from ..compat import urlencode
|
2013-08-25 16:39:56 +02:00
|
|
|
|
|
|
|
|
class RequestsHttpConnection(Connection):
|
2013-09-24 22:55:04 +02:00
|
|
|
"""
|
|
|
|
|
Connection using the `requests` library.
|
|
|
|
|
|
|
|
|
|
:arg http_auth: optional http auth information as either ':' separated
|
|
|
|
|
string or a tuple
|
2013-09-25 21:07:29 +02:00
|
|
|
:arg use_ssl: use ssl for the connection if `True`
|
2013-09-24 22:55:04 +02:00
|
|
|
"""
|
2013-09-25 21:07:29 +02:00
|
|
|
def __init__(self, host='localhost', port=9200, http_auth=None, use_ssl=False, **kwargs):
|
2013-10-11 10:37:04 +02:00
|
|
|
if not REQUESTS_AVAILABLE:
|
|
|
|
|
raise ImproperlyConfigured("Please install requests to use RequestsHttpConnection.")
|
|
|
|
|
|
2013-10-02 00:39:19 +02:00
|
|
|
super(RequestsHttpConnection, self).__init__(host= host, port=port, **kwargs)
|
2013-08-25 16:39:56 +02:00
|
|
|
self.session = requests.session()
|
2013-09-24 23:07:14 +02:00
|
|
|
if http_auth is not None:
|
2013-10-02 00:42:38 +02:00
|
|
|
if not isinstance(http_auth, (tuple, list)):
|
2013-10-02 01:03:27 +02:00
|
|
|
http_auth = http_auth.split(':', 1)
|
2013-10-02 00:42:38 +02:00
|
|
|
http_auth = tuple(http_auth)
|
2013-09-24 22:55:04 +02:00
|
|
|
self.session.auth = http_auth
|
2013-09-24 23:07:14 +02:00
|
|
|
self.base_url = 'http%s://%s:%d%s' % (
|
2013-09-25 21:07:29 +02:00
|
|
|
's' if use_ssl else '',
|
2013-09-24 23:07:14 +02:00
|
|
|
host, port, self.url_prefix
|
|
|
|
|
)
|
2013-09-24 22:55:04 +02:00
|
|
|
|
2013-08-25 16:39:56 +02:00
|
|
|
|
2013-09-25 23:09:50 +02:00
|
|
|
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
|
2013-09-24 23:07:14 +02:00
|
|
|
url = self.base_url + url
|
2013-10-02 00:39:19 +02:00
|
|
|
if params:
|
|
|
|
|
url = '%s?%s' % (url, urlencode(params or {}))
|
2013-08-25 16:39:56 +02:00
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
|
try:
|
2013-10-02 00:39:19 +02:00
|
|
|
response = self.session.request(method, url, data=body, timeout=timeout or self.timeout)
|
2013-08-25 16:39:56 +02:00
|
|
|
duration = time.time() - start
|
|
|
|
|
raw_data = response.text
|
|
|
|
|
except (requests.ConnectionError, requests.Timeout) as e:
|
2013-12-03 01:55:32 +01:00
|
|
|
self.log_request_fail(method, url, body, time.time() - start, exception=e)
|
2013-08-25 16:39:56 +02:00
|
|
|
raise ConnectionError('N/A', str(e), e)
|
|
|
|
|
|
|
|
|
|
# raise errors based on http status codes, let the client handle those if needed
|
2013-09-25 23:09:50 +02:00
|
|
|
if not (200 <= response.status_code < 300) and response.status_code not in ignore:
|
2013-12-03 01:55:32 +01:00
|
|
|
self.log_request_fail(method, url, body, duration, response.status_code)
|
2013-08-25 16:39:56 +02:00
|
|
|
self._raise_error(response.status_code, raw_data)
|
|
|
|
|
|
2013-10-02 00:54:05 +02:00
|
|
|
self.log_request_success(method, url, response.request.path_url, body, response.status_code, raw_data, duration)
|
2013-08-25 16:39:56 +02:00
|
|
|
|
2013-12-13 19:36:21 +01:00
|
|
|
return response.status_code, response.headers, raw_data
|