|
| 1 | +# Copyright 2017 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Helpers for providing client information. |
| 16 | +
|
| 17 | +Client information is used to send information about the calling client, |
| 18 | +such as the library and Python version, to API services. |
| 19 | +""" |
| 20 | + |
| 21 | +import platform |
| 22 | + |
| 23 | +import pkg_resources |
| 24 | + |
| 25 | +_PY_VERSION = platform.python_version() |
| 26 | +_API_CORE_VERSION = pkg_resources.get_distribution("google-api-core").version |
| 27 | + |
| 28 | +try: |
| 29 | + _GRPC_VERSION = pkg_resources.get_distribution("grpcio").version |
| 30 | +except pkg_resources.DistributionNotFound: # pragma: NO COVER |
| 31 | + _GRPC_VERSION = None |
| 32 | + |
| 33 | + |
| 34 | +class ClientInfo(object): |
| 35 | + """Client information used to generate a user-agent for API calls. |
| 36 | +
|
| 37 | + This user-agent information is sent along with API calls to allow the |
| 38 | + receiving service to do analytics on which versions of Python and Google |
| 39 | + libraries are being used. |
| 40 | +
|
| 41 | + Args: |
| 42 | + python_version (str): The Python interpreter version, for example, |
| 43 | + ``'2.7.13'``. |
| 44 | + grpc_version (Optional[str]): The gRPC library version. |
| 45 | + api_core_version (str): The google-api-core library version. |
| 46 | + gapic_version (Optional[str]): The sversion of gapic-generated client |
| 47 | + library, if the library was generated by gapic. |
| 48 | + client_library_version (Optional[str]): The version of the client |
| 49 | + library, generally used if the client library was not generated |
| 50 | + by gapic or if additional functionality was built on top of |
| 51 | + a gapic client library. |
| 52 | + user_agent (Optional[str]): Prefix to the user agent header. This is |
| 53 | + used to supply information such as application name or partner tool. |
| 54 | + Recommended format: ``application-or-tool-ID/major.minor.version``. |
| 55 | + """ |
| 56 | + |
| 57 | + def __init__( |
| 58 | + self, |
| 59 | + python_version=_PY_VERSION, |
| 60 | + grpc_version=_GRPC_VERSION, |
| 61 | + api_core_version=_API_CORE_VERSION, |
| 62 | + gapic_version=None, |
| 63 | + client_library_version=None, |
| 64 | + user_agent=None, |
| 65 | + ): |
| 66 | + self.python_version = python_version |
| 67 | + self.grpc_version = grpc_version |
| 68 | + self.api_core_version = api_core_version |
| 69 | + self.gapic_version = gapic_version |
| 70 | + self.client_library_version = client_library_version |
| 71 | + self.user_agent = user_agent |
| 72 | + |
| 73 | + def to_user_agent(self): |
| 74 | + """Returns the user-agent string for this client info.""" |
| 75 | + |
| 76 | + # Note: the order here is important as the internal metrics system |
| 77 | + # expects these items to be in specific locations. |
| 78 | + ua = "" |
| 79 | + |
| 80 | + if self.user_agent is not None: |
| 81 | + ua += "{user_agent} " |
| 82 | + |
| 83 | + ua += "gl-python/{python_version} " |
| 84 | + |
| 85 | + if self.grpc_version is not None: |
| 86 | + ua += "grpc/{grpc_version} " |
| 87 | + |
| 88 | + ua += "gax/{api_core_version} " |
| 89 | + |
| 90 | + if self.gapic_version is not None: |
| 91 | + ua += "gapic/{gapic_version} " |
| 92 | + |
| 93 | + if self.client_library_version is not None: |
| 94 | + ua += "gccl/{client_library_version} " |
| 95 | + |
| 96 | + return ua.format(**self.__dict__).strip() |
0 commit comments