# Path: SolrClient/transport/transportbase.py
# class TransportBase():
#     """
#     Base Transport Class
#     """
# 
#     #def __init__(self, solr, auth=(None, None), devel=None, host=None, router=PlainRouter, **kwargs):
#     def __init__(self, solr, auth=(None, None), devel=None, host=None, **kwargs):
#         self.logger = logging.getLogger(str(__package__))
#         self.auth = auth
#         self.host = host if type(host) is list else [host]
#         self._devel = devel
#         self._action_log = []
#         self._action_log_count = 1000
#         self.solr = solr
#         #self.router = router(self, host, **kwargs)
#         self.setup()
# 
#     def _add_to_action(self, action):
#         self._action_log.append(action)
#         if len(self._action_log) >= self._action_log_count:
#             self._action_log.pop(0)
# 
#     def _retry(function):
#         """
#         Internal mechanism to try to send data to multiple Solr Hosts if
#         the query fails on the first one.
#         """
# 
#         def inner(self, **kwargs):
#             last_exception = None
#             #for host in self.router.get_hosts(**kwargs):
#             for host in self.host:
#                 try:
#                     return function(self, host, **kwargs)
#                 except SolrError as e:
#                     self.logger.exception(e)
#                     raise
#                 except ConnectionError as e:
#                     self.logger.exception("Tried connecting to Solr, but couldn't because of the following exception.")
#                     if '401' in e.__str__():
#                         raise
#                     last_exception = e
#             # raise the last exception after contacting all hosts instead of returning None
#             if last_exception is not None:
#                 raise last_exception
#         return inner
# 
#     @_retry
#     def send_request(self, host, **kwargs):
#         if self._devel:
#             self._add_to_action({'host': host, 'params': dict(**kwargs)})
#         res_dict, c_inf = self._send(host, **kwargs)
#         if 'errors' in res_dict:
#             error = ", ".join([x for x in res_dict['errors'][0]['errorMessages']])
#             raise SolrError(error)
#         elif 'error' in res_dict:
#             raise SolrError(str(res_dict['error']))
#         return [res_dict, c_inf]
# 
#     def _log_connection_error(self, method, full_url, body, duration, status_code=None, exception=None):
#         self.logger.warning("Connection Error: [{}] {} - {} - {}".format(status_code, method, full_url, body))
# 
# Path: SolrClient/exceptions.py
# class SolrError(Exception):
#     """
#     Class to handle any issues that Solr Reports
#     """
# 
# class ConnectionError(SolrError):
#     """
#     Errors connecting to Solr
#     """


import time
    import requests
from .transportbase import TransportBase
from ..exceptions import SolrError, ConnectionError
            url = host + endpoint
        if headers is None:
            headers = {'content-type': 'application/json'}

        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.
        start = time.time()
        try:
            res = self.session.request(method, url, params=params, data=data, headers=headers, verify=False)
            duration = time.time() - start
            self.logger.debug("Request Completed in {} Seconds".format(round(duration, 2)))
        except requests.exceptions.SSLError as 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, 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:
            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:

# Path: SolrClient/transport/transportbase.py
# class TransportBase():
#     """
#     Base Transport Class
#     """
# 
#     #def __init__(self, solr, auth=(None, None), devel=None, host=None, router=PlainRouter, **kwargs):
#     def __init__(self, solr, auth=(None, None), devel=None, host=None, **kwargs):
#         self.logger = logging.getLogger(str(__package__))
#         self.auth = auth
#         self.host = host if type(host) is list else [host]
#         self._devel = devel
#         self._action_log = []
#         self._action_log_count = 1000
#         self.solr = solr
#         #self.router = router(self, host, **kwargs)
#         self.setup()
# 
#     def _add_to_action(self, action):
#         self._action_log.append(action)
#         if len(self._action_log) >= self._action_log_count:
#             self._action_log.pop(0)
# 
#     def _retry(function):
#         """
#         Internal mechanism to try to send data to multiple Solr Hosts if
#         the query fails on the first one.
#         """
# 
#         def inner(self, **kwargs):
#             last_exception = None
#             #for host in self.router.get_hosts(**kwargs):
#             for host in self.host:
#                 try:
#                     return function(self, host, **kwargs)
#                 except SolrError as e:
#                     self.logger.exception(e)
#                     raise
#                 except ConnectionError as e:
#                     self.logger.exception("Tried connecting to Solr, but couldn't because of the following exception.")
#                     if '401' in e.__str__():
#                         raise
#                     last_exception = e
#             # raise the last exception after contacting all hosts instead of returning None
#             if last_exception is not None:
#                 raise last_exception
#         return inner
# 
#     @_retry
#     def send_request(self, host, **kwargs):
#         if self._devel:
#             self._add_to_action({'host': host, 'params': dict(**kwargs)})
#         res_dict, c_inf = self._send(host, **kwargs)
#         if 'errors' in res_dict:
#             error = ", ".join([x for x in res_dict['errors'][0]['errorMessages']])
#             raise SolrError(error)
#         elif 'error' in res_dict:
#             raise SolrError(str(res_dict['error']))
#         return [res_dict, c_inf]
# 
#     def _log_connection_error(self, method, full_url, body, duration, status_code=None, exception=None):
#         self.logger.warning("Connection Error: [{}] {} - {} - {}".format(status_code, method, full_url, body))
# 
# Path: SolrClient/exceptions.py
# class SolrError(Exception):
#     """
#     Class to handle any issues that Solr Reports
#     """
# 
# class ConnectionError(SolrError):
#     """
#     Errors connecting to Solr
#     """


# Path: SolrClient/transport/transportrequests.py
import time
    import requests
from .transportbase import TransportBase
from ..exceptions import SolrError, ConnectionError

            url = host + endpoint
        if headers is None:
            headers = {'content-type': 'application/json'}

        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.
        start = time.time()
        try:
            res = self.session.request(method, url, params=params, data=data, headers=headers, verify=False)
            duration = time.time() - start
            self.logger.debug("Request Completed in {} Seconds".format(round(duration, 2)))
        except requests.exceptions.SSLError as 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, 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:
            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)