Merge "Further simplify microversion utils"

This commit is contained in:
Zuul 2019-07-23 18:52:10 +00:00 committed by Gerrit Code Review
commit 18c3ebc008
2 changed files with 16 additions and 12 deletions

View File

@ -102,25 +102,24 @@ def min_version_string():
# Based on code in twisted # Based on code in twisted
# https://github.com/twisted/twisted/blob/trunk/twisted/python/deprecate.py # https://github.com/twisted/twisted/blob/trunk/twisted/python/deprecate.py
def _fully_qualified_name(handler): def _fully_qualified_name(handler):
"""Return the name of a function or class used as an HTTP API handler, """Return the name of a function used as an HTTP API handler, qualified by
qualified by module name. module name.
""" """
if inspect.isfunction(handler) or inspect.isclass(handler): if inspect.isfunction(handler):
module_name = handler.__module__ module_name = handler.__module__
return "%s.%s" % (module_name, handler.__name__) return "%s.%s" % (module_name, handler.__name__)
# We got an object method or module. This is a coding error. # We got a class, object method, or module. This is a coding error.
raise TypeError("_fully_qualified_name received bad handler type. " raise TypeError("_fully_qualified_name received bad handler type. "
"Module-level class or function required.") "Module-level function required.")
def _find_method(f, version, status_code): def _find_method(qualified_name, version, status_code):
"""Look in VERSIONED_METHODS for method with right name matching version. """Look in VERSIONED_METHODS for method with right name matching version.
If no match is found a HTTPError corresponding to status_code will If no match is found a HTTPError corresponding to status_code will
be returned. be returned.
""" """
qualified_name = _fully_qualified_name(f)
# A KeyError shouldn't be possible here, but let's be robust # A KeyError shouldn't be possible here, but let's be robust
# just in case. # just in case.
method_list = VERSIONED_METHODS.get(qualified_name, []) method_list = VERSIONED_METHODS.get(qualified_name, [])
@ -160,7 +159,8 @@ def version_handler(min_ver, max_ver=None, status_code=404):
def decorated_func(req, *args, **kwargs): def decorated_func(req, *args, **kwargs):
version = req.environ[MICROVERSION_ENVIRON] version = req.environ[MICROVERSION_ENVIRON]
return _find_method(f, version, status_code)(req, *args, **kwargs) return _find_method(
qualified_name, version, status_code)(req, *args, **kwargs)
# Sort highest min version to beginning of list. # Sort highest min version to beginning of list.
VERSIONED_METHODS[qualified_name].sort(key=lambda x: x[0], VERSIONED_METHODS[qualified_name].sort(key=lambda x: x[0],

View File

@ -31,12 +31,16 @@ def handler():
class TestMicroversionFindMethod(testtools.TestCase): class TestMicroversionFindMethod(testtools.TestCase):
def test_method_405(self): def test_method_405(self):
self.assertRaises(webob.exc.HTTPMethodNotAllowed, self.assertRaises(
microversion._find_method, handler, '1.1', 405) webob.exc.HTTPMethodNotAllowed,
microversion._find_method,
microversion._fully_qualified_name(handler), '1.1', 405)
def test_method_404(self): def test_method_404(self):
self.assertRaises(webob.exc.HTTPNotFound, self.assertRaises(
microversion._find_method, handler, '1.1', 404) webob.exc.HTTPNotFound,
microversion._find_method,
microversion._fully_qualified_name(handler), '1.1', 404)
class TestMicroversionDecoration(testtools.TestCase): class TestMicroversionDecoration(testtools.TestCase):