Skip to content

Commit

Permalink
Add user_agent property to ClientInfo (#7799)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
tswast authored Apr 29, 2019
1 parent ed3ee90 commit a2c1f6c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
13 changes: 12 additions & 1 deletion api_core/google/api_core/gapic_v1/client_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class ClientInfo(object):
library, generally used if the client library was not generated
by gapic or if additional functionality was built on top of
a gapic client library.
user_agent (Optional[str]): Prefix to the user agent header. This is
used to supply information such as application name or partner tool.
Recommended format: ``application-or-tool-ID/major.minor.version``.
"""

def __init__(
Expand All @@ -60,18 +63,26 @@ def __init__(
api_core_version=_API_CORE_VERSION,
gapic_version=None,
client_library_version=None,
user_agent=None,
):
self.python_version = python_version
self.grpc_version = grpc_version
self.api_core_version = api_core_version
self.gapic_version = gapic_version
self.client_library_version = client_library_version
self.user_agent = user_agent

def to_user_agent(self):
"""Returns the user-agent string for this client info."""

# Note: the order here is important as the internal metrics system
# expects these items to be in specific locations.
ua = "gl-python/{python_version} "
ua = ""

if self.user_agent is not None:
ua += "{user_agent} "

ua += "gl-python/{python_version} "

if self.grpc_version is not None:
ua += "grpc/{grpc_version} "
Expand Down
2 changes: 2 additions & 0 deletions api_core/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ inputs =
exclude =
tests/
output = pytype_output/
# Workaround for https://github.com/google/pytype/issues/150
disable = pyi-error
5 changes: 4 additions & 1 deletion api_core/tests/unit/gapic/test_client_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ def test_constructor_options():
api_core_version="3",
gapic_version="4",
client_library_version="5",
user_agent="6"
)

assert info.python_version == "1"
assert info.grpc_version == "2"
assert info.api_core_version == "3"
assert info.gapic_version == "4"
assert info.client_library_version == "5"
assert info.user_agent == "6"


def test_to_user_agent_minimal():
Expand All @@ -59,11 +61,12 @@ def test_to_user_agent_full():
api_core_version="3",
gapic_version="4",
client_library_version="5",
user_agent="app-name/1.0",
)

user_agent = info.to_user_agent()

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


def test_to_grpc_metadata():
Expand Down

0 comments on commit a2c1f6c

Please sign in to comment.