Add user agent to API calls

Signed-off-by: Ben Firshman <ben@firshman.co.uk>
This commit is contained in:
Ben Firshman 2016-07-24 18:57:36 +01:00
parent 5f0186e008
commit 1877a41b92
No known key found for this signature in database
GPG Key ID: 18296449E36D2F1E
3 changed files with 30 additions and 0 deletions

View File

@ -10,6 +10,7 @@ from docker.utils import kwargs_from_env
from ..const import HTTP_TIMEOUT from ..const import HTTP_TIMEOUT
from .errors import UserError from .errors import UserError
from .utils import generate_user_agent
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -67,4 +68,6 @@ def docker_client(environment, version=None, tls_config=None, host=None,
else: else:
kwargs['timeout'] = HTTP_TIMEOUT kwargs['timeout'] = HTTP_TIMEOUT
kwargs['user_agent'] = generate_user_agent()
return Client(**kwargs) return Client(**kwargs)

View File

@ -107,3 +107,18 @@ def get_build_version():
def is_docker_for_mac_installed(): def is_docker_for_mac_installed():
return is_mac() and os.path.isdir('/Applications/Docker.app') return is_mac() and os.path.isdir('/Applications/Docker.app')
def generate_user_agent():
parts = [
"docker-compose/{}".format(compose.__version__),
"docker-py/{}".format(docker.__version__),
]
try:
p_system = platform.system()
p_release = platform.release()
except IOError:
pass
else:
parts.append("{}/{}".format(p_system, p_release))
return " ".join(parts)

View File

@ -2,10 +2,12 @@ from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import os import os
import platform
import docker import docker
import pytest import pytest
import compose
from compose.cli import errors from compose.cli import errors
from compose.cli.docker_client import docker_client from compose.cli.docker_client import docker_client
from compose.cli.docker_client import tls_config_from_options from compose.cli.docker_client import tls_config_from_options
@ -40,6 +42,16 @@ class DockerClientTestCase(unittest.TestCase):
assert fake_log.error.call_count == 1 assert fake_log.error.call_count == 1
assert '123' in fake_log.error.call_args[0][0] assert '123' in fake_log.error.call_args[0][0]
def test_user_agent(self):
client = docker_client(os.environ)
expected = "docker-compose/{0} docker-py/{1} {2}/{3}".format(
compose.__version__,
docker.__version__,
platform.system(),
platform.release()
)
self.assertEqual(client.headers['User-Agent'], expected)
class TLSConfigTestCase(unittest.TestCase): class TLSConfigTestCase(unittest.TestCase):
ca_cert = 'tests/fixtures/tls/ca.pem' ca_cert = 'tests/fixtures/tls/ca.pem'