# Path: pyhaystack/client/ops/grid.py
# class BaseAuthOperation(state.HaystackOperation):
#     """
#     A base class authentication operations.
#     """
# 
#     def __init__(self, session, uri, retries=2, cache=False):
#         """
#         Initialise a request for the authenticating with the given URI and arguments.
# 
#         It also contains the state machine for reconnection if needed.
# 
#         :param session: Haystack HTTP session object.
#         :param uri: Possibly partial URI relative to the server base address
#                     to perform a query.  No arguments shall be given here.
#         :param retries: Number of retries permitted in case of failure.
#         :param cache: Whether or not to cache this result.  If True, the
#                       result is cached by the session object.
#         """
# 
#         super(BaseAuthOperation, self).__init__()
# 
#         self._retries = retries
#         self._session = session
#         self._uri = uri
#         self._headers = {}
# 
#         self._cache = cache
# 
#         self._state_machine = fysom.Fysom(
#             initial="init",
#             final="done",
#             events=[
#                 # Event             Current State       New State
#                 ("auth_ok", "init", "check_cache"),
#                 ("auth_not_ok", "init", "auth_attempt"),
#                 ("auth_ok", "auth_attempt", "check_cache"),
#                 ("auth_not_ok", "auth_attempt", "auth_failed"),
#                 ("auth_failed", "auth_attempt", "done"),
#                 ("cache_hit", "check_cache", "done"),
#                 ("cache_miss", "check_cache", "submit"),
#                 ("response_ok", "submit", "done"),
#                 ("exception", "*", "failed"),
#                 ("retry", "failed", "init"),
#                 ("abort", "failed", "done"),
#             ],
#             callbacks={
#                 "onretry": self._check_auth,
#                 "onenterauth_attempt": self._do_auth_attempt,
#                 "onenterauth_failed": self._do_auth_failed,
#                 "onentercheck_cache": self._do_check_cache,
#                 "onentersubmit": self._do_submit,
#                 "onenterfailed": self._do_fail_retry,
#                 "onenterdone": self._do_done,
#             },
#         )
# 
#     def go(self):
#         """
#         Start the request.
#         """
#         self._check_auth()
# 
#     def _check_auth(self, *args):
#         """
#         Check authentication.
#         """
#         # Are we logged in?
#         try:
#             if self._session.is_logged_in:
#                 self._state_machine.auth_ok()
#             else:
#                 self._state_machine.auth_not_ok()
#         except:  # Catch all exceptions to pass to caller.
#             self._log.debug("Authentication check fails", exc_info=1)
#             self._state_machine.exception(result=AsynchronousException())
# 
#     def _do_auth_attempt(self, event):
#         """
#         Tell the session object to log in, then call us back.
#         """
#         try:
#             self._session.authenticate(callback=self._on_authenticate)
#         except:  # Catch all exceptions to pass to caller.
#             self._state_machine.exception(result=AsynchronousException())
# 
#     def _on_authenticate(self, *args, **kwargs):
#         """
#         Retry the authentication check.
#         """
#         self._log.debug("Authenticated, trying again")
#         self.go()
# 
#     def _do_check_cache(self, event):
#         """
#         Implement if needed
#         """
#         self._state_machine.cache_miss()  # Nope
#         return
# 
#     def _on_response(self, response):
#         raise NotImplementedError()
# 
#     def _do_fail_retry(self, event):
#         """
#         Determine whether we retry or fail outright.
#         """
#         if self._retries > 0:
#             self._retries -= 1
#             self._state_machine.retry()
#         else:
#             self._state_machine.abort(result=event.result)
# 
#     def _do_auth_failed(self, event):
#         """
#         Raise and capture an authentication failure.
#         """
#         try:
#             raise AuthenticationProblem()
#         except:
#             self._state_machine.exception(result=AsynchronousException())
# 
#     def _do_done(self, event):
#         """
#         Return the result from the state machine.
#         """
#         self._done(event.result)
# 
# Path: pyhaystack/util/asyncexc.py
# class AsynchronousException(object):
#     def __init__(self):
#         self._exc_info = exc_info()
# 
#     def reraise(self):
#         reraise(*self._exc_info)


from requests.compat import urljoin
from ....ops.grid import BaseAuthOperation
from .....util.asyncexc import AsynchronousException
    from urllib.parse import quote as quote_uri
    from urllib import quote as quote_uri
import io
import pandas as pd

                self._uri, api=False, headers=self._headers, callback=self._on_response
            )
        except:  # Catch all exceptions to pass to caller.
            self._log.debug("Get fails", exc_info=1)
            self._state_machine.exception(result=AsynchronousException())

    def _on_response(self, response):
        """
        Process the response given back by the HTTP server.
        """
        try:
            # Does the session want to invoke any relevant hooks?
            # This allows a session to detect problems in the session and
            # abort the operation.
            if hasattr(self._session, "_on_http_grid_response"):
                self._session._on_http_grid_response(response)

            # Process the HTTP error, if any.
            if isinstance(response, AsynchronousException):
                response.reraise()

            # If we're expecting a raw response back, then just hand the
            # request object back and finish here.
            self._file_like_object = io.StringIO(response.body.decode("UTF-8"))
            df = pd.read_csv(self._file_like_object)
            self._state_machine.response_ok(result=df)
            return

        except:  # Catch all exceptions for the caller.
            self._log.debug("Parse fails", exc_info=1)

