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
This commit is contained in:
Jamie Lennox 2013-07-31 16:00:58 +10:00
parent 8419ae30db
commit 0227207771
2 changed files with 10 additions and 1 deletions

View File

@ -4,6 +4,8 @@
Exception definitions. Exception definitions.
""" """
from keystoneclient.openstack.common import jsonutils
class CommandError(Exception): class CommandError(Exception):
pass pass
@ -143,7 +145,7 @@ _code_map = dict((c.http_status, c) for c in [BadRequest,
ServiceUnavailable]) ServiceUnavailable])
def from_response(response, body): def from_response(response, body=None):
""" """
Return an instance of an ClientException or subclass Return an instance of an ClientException or subclass
based on an requests response. based on an requests response.
@ -155,6 +157,12 @@ def from_response(response, body):
raise exception_from_response(resp, resp.text) raise exception_from_response(resp, resp.text)
""" """
cls = _code_map.get(response.status_code, ClientException) 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 body:
if hasattr(body, 'keys'): if hasattr(body, 'keys'):
error = body[body.keys()[0]] error = body[body.keys()[0]]

View File

@ -3,6 +3,7 @@ hacking>=0.5.6,<0.7
coverage>=3.6 coverage>=3.6
discover discover
fixtures>=0.3.12 fixtures>=0.3.12
httpretty>=0.6.3
keyring>=1.6.1 keyring>=1.6.1
mock>=0.8.0 mock>=0.8.0
mox>=0.5.3 mox>=0.5.3