Skip to content

Commit 0e2f6a3

Browse files
authored
Add user_agent property to ClientInfo (#7799)
* Add `user_agent` property to `ClientInfo` This provides a way for partners to define a prefix identifying their tool or application, as required by many cloud partnership agreements. * Workaround for pytype pyi error with nested class.
1 parent ca8822b commit 0e2f6a3

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

google/api_core/gapic_v1/client_info.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class ClientInfo(object):
5151
library, generally used if the client library was not generated
5252
by gapic or if additional functionality was built on top of
5353
a gapic client library.
54+
user_agent (Optional[str]): Prefix to the user agent header. This is
55+
used to supply information such as application name or partner tool.
56+
Recommended format: ``application-or-tool-ID/major.minor.version``.
5457
"""
5558

5659
def __init__(
@@ -60,18 +63,26 @@ def __init__(
6063
api_core_version=_API_CORE_VERSION,
6164
gapic_version=None,
6265
client_library_version=None,
66+
user_agent=None,
6367
):
6468
self.python_version = python_version
6569
self.grpc_version = grpc_version
6670
self.api_core_version = api_core_version
6771
self.gapic_version = gapic_version
6872
self.client_library_version = client_library_version
73+
self.user_agent = user_agent
6974

7075
def to_user_agent(self):
7176
"""Returns the user-agent string for this client info."""
77+
7278
# Note: the order here is important as the internal metrics system
7379
# expects these items to be in specific locations.
74-
ua = "gl-python/{python_version} "
80+
ua = ""
81+
82+
if self.user_agent is not None:
83+
ua += "{user_agent} "
84+
85+
ua += "gl-python/{python_version} "
7586

7687
if self.grpc_version is not None:
7788
ua += "grpc/{grpc_version} "

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ inputs =
88
exclude =
99
tests/
1010
output = pytype_output/
11+
# Workaround for https://github.com/google/pytype/issues/150
12+
disable = pyi-error

tests/unit/gapic/test_client_info.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ def test_constructor_options():
3333
api_core_version="3",
3434
gapic_version="4",
3535
client_library_version="5",
36+
user_agent="6"
3637
)
3738

3839
assert info.python_version == "1"
3940
assert info.grpc_version == "2"
4041
assert info.api_core_version == "3"
4142
assert info.gapic_version == "4"
4243
assert info.client_library_version == "5"
44+
assert info.user_agent == "6"
4345

4446

4547
def test_to_user_agent_minimal():
@@ -59,11 +61,12 @@ def test_to_user_agent_full():
5961
api_core_version="3",
6062
gapic_version="4",
6163
client_library_version="5",
64+
user_agent="app-name/1.0",
6265
)
6366

6467
user_agent = info.to_user_agent()
6568

66-
assert user_agent == "gl-python/1 grpc/2 gax/3 gapic/4 gccl/5"
69+
assert user_agent == "app-name/1.0 gl-python/1 grpc/2 gax/3 gapic/4 gccl/5"
6770

6871

6972
def test_to_grpc_metadata():

0 commit comments

Comments
 (0)