mirror of https://github.com/docker/docker-py.git
Set custom user agent on client
Signed-off-by: Ben Firshman <ben@firshman.co.uk>
This commit is contained in:
parent
e15ba7400a
commit
bd73225e14
|
@ -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()
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
||||
****
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue