Skip to content

Commit

Permalink
fix: do not append duplicates to user agent string (#570)
Browse files Browse the repository at this point in the history
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 🦕
  • Loading branch information
ddelgrosso1 committed Sep 2, 2021
1 parent d0f45e9 commit 57cf3a1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion google/cloud/storage/_http.py
Expand Up @@ -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."""
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test__http.py
Expand Up @@ -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)

0 comments on commit 57cf3a1

Please sign in to comment.