Add _single_path method for single resources path
It makes sure that resource id is not None, so that e.g. DELETE request is not accidentally called on a collection URL. Also _path method stub is now in base.Manager, to be overriden by child classes.
This commit is contained in:
parent
9df8cbf19b
commit
b8afcf52fd
@ -47,6 +47,26 @@ class Manager(object):
|
||||
def __init__(self, api):
|
||||
self.api = api
|
||||
|
||||
@staticmethod
|
||||
def _path(id=None):
|
||||
"""Helper method to be defined in subclasses. It returns the
|
||||
resource/collection path. If id is given, then single resource
|
||||
path is returned. Otherwise the collection path is returned.
|
||||
|
||||
:param id: id of the resource (optional)
|
||||
"""
|
||||
raise NotImplementedError("_path method not implemented.")
|
||||
|
||||
def _single_path(self, id):
|
||||
"""This is like the _path method, but it asserts that the rack_id
|
||||
parameter is not None. This is useful e.g. when you want to make sure
|
||||
that you can't issue a DELETE request on a collection URL.
|
||||
"""
|
||||
if not id:
|
||||
raise ValueError("{0} id for deletion must not be null."
|
||||
.format(self.resource_class))
|
||||
return self._path(id)
|
||||
|
||||
def _create(self, url, body):
|
||||
resp, body = self.api.json_request('POST', url, body=body)
|
||||
if body:
|
||||
|
@ -42,3 +42,14 @@ class ManagerTest(tutils.TestCase):
|
||||
self.m._list.assert_called_with('url', response_key='response_key',
|
||||
obj_class='obj_class',
|
||||
body='body', expect_single=True)
|
||||
|
||||
def test_path(self):
|
||||
self.assertRaises(NotImplementedError, self.m._path)
|
||||
|
||||
def test_single_path(self):
|
||||
self.m._path = mock.Mock(return_value='/v1/somethings/42')
|
||||
self.m._single_path(42)
|
||||
self.m._path.assert_called_with(42)
|
||||
|
||||
def test_single_path_without_id(self):
|
||||
self.assertRaises(ValueError, self.m._single_path, None)
|
||||
|
Loading…
x
Reference in New Issue
Block a user