Kaydet (Commit) 29b12cf0 authored tarafından Aanand Prasad's avatar Aanand Prasad Kaydeden (comit) Joffrey F

_url can take arbitrarily many arguments

Signed-off-by: 's avatarAanand Prasad <aanand.prasad@gmail.com>
üst 26e22bbd
......@@ -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."""
......
......@@ -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))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment