diff options
author | Aanand Prasad <aanand.prasad@gmail.com> | 2016-07-22 14:55:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-22 14:55:19 +0100 |
commit | 01cb969215fc91fa99642bd9718bfa0ce2a66a0c (patch) | |
tree | 62ae586487b6177c6796adcf649395e56c958796 | |
parent | e15ba7400a457ea7b605d08131eb0148c3f7528f (diff) | |
parent | bd73225e14265dae4e2de1b15ad4a0c7fbc3e5ba (diff) | |
download | docker-py-01cb969215fc91fa99642bd9718bfa0ce2a66a0c.tar.gz |
Merge pull request #1125 from bfirsh/user-agent
Set custom user agent on client
-rw-r--r-- | docker/client.py | 4 | ||||
-rw-r--r-- | docker/constants.py | 3 | ||||
-rw-r--r-- | docs/api.md | 1 | ||||
-rw-r--r-- | tests/unit/api_test.py | 30 |
4 files changed, 37 insertions, 1 deletions
diff --git a/docker/client.py b/docker/client.py index 6ca9e57..c3e5874 100644 --- a/docker/client.py +++ b/docker/client.py @@ -50,7 +50,8 @@ class Client( api.VolumeApiMixin, api.NetworkApiMixin): def __init__(self, base_url=None, version=None, - timeout=constants.DEFAULT_TIMEOUT_SECONDS, tls=False): + timeout=constants.DEFAULT_TIMEOUT_SECONDS, tls=False, + user_agent=constants.DEFAULT_USER_AGENT): super(Client, self).__init__() if tls and not base_url: @@ -60,6 +61,7 @@ class Client( self.base_url = base_url self.timeout = timeout + self.headers['User-Agent'] = user_agent self._auth_configs = auth.load_config() diff --git a/docker/constants.py b/docker/constants.py index 0388f70..904d50e 100644 --- a/docker/constants.py +++ b/docker/constants.py @@ -1,4 +1,5 @@ import sys +from .version import version DEFAULT_DOCKER_API_VERSION = '1.22' DEFAULT_TIMEOUT_SECONDS = 60 @@ -12,3 +13,5 @@ INSECURE_REGISTRY_DEPRECATION_WARNING = \ 'is deprecated and non-functional. Please remove it.' IS_WINDOWS_PLATFORM = (sys.platform == 'win32') + +DEFAULT_USER_AGENT = "docker-py/{0}".format(version) diff --git a/docs/api.md b/docs/api.md index 41c5e6c..e058deb 100644 --- a/docs/api.md +++ b/docs/api.md @@ -16,6 +16,7 @@ is hosted. to use the API version provided by the server. * timeout (int): The HTTP request timeout, in seconds. * tls (bool or [TLSConfig](tls.md#TLSConfig)): Equivalent CLI options: `docker --tls ...` +* user_agent (str): Set a custom user agent for requests to the server. **** diff --git a/tests/unit/api_test.py b/tests/unit/api_test.py index 34bf14f..696c073 100644 --- a/tests/unit/api_test.py +++ b/tests/unit/api_test.py @@ -420,3 +420,33 @@ class StreamTest(base.Cleanup, base.BaseTestCase): self.assertEqual(list(stream), [ str(i).encode() for i in range(50)]) + + +class UserAgentTest(base.BaseTestCase): + def setUp(self): + self.patcher = mock.patch.object( + docker.Client, + 'send', + return_value=fake_resp("GET", "%s/version" % fake_api.prefix) + ) + self.mock_send = self.patcher.start() + + def tearDown(self): + self.patcher.stop() + + def test_default_user_agent(self): + client = docker.Client() + client.version() + + self.assertEqual(self.mock_send.call_count, 1) + headers = self.mock_send.call_args[0][0].headers + expected = 'docker-py/%s' % docker.__version__ + self.assertEqual(headers['User-Agent'], expected) + + def test_custom_user_agent(self): + client = docker.Client(user_agent='foo/bar') + client.version() + + self.assertEqual(self.mock_send.call_count, 1) + headers = self.mock_send.call_args[0][0].headers + self.assertEqual(headers['User-Agent'], 'foo/bar') |