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:
parent
8419ae30db
commit
0227207771
@ -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]]
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user