Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for rest/ token in x-goog-api-client header #189

Merged
merged 2 commits into from May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions google/api_core/client_info.py
Expand Up @@ -54,6 +54,7 @@ class ClientInfo(object):
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``.
rest_version (Optional[str]): The requests library version.
"""

def __init__(
Expand All @@ -64,13 +65,15 @@ def __init__(
gapic_version=None,
client_library_version=None,
user_agent=None,
rest_version=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
self.rest_version = rest_version
Comment on lines 70 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be a check that either rest_version XOR grpc_version is set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable to me, I was thinking about the same thing, but then decided that it would be safer to not add something like tha to a GA'ed library. If you feel strong about it, I can add it. Should I?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly about it, but it seems like a reasonable thing for someone looking at the fields to expect.


def to_user_agent(self):
"""Returns the user-agent string for this client info."""
Expand All @@ -87,6 +90,9 @@ def to_user_agent(self):
if self.grpc_version is not None:
ua += "grpc/{grpc_version} "

if self.rest_version is not None:
ua += "rest/{rest_version} "

ua += "gax/{api_core_version} "

if self.gapic_version is not None:
Expand Down
21 changes: 20 additions & 1 deletion tests/unit/test_client_info.py
Expand Up @@ -24,6 +24,7 @@ def test_constructor_defaults():
assert info.api_core_version is not None
assert info.gapic_version is None
assert info.client_library_version is None
assert info.rest_version is None


def test_constructor_options():
Expand All @@ -33,7 +34,8 @@ def test_constructor_options():
api_core_version="3",
gapic_version="4",
client_library_version="5",
user_agent="6"
user_agent="6",
rest_version="7",
)

assert info.python_version == "1"
Expand All @@ -42,6 +44,7 @@ def test_constructor_options():
assert info.gapic_version == "4"
assert info.client_library_version == "5"
assert info.user_agent == "6"
assert info.rest_version == "7"


def test_to_user_agent_minimal():
Expand All @@ -67,3 +70,19 @@ def test_to_user_agent_full():
user_agent = info.to_user_agent()

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


def test_to_user_agent_rest():
info = client_info.ClientInfo(
python_version="1",
grpc_version=None,
rest_version="2",
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 == "app-name/1.0 gl-python/1 rest/2 gax/3 gapic/4 gccl/5"