From 477de5d3ede3f2148b328722226f6307837daeb1 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 7 May 2019 11:38:31 -0400 Subject: [PATCH 1/4] Add client_info support to client / connection. Toward #7258. --- .../google/cloud/runtimeconfig/_http.py | 14 ++++--- .../google/cloud/runtimeconfig/client.py | 11 +++++- runtimeconfig/noxfile.py | 6 ++- runtimeconfig/tests/unit/test__http.py | 4 +- runtimeconfig/tests/unit/test_client.py | 39 +++++++++++++++++-- 5 files changed, 60 insertions(+), 14 deletions(-) diff --git a/runtimeconfig/google/cloud/runtimeconfig/_http.py b/runtimeconfig/google/cloud/runtimeconfig/_http.py index fdc559454f70..146a69f3051c 100644 --- a/runtimeconfig/google/cloud/runtimeconfig/_http.py +++ b/runtimeconfig/google/cloud/runtimeconfig/_http.py @@ -21,16 +21,22 @@ from google.cloud.runtimeconfig import __version__ -_CLIENT_INFO = _http.CLIENT_INFO_TEMPLATE.format(__version__) - - class Connection(_http.JSONConnection): """A connection to Google Cloud RuntimeConfig via the JSON REST API. :type client: :class:`~google.cloud.runtimeconfig.client.Client` :param client: The client that owns the current connection. + + :type client_info: :class:`~google.api_core.client_info.ClientInfo` + :param client_info: (Optional) instance used to generate user agent. """ + def __init__(self, client, client_info=None): + super(Connection, self).__init__(client, client_info) + + self._client_info.gapic_version = __version__ + self._client_info.client_library_version = __version__ + API_BASE_URL = "https://runtimeconfig.googleapis.com" """The base of the API call URL.""" @@ -39,5 +45,3 @@ class Connection(_http.JSONConnection): API_URL_TEMPLATE = "{api_base_url}/{api_version}{path}" """A template for the URL of a particular API call.""" - - _EXTRA_HEADERS = {_http.CLIENT_INFO_HEADER: _CLIENT_INFO} diff --git a/runtimeconfig/google/cloud/runtimeconfig/client.py b/runtimeconfig/google/cloud/runtimeconfig/client.py index 81bf021aa9cc..85d6b5abcbfc 100644 --- a/runtimeconfig/google/cloud/runtimeconfig/client.py +++ b/runtimeconfig/google/cloud/runtimeconfig/client.py @@ -43,16 +43,23 @@ class Client(ClientWithProject): ``credentials`` for the current object. This parameter should be considered private, and could change in the future. + + :type client_info: :class:`~google.api_core.client_info.ClientInfo: + :param client_info: + The client info used to send a user-agent string along with API + requests. If ``None``, then default info will be used. Generally, + you only need to set this if you're developing your own library + or partner tool. """ SCOPE = ("https://www.googleapis.com/auth/cloudruntimeconfig",) """The scopes required for authenticating as a RuntimeConfig consumer.""" - def __init__(self, project=None, credentials=None, _http=None): + def __init__(self, project=None, credentials=None, _http=None, client_info=None): super(Client, self).__init__( project=project, credentials=credentials, _http=_http ) - self._connection = Connection(self) + self._connection = Connection(self, client_info=client_info) def config(self, config_name): """Factory constructor for config object. diff --git a/runtimeconfig/noxfile.py b/runtimeconfig/noxfile.py index 76b88851320c..54497056ebe7 100644 --- a/runtimeconfig/noxfile.py +++ b/runtimeconfig/noxfile.py @@ -67,8 +67,10 @@ def lint_setup_py(session): def default(session): """Default unit test session. """ - # Install all test dependencies, then install this package in-place. - session.install('mock', 'pytest', 'pytest-cov', *LOCAL_DEPS) + # Install all test dependencies, then install local packages in-place. + session.install('mock', 'pytest', 'pytest-cov') + for local_dep in LOCAL_DEPS: + session.install('-e', local_dep) session.install('-e', '.') # Run py.test against the unit tests. diff --git a/runtimeconfig/tests/unit/test__http.py b/runtimeconfig/tests/unit/test__http.py index c015a2944307..7ddc8bb2ffa0 100644 --- a/runtimeconfig/tests/unit/test__http.py +++ b/runtimeconfig/tests/unit/test__http.py @@ -53,8 +53,8 @@ def test_extra_headers(self): expected_headers = { "Accept-Encoding": "gzip", - base_http.CLIENT_INFO_HEADER: MUT._CLIENT_INFO, - "User-Agent": conn.USER_AGENT, + base_http.CLIENT_INFO_HEADER: conn.user_agent, + "User-Agent": conn.user_agent, } expected_uri = conn.build_api_url("/rainbow") http.request.assert_called_once_with( diff --git a/runtimeconfig/tests/unit/test_client.py b/runtimeconfig/tests/unit/test_client.py index 481f19b28fde..70e952cb8fc8 100644 --- a/runtimeconfig/tests/unit/test_client.py +++ b/runtimeconfig/tests/unit/test_client.py @@ -33,15 +33,48 @@ def _get_target_class(): def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) + def test_ctor_wo_client_info(self): + from google.cloud._http import ClientInfo + from google.cloud.runtimeconfig._http import Connection + + PROJECT = "PROJECT" + CONFIG_NAME = "config_name" + http = object() + creds = _make_credentials() + + client = self._make_one(project=PROJECT, credentials=creds, _http=http) + self.assertIsInstance(client._connection, Connection) + self.assertIs(client._credentials, creds) + self.assertIs(client._http_internal, http) + self.assertIsInstance(client._connection._client_info, ClientInfo) + + def test_ctor_w_client_info(self): + from google.cloud._http import ClientInfo + from google.cloud.runtimeconfig._http import Connection + + PROJECT = "PROJECT" + CONFIG_NAME = "config_name" + http = object() + creds = _make_credentials() + client_info = ClientInfo() + + client = self._make_one( + project=PROJECT, credentials=creds, _http=http, client_info=client_info + ) + self.assertIsInstance(client._connection, Connection) + self.assertIs(client._credentials, creds) + self.assertIs(client._http_internal, http) + self.assertIs(client._connection._client_info, client_info) + def test_config(self): PROJECT = "PROJECT" CONFIG_NAME = "config_name" creds = _make_credentials() - client_obj = self._make_one(project=PROJECT, credentials=creds) - new_config = client_obj.config(CONFIG_NAME) + client = self._make_one(project=PROJECT, credentials=creds) + new_config = client.config(CONFIG_NAME) self.assertEqual(new_config.name, CONFIG_NAME) - self.assertIs(new_config._client, client_obj) + self.assertIs(new_config._client, client) self.assertEqual(new_config.project, PROJECT) self.assertEqual( new_config.full_name, "projects/%s/configs/%s" % (PROJECT, CONFIG_NAME) From b7a92de8dbc9b3897901adc9f8c201dfcda39143 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 7 May 2019 12:31:02 -0400 Subject: [PATCH 2/4] Docstring typo. --- runtimeconfig/google/cloud/runtimeconfig/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtimeconfig/google/cloud/runtimeconfig/client.py b/runtimeconfig/google/cloud/runtimeconfig/client.py index 85d6b5abcbfc..26c291d9c4a9 100644 --- a/runtimeconfig/google/cloud/runtimeconfig/client.py +++ b/runtimeconfig/google/cloud/runtimeconfig/client.py @@ -44,7 +44,7 @@ class Client(ClientWithProject): This parameter should be considered private, and could change in the future. - :type client_info: :class:`~google.api_core.client_info.ClientInfo: + :type client_info: :class:`~google.api_core.client_info.ClientInfo` :param client_info: The client info used to send a user-agent string along with API requests. If ``None``, then default info will be used. Generally, From 54226ae31174d35d8bbd452b7cf3849189c122c7 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 7 May 2019 12:31:24 -0400 Subject: [PATCH 3/4] Lint. --- runtimeconfig/tests/unit/test__http.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/runtimeconfig/tests/unit/test__http.py b/runtimeconfig/tests/unit/test__http.py index 7ddc8bb2ffa0..82fda8463f85 100644 --- a/runtimeconfig/tests/unit/test__http.py +++ b/runtimeconfig/tests/unit/test__http.py @@ -34,9 +34,7 @@ def test_default_url(self): def test_extra_headers(self): import requests - from google.cloud import _http as base_http - from google.cloud.runtimeconfig import _http as MUT http = mock.create_autospec(requests.Session, instance=True) response = requests.Response() From 66afe37fad78ebd76a44b902c8028b5db31917b8 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 7 May 2019 12:47:06 -0400 Subject: [PATCH 4/4] Lint. --- runtimeconfig/tests/unit/test_client.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/runtimeconfig/tests/unit/test_client.py b/runtimeconfig/tests/unit/test_client.py index 70e952cb8fc8..f112d64092b4 100644 --- a/runtimeconfig/tests/unit/test_client.py +++ b/runtimeconfig/tests/unit/test_client.py @@ -38,7 +38,6 @@ def test_ctor_wo_client_info(self): from google.cloud.runtimeconfig._http import Connection PROJECT = "PROJECT" - CONFIG_NAME = "config_name" http = object() creds = _make_credentials() @@ -53,7 +52,6 @@ def test_ctor_w_client_info(self): from google.cloud.runtimeconfig._http import Connection PROJECT = "PROJECT" - CONFIG_NAME = "config_name" http = object() creds = _make_credentials() client_info = ClientInfo()