import requests
from requests.exceptions import RequestException
+from requests.exceptions import JSONDecodeError
# Own modules
from . import DEFAULT_CONSUL_API_KV_ROOTPATH
from .errors import ConsulHandlerError
from .errors import ConsulRequestError
from .errors import ConsulApiError
+from .errors import ConsulApiNotAuthorizedError
+from .errors import ConsulApiNotFoundError
from .xlate import XLATOR
-__version__ = '0.2.0'
+__version__ = '0.3.0'
LOG = logging.getLogger(__name__)
LOGLEVEL_REQUESTS_SET = False
if response.ok:
return
- err = response.json()
code = response.status_code
- msg = err['error']
- LOG.debug(_('Got an error response code {code}: {msg}').format(code=code, msg=msg))
+ try:
+ err = response.json()
+ if 'error' in err:
+ msg = err['error']
+ else:
+ msg = pp(err)
+ except JSONDecodeError:
+ msg = response.text
+ if not msg:
+ msg = response.reason
+
+ if self.verbose > 2:
+ LOG.debug(_('Got an error response code {code}: {msg}').format(code=code, msg=msg))
+
+ if response.status_code == 401:
+ raise ConsulApiNotAuthorizedError(code, msg, url)
+ if response.status_code == 404:
+ raise ConsulApiNotFoundError(code, msg, url)
raise ConsulApiError(code, msg, url)
# -------------------------------------------------------------------------
def get_keys(self, key=''):
"""Return a recursive list of all keys below the given start point."""
- result = self.perform_request(key, keys=True)
+ try:
+ result = self.perform_request(key, keys=True)
+ except ConsulApiNotFoundError as e:
+ LOG.error(str(e))
+ return []
+
if not result:
return []
if the key does not exists, it returns None.
"""
- result = self.perform_request(key)
+ try:
+ result = self.perform_request(key)
+ except ConsulApiNotFoundError as e:
+ if self.verbose > 1:
+ LOG.debug(str(e))
+ return None
+
if not result:
return None
first_data = result[0]
- if not 'Value':
+ if 'Value' not in first_data:
return None
encoded_data = first_data['Value']
json_data = base64.b64decode(encoded_data)
value = json.loads(str(json_data))
+ if self.verbose > 2:
+ LOG.debug(_('Got data from Consul for key {!r}:').format(key) + '\n' + pp(value))
+
return value
# -------------------------------------------------------------------------
from .xlate import XLATOR
-__version__ = '1.5.0'
+__version__ = '1.6.0'
_ = XLATOR.gettext
ngettext = XLATOR.ngettext
return msg
+# =============================================================================
+class ConsulApiNotAuthorizedError(ConsulApiError):
+ """The authorization information provided is not correct."""
+
+ pass
+
+
+# =============================================================================
+class ConsulApiNotFoundError(ConsulApiError):
+ """The authorization information provided is not correct."""
+
+ pass
+
+
# =============================================================================
class ConsulRequestError(ConsulHandlerError):
"""Raised, when some other exceptions occured on a HTTP(S) request."""