From 4f1d25e4896a94f1a9760b309de7e86093da472a Mon Sep 17 00:00:00 2001 From: Jamie Lennox Date: Fri, 28 Mar 2014 10:15:11 +1000 Subject: [PATCH] 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 --- keystoneclient/session.py | 8 ++++++-- keystoneclient/tests/test_session.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/keystoneclient/session.py b/keystoneclient/session.py index b0f473494..9b456b24c 100644 --- a/keystoneclient/session.py +++ b/keystoneclient/session.py @@ -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) diff --git a/keystoneclient/tests/test_session.py b/keystoneclient/tests/test_session.py index c86bc3554..869d4396b 100644 --- a/keystoneclient/tests/test_session.py +++ b/keystoneclient/tests/test_session.py @@ -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()