diff options
author | Aanand Prasad <aanand.prasad@gmail.com> | 2015-09-25 20:10:16 +0200 |
---|---|---|
committer | Joffrey F <joffrey@docker.com> | 2015-09-25 14:25:00 -0700 |
commit | 29b12cf007bf59c15ffbb6f508b41fc7eaa41c41 (patch) | |
tree | 36e45f2f20f1f5ab99c417a7518b0a806179cdfb | |
parent | 26e22bbd391c9712db643d85087a5113d11c6c82 (diff) | |
download | docker-py-29b12cf007bf59c15ffbb6f508b41fc7eaa41c41.tar.gz |
_url can take arbitrarily many arguments
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
-rw-r--r-- | docker/client.py | 23 | ||||
-rw-r--r-- | tests/test.py | 30 |
2 files changed, 39 insertions, 14 deletions
diff --git a/docker/client.py b/docker/client.py index 58d0496..9decd61 100644 --- a/docker/client.py +++ b/docker/client.py @@ -111,21 +111,22 @@ class Client( def _delete(self, url, **kwargs): return self.delete(url, **self._set_request_timeout(kwargs)) - def _url(self, pathfmt, resource_id=None, versioned_api=True): - if resource_id and not isinstance(resource_id, six.string_types): - raise ValueError( - 'Expected a resource ID string but found {0} ({1}) ' - 'instead'.format(resource_id, type(resource_id)) - ) - elif resource_id: - resource_id = six.moves.urllib.parse.quote_plus(resource_id) + def _url(self, pathfmt, *args, **kwargs): + for arg in args: + if not isinstance(arg, six.string_types): + raise ValueError( + 'Expected a string but found {0} ({1}) ' + 'instead'.format(arg, type(arg)) + ) + + args = map(six.moves.urllib.parse.quote_plus, args) - if versioned_api: + if kwargs.get('versioned_api', True): return '{0}/v{1}{2}'.format( - self.base_url, self._version, pathfmt.format(resource_id) + self.base_url, self._version, pathfmt.format(*args) ) else: - return '{0}{1}'.format(self.base_url, pathfmt.format(resource_id)) + return '{0}{1}'.format(self.base_url, pathfmt.format(*args)) def _raise_for_status(self, response, explanation=None): """Raises stored :class:`APIError`, if one occurred.""" diff --git a/tests/test.py b/tests/test.py index 8ed5c14..4a70b26 100644 --- a/tests/test.py +++ b/tests/test.py @@ -104,7 +104,9 @@ def fake_put(self, url, *args, **kwargs): def fake_delete(self, url, *args, **kwargs): return fake_request('DELETE', url, *args, **kwargs) -url_prefix = 'http+docker://localunixsocket/v{0}/'.format( +url_base = 'http+docker://localunixsocket/' +url_prefix = '{0}v{1}/'.format( + url_base, docker.constants.DEFAULT_DOCKER_API_VERSION) @@ -174,6 +176,14 @@ class DockerClientTest(Cleanup, base.BaseTestCase): url, '{0}{1}'.format(url_prefix, 'hello/somename/world') ) + url = self.client._url( + '/hello/{0}/world/{1}', 'somename', 'someothername' + ) + self.assertEqual( + url, + '{0}{1}'.format(url_prefix, 'hello/somename/world/someothername') + ) + url = self.client._url('/hello/{0}/world', '/some?name') self.assertEqual( url, '{0}{1}'.format(url_prefix, 'hello/%2Fsome%3Fname/world') @@ -187,8 +197,13 @@ class DockerClientTest(Cleanup, base.BaseTestCase): url = self.client._url('/simple') self.assertEqual(url, '{0}{1}'.format(url_prefix, 'simple')) - url = self.client._url('/simple', None) - self.assertEqual(url, '{0}{1}'.format(url_prefix, 'simple')) + def test_url_unversioned_api(self): + url = self.client._url( + '/hello/{0}/world', 'somename', versioned_api=False + ) + self.assertEqual( + url, '{0}{1}'.format(url_base, 'hello/somename/world') + ) ######################### # INFORMATION TESTS # @@ -202,6 +217,15 @@ class DockerClientTest(Cleanup, base.BaseTestCase): timeout=DEFAULT_TIMEOUT_SECONDS ) + def test_version_no_api_version(self): + self.client.version(False) + + fake_request.assert_called_with( + 'GET', + url_base + 'version', + timeout=DEFAULT_TIMEOUT_SECONDS + ) + def test_retrieve_server_version(self): client = docker.Client(version="auto") self.assertTrue(isinstance(client._version, six.string_types)) |