Browse Source

raise_exc default in Adapter

It can be annoying to have to say raise_exc=False (or use try/except) on
every call when talking to an API where 4xx response codes are
useful/normal/informative or where the preferred coding style is to use
conditionals rather than try/except.

With this change, the Adapter constructor takes a new kwarg, raise_exc.
It defaults to None, and the existing behavior is unchanged.  If set to
a boolean value, that is used as the default for requests.  Specifying
raise_exc to the primitives (get, head, put, post, patch, delete,
request) at any point along the chain will still take precedence.

Change-Id: Ie291c3cb891467728d8ca33cf62afdab37c82f34
Closes-Bug: #1776501
Eric Fried 10 months ago
parent
commit
51bfa030b1
2 changed files with 36 additions and 1 deletions
  1. 9
    1
      keystoneauth1/adapter.py
  2. 27
    0
      keystoneauth1/tests/unit/test_session.py

+ 9
- 1
keystoneauth1/adapter.py View File

@@ -88,6 +88,10 @@ class Adapter(object):
88 88
                                         should be retried (optional,
89 89
                                         defaults to HTTP 503, has no effect
90 90
                                         when status_code_retries is 0).
91
+    :param bool raise_exc: If True, requests returning failing HTTP responses
92
+                           will raise an exception; if False, the response is
93
+                           returned. This can be overridden on a per-request
94
+                           basis via the kwarg of the same name.
91 95
     """
92 96
 
93 97
     client_name = None
@@ -102,7 +106,7 @@ class Adapter(object):
102 106
                  global_request_id=None,
103 107
                  min_version=None, max_version=None,
104 108
                  default_microversion=None, status_code_retries=None,
105
-                 retriable_status_codes=None):
109
+                 retriable_status_codes=None, raise_exc=None):
106 110
         if version and (min_version or max_version):
107 111
             raise TypeError(
108 112
                 "version is mutually exclusive with min_version and"
@@ -131,6 +135,7 @@ class Adapter(object):
131 135
         self.default_microversion = default_microversion
132 136
         self.status_code_retries = status_code_retries
133 137
         self.retriable_status_codes = retriable_status_codes
138
+        self.raise_exc = raise_exc
134 139
 
135 140
         self.global_request_id = global_request_id
136 141
 
@@ -202,6 +207,9 @@ class Adapter(object):
202 207
             kwargs.setdefault('headers', {}).setdefault(
203 208
                 "X-OpenStack-Request-ID", self.global_request_id)
204 209
 
210
+        if self.raise_exc is not None:
211
+            kwargs.setdefault('raise_exc', self.raise_exc)
212
+
205 213
         return self.session.request(url, method, **kwargs)
206 214
 
207 215
     def get_token(self, auth=None):

+ 27
- 0
keystoneauth1/tests/unit/test_session.py View File

@@ -1580,6 +1580,33 @@ class AdapterTest(utils.TestCase):
1580 1580
         validate({'default_microversion': '1.2'}, {'microversion': '1.5'},
1581 1581
                  {'microversion': '1.5'})
1582 1582
 
1583
+    def test_raise_exc_override(self):
1584
+        sess = client_session.Session()
1585
+        url = 'http://url'
1586
+
1587
+        def validate(adap_kwargs, get_kwargs, exp_kwargs):
1588
+            with mock.patch.object(sess, 'request') as m:
1589
+                adapter.Adapter(sess, **adap_kwargs).get(url, **get_kwargs)
1590
+                m.assert_called_once_with(url, 'GET', endpoint_filter={},
1591
+                                          **exp_kwargs)
1592
+
1593
+        # No raise_exc in Adapter or get()
1594
+        validate({}, {}, {})
1595
+
1596
+        # Set in Adapter, unset in get()
1597
+        validate({'raise_exc': True}, {}, {'raise_exc': True})
1598
+        validate({'raise_exc': False}, {}, {'raise_exc': False})
1599
+
1600
+        # Unset in Adapter, set in get()
1601
+        validate({}, {'raise_exc': True}, {'raise_exc': True})
1602
+        validate({}, {'raise_exc': False}, {'raise_exc': False})
1603
+
1604
+        # Setting in get() overrides the one in Adapter
1605
+        validate({'raise_exc': True}, {'raise_exc': False},
1606
+                 {'raise_exc': False})
1607
+        validate({'raise_exc': False}, {'raise_exc': True},
1608
+                 {'raise_exc': True})
1609
+
1583 1610
 
1584 1611
 class TCPKeepAliveAdapterTest(utils.TestCase):
1585 1612
 

Loading…
Cancel
Save