Allow session to return an error response object
Typically we want to have exceptions thrown when dealing with requests that return an HTTP error. However when looking at integrating the session object with other clients it becomes apparent that the exception handling is sufficiently different that it is best for now to let the existing error handling work. Add an option to return the failed request rather than raise an exception so existing clients can do there own error handling. Blueprint: session-propagation DocImpact: New session parameter. Change-Id: I63ea034e7c6eaaf42d4329526a902677a8dd709d
This commit is contained in:
@@ -114,7 +114,8 @@ class Session(object):
|
||||
@utils.positional(enforcement=utils.positional.WARN)
|
||||
def request(self, url, method, json=None, original_ip=None,
|
||||
user_agent=None, redirect=None, authenticated=None,
|
||||
endpoint_filter=None, auth=None, requests_auth=None, **kwargs):
|
||||
endpoint_filter=None, auth=None, requests_auth=None,
|
||||
raise_exc=True, **kwargs):
|
||||
"""Send an HTTP request with the specified characteristics.
|
||||
|
||||
Wrapper around `requests.Session.request` to handle tasks such as
|
||||
@@ -157,6 +158,9 @@ class Session(object):
|
||||
passed via kwarg because the `auth` kwarg
|
||||
collides with our own auth plugins. (optional)
|
||||
:type requests_auth: :class:`requests.auth.AuthBase`
|
||||
:param bool raise_exc: If True then raise an appropriate exception for
|
||||
failed HTTP requests. If False then return the
|
||||
request object. (optional, default True)
|
||||
:param kwargs: any other parameter that can be passed to
|
||||
requests.Session.request (such as `headers`). Except:
|
||||
'data' will be overwritten by the data in 'json' param.
|
||||
@@ -256,7 +260,7 @@ class Session(object):
|
||||
# returned by the requests library.
|
||||
resp.history = tuple(resp.history)
|
||||
|
||||
if resp.status_code >= 400:
|
||||
if raise_exc and resp.status_code >= 400:
|
||||
_logger.debug('Request returned failure status: %s',
|
||||
resp.status_code)
|
||||
raise exceptions.from_response(resp, method, url)
|
||||
|
@@ -393,6 +393,19 @@ class SessionAuthTests(utils.TestCase):
|
||||
endpoint_filter={'service_type': 'unknown',
|
||||
'interface': 'public'})
|
||||
|
||||
@httpretty.activate
|
||||
def test_raises_exc_only_when_asked(self):
|
||||
# A request that returns a HTTP error should by default raise an
|
||||
# exception by default, if you specify raise_exc=False then it will not
|
||||
|
||||
self.stub_url(httpretty.GET, status=401)
|
||||
|
||||
sess = client_session.Session()
|
||||
self.assertRaises(exceptions.Unauthorized, sess.get, self.TEST_URL)
|
||||
|
||||
resp = sess.get(self.TEST_URL, raise_exc=False)
|
||||
self.assertEqual(401, resp.status_code)
|
||||
|
||||
@httpretty.activate
|
||||
def test_passed_auth_plugin(self):
|
||||
passed = CalledAuthPlugin()
|
||||
|
Reference in New Issue
Block a user