Skip to content

Commit

Permalink
made requests required and made some fixes to transportrequests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddorian committed Nov 27, 2016
1 parent a043d72 commit 9086a2f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
53 changes: 24 additions & 29 deletions SolrClient/transport/transportrequests.py
@@ -1,28 +1,24 @@
import logging
import time
from .transportbase import TransportBase
from ..exceptions import *
from ..exceptions import SolrError, ConnectionError

try:
import requests
req = True
except ImportError:
req = False
raise ImportError("Requests Module not found. Please install it before using this transport")


class TransportRequests(TransportBase):
"""
Class that Uses Requests as Transport Mechanism.
"""

def setup(self):
if not req:
logging.error("Requests Module not found. Please install it before using this transport")
raise ImportException("Requests Module not found. Please install it before using this transport")
self.session = requests.session()
if self.auth:
self.session.auth = (self.auth[0], self.auth[1])


def _send(self, host, method='GET', **kwargs):
url = None
params = kwargs['params'] if 'params' in kwargs else {}
if 'wt' not in params:
params['wt'] = 'json'
Expand All @@ -33,42 +29,41 @@ def _send(self, host, method='GET', **kwargs):
if not host.endswith('/'):
host += '/'
data = kwargs['data'] if 'data' in kwargs else {}
if 'endpoint' in kwargs:
if 'collection' in kwargs:
url = "{}{}/{}".format(host,kwargs['collection'], kwargs['endpoint'])
else:
url = host + kwargs['endpoint']
else:
if 'endpoint' not in kwargs:
raise ValueError("No URL 'endpoint' set in parameters to send_request")

if 'collection' in kwargs:
url = "{}{}/{}".format(host, kwargs['collection'], kwargs['endpoint'])
else:
url = host + kwargs['endpoint']
headers = kwargs.get('headers') or {'content-type': 'application/json'}

self.logger.debug("Sending Request to {} with {}".format(url,", ".join([str("{}={}".format(key, params[key])) for key in params])))
self.logger.debug("Sending Request to {} with {}".format(url, ", ".join(
[str("{}={}".format(key, params[key])) for key in params])))

#Some code used from ES python client.
# Some code used from ES python client.
start = time.time()
try:
res = self.session.request(method, url, params=params, data=data, headers=headers)
duration = time.time() - start
self.logger.debug("Request Completed in {} Seconds".format(round(duration,2)))
self.logger.debug("Request Completed in {} Seconds".format(round(duration, 2)))
except requests.exceptions.SSLError as e:
self._log_connection_error(method, url, body, time.time() - start, exception=e)
self._log_connection_error(method, url, data, time.time() - start, exception=e)
raise ConnectionError('N/A', str(e), e)
except requests.Timeout as e:
self._log_connection_error(method, url, body, time.time() - start, exception=e)
self._log_connection_error(method, url, data, time.time() - start, exception=e)
raise ConnectionError('TIMEOUT', str(e), e)
except requests.ConnectionError as e:
self._log_connection_error(method, url, str(e), time.time() - start, exception=e)
raise ConnectionError('N/A', str(e), e)

if (200 <= res.status_code < 300):
if 200 <= res.status_code < 300:
return [res.json(), {'url': res.url}]
if res.status_code == 404:
raise ConnectionError("404 - {}".format(res.url))
elif res.status_code == 401:
raise ConnectionError("401 - {}".format(res.url))
elif res.status_code == 500:
raise SolrError("500 - " + res.url + " " + res.text)
else:
if res.status_code == 404:
raise ConnectionError("404 - {}".format(res.url))
elif res.status_code == 401:
raise ConnectionError("401 - {}".format(res.url))
elif res.status_code == 500:
raise SolrError("500 - " + res.url + " "+res.text)
else:
raise SolrError(res.url+" "+res.text)
raise SolrError(res.url + " " + res.text)
1 change: 1 addition & 0 deletions requirements.txt
@@ -1,2 +1,3 @@
kazoo==2.2.1
tox==2.3.1
requests>=2.2.1

0 comments on commit 9086a2f

Please sign in to comment.