# Path: pyhaystack/client/ops/grid.py
# class BaseAuthOperation(state.HaystackOperation):
#     """
#     A base class authentication operations.
#     """
# 
#     def __init__(self, session, uri, retries=2, cache=False):
#         """
#         Initialise a request for the authenticating with the given URI and arguments.
# 
#         It also contains the state machine for reconnection if needed.
# 
#         :param session: Haystack HTTP session object.
#         :param uri: Possibly partial URI relative to the server base address
#                     to perform a query.  No arguments shall be given here.
#         :param retries: Number of retries permitted in case of failure.
#         :param cache: Whether or not to cache this result.  If True, the
#                       result is cached by the session object.
#         """
# 
#         super(BaseAuthOperation, self).__init__()
# 
#         self._retries = retries
#         self._session = session
#         self._uri = uri
#         self._headers = {}
# 
#         self._cache = cache
# 
#         self._state_machine = fysom.Fysom(
#             initial="init",
#             final="done",
#             events=[
#                 # Event             Current State       New State
#                 ("auth_ok", "init", "check_cache"),
#                 ("auth_not_ok", "init", "auth_attempt"),
#                 ("auth_ok", "auth_attempt", "check_cache"),
#                 ("auth_not_ok", "auth_attempt", "auth_failed"),
#                 ("auth_failed", "auth_attempt", "done"),
#                 ("cache_hit", "check_cache", "done"),
#                 ("cache_miss", "check_cache", "submit"),
#                 ("response_ok", "submit", "done"),
#                 ("exception", "*", "failed"),
#                 ("retry", "failed", "init"),
#                 ("abort", "failed", "done"),
#             ],
#             callbacks={
#                 "onretry": self._check_auth,
#                 "onenterauth_attempt": self._do_auth_attempt,
#                 "onenterauth_failed": self._do_auth_failed,
#                 "onentercheck_cache": self._do_check_cache,
#                 "onentersubmit": self._do_submit,
#                 "onenterfailed": self._do_fail_retry,
#                 "onenterdone": self._do_done,
#             },
#         )
# 
#     def go(self):
#         """
#         Start the request.
#         """
#         self._check_auth()
# 
#     def _check_auth(self, *args):
#         """
#         Check authentication.
#         """
#         # Are we logged in?
#         try:
#             if self._session.is_logged_in:
#                 self._state_machine.auth_ok()
#             else:
#                 self._state_machine.auth_not_ok()
#         except:  # Catch all exceptions to pass to caller.
#             self._log.debug("Authentication check fails", exc_info=1)
#             self._state_machine.exception(result=AsynchronousException())
# 
#     def _do_auth_attempt(self, event):
#         """
#         Tell the session object to log in, then call us back.
#         """
#         try:
#             self._session.authenticate(callback=self._on_authenticate)
#         except:  # Catch all exceptions to pass to caller.
#             self._state_machine.exception(result=AsynchronousException())
# 
#     def _on_authenticate(self, *args, **kwargs):
#         """
#         Retry the authentication check.
#         """
#         self._log.debug("Authenticated, trying again")
#         self.go()
# 
#     def _do_check_cache(self, event):
#         """
#         Implement if needed
#         """
#         self._state_machine.cache_miss()  # Nope
#         return
# 
#     def _on_response(self, response):
#         raise NotImplementedError()
# 
#     def _do_fail_retry(self, event):
#         """
#         Determine whether we retry or fail outright.
#         """
#         if self._retries > 0:
#             self._retries -= 1
#             self._state_machine.retry()
#         else:
#             self._state_machine.abort(result=event.result)
# 
#     def _do_auth_failed(self, event):
#         """
#         Raise and capture an authentication failure.
#         """
#         try:
#             raise AuthenticationProblem()
#         except:
#             self._state_machine.exception(result=AsynchronousException())
# 
#     def _do_done(self, event):
#         """
#         Return the result from the state machine.
#         """
#         self._done(event.result)
# 
# Path: pyhaystack/util/asyncexc.py
# class AsynchronousException(object):
#     def __init__(self):
#         self._exc_info = exc_info()
# 
#     def reraise(self):
#         reraise(*self._exc_info)


# Path: pyhaystack/client/mixins/vendor/niagara/bql.py
from requests.compat import urljoin
from ....ops.grid import BaseAuthOperation
from .....util.asyncexc import AsynchronousException
    from urllib.parse import quote as quote_uri
    from urllib import quote as quote_uri
import io
import pandas as pd

                self._uri, api=False, headers=self._headers, callback=self._on_response
            )
        except:  # Catch all exceptions to pass to caller.
            self._log.debug("Get fails", exc_info=1)
            self._state_machine.exception(result=AsynchronousException())

    def _on_response(self, response):
        """
        Process the response given back by the HTTP server.
        """
        try:
            # Does the session want to invoke any relevant hooks?
            # This allows a session to detect problems in the session and
            # abort the operation.
            if hasattr(self._session, "_on_http_grid_response"):
                self._session._on_http_grid_response(response)

            # Process the HTTP error, if any.
            if isinstance(response, AsynchronousException):
                response.reraise()

            # If we're expecting a raw response back, then just hand the
            # request object back and finish here.
            self._file_like_object = io.StringIO(response.body.decode("UTF-8"))
            df = pd.read_csv(self._file_like_object)
            self._state_machine.response_ok(result=df)
            return

        except:  # Catch all exceptions for the caller.
            self._log.debug("Parse fails", exc_info=1)

            self._state_machine.exception(result=AsynchronousException())
