From 022720777132fa0be4ec13fbe29501c69c4f18b7 Mon Sep 17 00:00:00 2001 From: Jamie Lennox Date: Wed, 31 Jul 2013 16:00:58 +1000 Subject: [PATCH] Extract basic request call Create a function out of the standard request call that handles common headers like user agent and logging. This makes future changes easier to digest. Change-Id: Ia25f997df64efdce27c8fb815e544922940145c3 --- keystoneclient/exceptions.py | 10 +++++++++- test-requirements.txt | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/keystoneclient/exceptions.py b/keystoneclient/exceptions.py index 833b3925..d3a7c321 100644 --- a/keystoneclient/exceptions.py +++ b/keystoneclient/exceptions.py @@ -4,6 +4,8 @@ Exception definitions. """ +from keystoneclient.openstack.common import jsonutils + class CommandError(Exception): pass @@ -143,7 +145,7 @@ _code_map = dict((c.http_status, c) for c in [BadRequest, ServiceUnavailable]) -def from_response(response, body): +def from_response(response, body=None): """ Return an instance of an ClientException or subclass based on an requests response. @@ -155,6 +157,12 @@ def from_response(response, body): raise exception_from_response(resp, resp.text) """ cls = _code_map.get(response.status_code, ClientException) + if body is None: + try: + body = jsonutils.loads(response.text) + except Exception: + body = response.text + if body: if hasattr(body, 'keys'): error = body[body.keys()[0]] diff --git a/test-requirements.txt b/test-requirements.txt index 0f61dc1b..2525fa8a 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -3,6 +3,7 @@ hacking>=0.5.6,<0.7 coverage>=3.6 discover fixtures>=0.3.12 +httpretty>=0.6.3 keyring>=1.6.1 mock>=0.8.0 mox>=0.5.3