From dfe0240f2b039098b1e67e39c91a4d3057d56575 Mon Sep 17 00:00:00 2001 From: Takuya Kitazawa Date: Thu, 6 Dec 2018 17:40:32 +0900 Subject: [PATCH] Make the library runnable w/ and w/o requests_kerberos --- prestodb/__init__.py | 5 +++++ prestodb/client.py | 17 +++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/prestodb/__init__.py b/prestodb/__init__.py index f3b3f49..9bbd65c 100644 --- a/prestodb/__init__.py +++ b/prestodb/__init__.py @@ -13,6 +13,11 @@ from __future__ import division from __future__ import print_function +try: + from .auth import KerberosAuthentication +except ImportError: + pass + from . import dbapi from . import client from . import constants diff --git a/prestodb/client.py b/prestodb/client.py index edb642b..bab44e4 100644 --- a/prestodb/client.py +++ b/prestodb/client.py @@ -37,7 +37,6 @@ from __future__ import print_function import os -from requests_kerberos.exceptions import KerberosExchangeError from typing import Any, Dict, List, Optional, Text, Tuple, Union # NOQA for mypy types import requests @@ -240,6 +239,16 @@ def __init__( raise ValueError('cannot use authentication with HTTP') self._auth.set_http_session(self._http_session) + default_exceptions = ( + PrestoRequest.http.ConnectionError, # type: ignore + PrestoRequest.http.Timeout, # type: ignore + ) + try: + from requests_kerberos.exceptions import KerberosExchangeError + self._exceptions = default_exceptions + (KerberosExchangeError,) + except ImportError: + self._exceptions = default_exceptions + self._redirect_handler = redirect_handler self._request_timeout = request_timeout self._handle_retry = handle_retry @@ -298,11 +307,7 @@ def max_attempts(self, value): with_retry = exceptions.retry_with( self._handle_retry, - exceptions=( - KerberosExchangeError, - PrestoRequest.http.ConnectionError, # type: ignore - PrestoRequest.http.Timeout, # type: ignore - ), + exceptions=self._exceptions, conditions=( # need retry when there is no exception but the status code is 503 lambda response: getattr(response, 'status_code', None) == 503,