From 57cf3a1f27292939ed097ef8afa3f4392c4b83e0 Mon Sep 17 00:00:00 2001 From: Denis DelGrosso <85250797+ddelgrosso1@users.noreply.github.com> Date: Thu, 2 Sep 2021 13:34:47 -0400 Subject: [PATCH] fix: do not append duplicates to user agent string (#570) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-storage/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #565 🦕 --- google/cloud/storage/_http.py | 4 +++- tests/unit/test__http.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/google/cloud/storage/_http.py b/google/cloud/storage/_http.py index 0dcc68cdb..6c9d11700 100644 --- a/google/cloud/storage/_http.py +++ b/google/cloud/storage/_http.py @@ -56,7 +56,9 @@ def __init__(self, client, client_info=None, api_endpoint=None): # TODO: When metrics all use gccl, this should be removed #9552 if self._client_info.user_agent is None: # pragma: no branch self._client_info.user_agent = "" - self._client_info.user_agent += " gcloud-python/{} ".format(__version__) + agent_version = "gcloud-python/{}".format(__version__) + if agent_version not in self._client_info.user_agent: + self._client_info.user_agent += " {} ".format(agent_version) API_VERSION = "v1" """The version of the API, used in building the API call's URL.""" diff --git a/tests/unit/test__http.py b/tests/unit/test__http.py index ac8471d07..53f785885 100644 --- a/tests/unit/test__http.py +++ b/tests/unit/test__http.py @@ -228,3 +228,18 @@ def test_mtls(self): self.assertEqual(conn.ALLOW_AUTO_SWITCH_TO_MTLS_URL, False) self.assertEqual(conn.API_BASE_URL, "http://foo") self.assertEqual(conn.API_BASE_MTLS_URL, "https://storage.mtls.googleapis.com") + + def test_duplicate_user_agent(self): + # Regression test for issue #565 + from google.cloud._http import ClientInfo + from google.cloud.storage.client import Client + from google.cloud.storage.batch import Batch + from google.cloud.storage import __version__ + + client_info = ClientInfo(user_agent="test/123") + client = Client(project="test-project", client_info=client_info) + conn = self._make_one(client, client_info) + expected_user_agent = "test/123 gcloud-python/{} ".format(__version__) + self.assertEqual(conn._client_info.user_agent, expected_user_agent) + batch = Batch(client) + self.assertEqual(batch._client_info.user_agent, expected_user_agent)