-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Core, BigQuery: refactor 'client_info' support. #7849
Changes from 13 commits
73e497e
dffc79b
13aa677
cfed70b
7803503
ce60f3a
551a76b
e6dc855
0276af1
dfa12fe
8597741
08b316f
5a7da2e
443a822
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Copyright 2017 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Helpers for providing client information. | ||
|
||
Client information is used to send information about the calling client, | ||
such as the library and Python version, to API services. | ||
""" | ||
|
||
import platform | ||
|
||
import pkg_resources | ||
|
||
_PY_VERSION = platform.python_version() | ||
_API_CORE_VERSION = pkg_resources.get_distribution("google-api-core").version | ||
|
||
try: | ||
_GRPC_VERSION = pkg_resources.get_distribution("grpcio").version | ||
except pkg_resources.DistributionNotFound: # pragma: NO COVER | ||
_GRPC_VERSION = None | ||
|
||
|
||
class ClientInfo(object): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tswast In order to support the API libraries which do not install grpc, I had to split out a base There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Good catch. |
||
"""Client information used to generate a user-agent for API calls. | ||
|
||
This user-agent information is sent along with API calls to allow the | ||
receiving service to do analytics on which versions of Python and Google | ||
libraries are being used. | ||
|
||
Args: | ||
python_version (str): The Python interpreter version, for example, | ||
``'2.7.13'``. | ||
grpc_version (Optional[str]): The gRPC library version. | ||
api_core_version (str): The google-api-core library version. | ||
gapic_version (Optional[str]): The sversion of gapic-generated client | ||
library, if the library was generated by gapic. | ||
client_library_version (Optional[str]): The version of the client | ||
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__( | ||
self, | ||
python_version=_PY_VERSION, | ||
grpc_version=_GRPC_VERSION, | ||
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 = "" | ||
|
||
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} " | ||
|
||
ua += "gax/{api_core_version} " | ||
|
||
if self.gapic_version is not None: | ||
ua += "gapic/{gapic_version} " | ||
|
||
if self.client_library_version is not None: | ||
ua += "gccl/{client_library_version} " | ||
|
||
return ua.format(**self.__dict__).strip() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2017 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from google.api_core import client_info | ||
|
||
|
||
def test_constructor_defaults(): | ||
info = client_info.ClientInfo() | ||
|
||
assert info.python_version is not None | ||
assert info.grpc_version is not None | ||
assert info.api_core_version is not None | ||
assert info.gapic_version is None | ||
assert info.client_library_version is None | ||
|
||
|
||
def test_constructor_options(): | ||
info = client_info.ClientInfo( | ||
python_version="1", | ||
grpc_version="2", | ||
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(): | ||
info = client_info.ClientInfo( | ||
python_version="1", api_core_version="2", grpc_version=None | ||
) | ||
|
||
user_agent = info.to_user_agent() | ||
|
||
assert user_agent == "gl-python/1 gax/2" | ||
|
||
|
||
def test_to_user_agent_full(): | ||
info = client_info.ClientInfo( | ||
python_version="1", | ||
grpc_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 grpc/2 gax/3 gapic/4 gccl/5" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a unit test for this? It'd be good to be doubly sure that it doesn't blow up when
_GRPC_VERSION
isNone
.