Skip to content

Commit 29b12cf

Browse files
aanandshin-
authored andcommitted
_url can take arbitrarily many arguments
Signed-off-by: Aanand Prasad <[email protected]>
1 parent 26e22bb commit 29b12cf

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

docker/client.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,22 @@ def _get(self, url, **kwargs):
111111
def _delete(self, url, **kwargs):
112112
return self.delete(url, **self._set_request_timeout(kwargs))
113113

114-
def _url(self, pathfmt, resource_id=None, versioned_api=True):
115-
if resource_id and not isinstance(resource_id, six.string_types):
116-
raise ValueError(
117-
'Expected a resource ID string but found {0} ({1}) '
118-
'instead'.format(resource_id, type(resource_id))
119-
)
120-
elif resource_id:
121-
resource_id = six.moves.urllib.parse.quote_plus(resource_id)
114+
def _url(self, pathfmt, *args, **kwargs):
115+
for arg in args:
116+
if not isinstance(arg, six.string_types):
117+
raise ValueError(
118+
'Expected a string but found {0} ({1}) '
119+
'instead'.format(arg, type(arg))
120+
)
121+
122+
args = map(six.moves.urllib.parse.quote_plus, args)
122123

123-
if versioned_api:
124+
if kwargs.get('versioned_api', True):
124125
return '{0}/v{1}{2}'.format(
125-
self.base_url, self._version, pathfmt.format(resource_id)
126+
self.base_url, self._version, pathfmt.format(*args)
126127
)
127128
else:
128-
return '{0}{1}'.format(self.base_url, pathfmt.format(resource_id))
129+
return '{0}{1}'.format(self.base_url, pathfmt.format(*args))
129130

130131
def _raise_for_status(self, response, explanation=None):
131132
"""Raises stored :class:`APIError`, if one occurred."""

tests/test.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ def fake_put(self, url, *args, **kwargs):
104104
def fake_delete(self, url, *args, **kwargs):
105105
return fake_request('DELETE', url, *args, **kwargs)
106106

107-
url_prefix = 'http+docker://localunixsocket/v{0}/'.format(
107+
url_base = 'http+docker://localunixsocket/'
108+
url_prefix = '{0}v{1}/'.format(
109+
url_base,
108110
docker.constants.DEFAULT_DOCKER_API_VERSION)
109111

110112

@@ -174,6 +176,14 @@ def test_url_valid_resource(self):
174176
url, '{0}{1}'.format(url_prefix, 'hello/somename/world')
175177
)
176178

179+
url = self.client._url(
180+
'/hello/{0}/world/{1}', 'somename', 'someothername'
181+
)
182+
self.assertEqual(
183+
url,
184+
'{0}{1}'.format(url_prefix, 'hello/somename/world/someothername')
185+
)
186+
177187
url = self.client._url('/hello/{0}/world', '/some?name')
178188
self.assertEqual(
179189
url, '{0}{1}'.format(url_prefix, 'hello/%2Fsome%3Fname/world')
@@ -187,8 +197,13 @@ def test_url_no_resource(self):
187197
url = self.client._url('/simple')
188198
self.assertEqual(url, '{0}{1}'.format(url_prefix, 'simple'))
189199

190-
url = self.client._url('/simple', None)
191-
self.assertEqual(url, '{0}{1}'.format(url_prefix, 'simple'))
200+
def test_url_unversioned_api(self):
201+
url = self.client._url(
202+
'/hello/{0}/world', 'somename', versioned_api=False
203+
)
204+
self.assertEqual(
205+
url, '{0}{1}'.format(url_base, 'hello/somename/world')
206+
)
192207

193208
#########################
194209
# INFORMATION TESTS #
@@ -202,6 +217,15 @@ def test_version(self):
202217
timeout=DEFAULT_TIMEOUT_SECONDS
203218
)
204219

220+
def test_version_no_api_version(self):
221+
self.client.version(False)
222+
223+
fake_request.assert_called_with(
224+
'GET',
225+
url_base + 'version',
226+
timeout=DEFAULT_TIMEOUT_SECONDS
227+
)
228+
205229
def test_retrieve_server_version(self):
206230
client = docker.Client(version="auto")
207231
self.assertTrue(isinstance(client._version, six.string_types))

0 commit comments

Comments
 (0)