Skip to content

Commit ae86949

Browse files
bfirshshin-
authored andcommitted
Set custom user agent on client
Signed-off-by: Ben Firshman <[email protected]>
1 parent 0f15c65 commit ae86949

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

docker/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class Client(
4949
api.VolumeApiMixin,
5050
api.NetworkApiMixin):
5151
def __init__(self, base_url=None, version=None,
52-
timeout=constants.DEFAULT_TIMEOUT_SECONDS, tls=False):
52+
timeout=constants.DEFAULT_TIMEOUT_SECONDS, tls=False,
53+
user_agent=constants.DEFAULT_USER_AGENT):
5354
super(Client, self).__init__()
5455

5556
if tls and not base_url:
@@ -59,6 +60,7 @@ def __init__(self, base_url=None, version=None,
5960

6061
self.base_url = base_url
6162
self.timeout = timeout
63+
self.headers['User-Agent'] = user_agent
6264

6365
self._auth_configs = auth.load_config()
6466

docker/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from .version import version
23

34
DEFAULT_DOCKER_API_VERSION = '1.22'
45
DEFAULT_TIMEOUT_SECONDS = 60
@@ -12,3 +13,5 @@
1213
'is deprecated and non-functional. Please remove it.'
1314

1415
IS_WINDOWS_PLATFORM = (sys.platform == 'win32')
16+
17+
DEFAULT_USER_AGENT = "docker-py/{0}".format(version)

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ is hosted.
1616
to use the API version provided by the server.
1717
* timeout (int): The HTTP request timeout, in seconds.
1818
* tls (bool or [TLSConfig](tls.md#TLSConfig)): Equivalent CLI options: `docker --tls ...`
19+
* user_agent (str): Set a custom user agent for requests to the server.
1920

2021
****
2122

tests/unit/api_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,33 @@ def test_early_stream_response(self):
415415

416416
self.assertEqual(list(stream), [
417417
str(i).encode() for i in range(50)])
418+
419+
420+
class UserAgentTest(base.BaseTestCase):
421+
def setUp(self):
422+
self.patcher = mock.patch.object(
423+
docker.Client,
424+
'send',
425+
return_value=fake_resp("GET", "%s/version" % fake_api.prefix)
426+
)
427+
self.mock_send = self.patcher.start()
428+
429+
def tearDown(self):
430+
self.patcher.stop()
431+
432+
def test_default_user_agent(self):
433+
client = docker.Client()
434+
client.version()
435+
436+
self.assertEqual(self.mock_send.call_count, 1)
437+
headers = self.mock_send.call_args[0][0].headers
438+
expected = 'docker-py/%s' % docker.__version__
439+
self.assertEqual(headers['User-Agent'], expected)
440+
441+
def test_custom_user_agent(self):
442+
client = docker.Client(user_agent='foo/bar')
443+
client.version()
444+
445+
self.assertEqual(self.mock_send.call_count, 1)
446+
headers = self.mock_send.call_args[0][0].headers
447+
self.assertEqual(headers['User-Agent'], 'foo/bar')

0 commit comments

Comments
 (0)