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 
    17Client information is used to send information about the calling client, 
    18such as the library and Python version, to API services. 
    19""" 
    20 
    21import platform 
    22from typing import Union 
    23 
    24from google.api_core import version as api_core_version 
    25 
    26_PY_VERSION = platform.python_version() 
    27_API_CORE_VERSION = api_core_version.__version__ 
    28 
    29_GRPC_VERSION: Union[str, None] 
    30 
    31try: 
    32    import grpc 
    33 
    34    _GRPC_VERSION = grpc.__version__ 
    35except ImportError:  # pragma: NO COVER 
    36    _GRPC_VERSION = None 
    37 
    38 
    39class ClientInfo(object): 
    40    """Client information used to generate a user-agent for API calls. 
    41 
    42    This user-agent information is sent along with API calls to allow the 
    43    receiving service to do analytics on which versions of Python and Google 
    44    libraries are being used. 
    45 
    46    Args: 
    47        python_version (str): The Python interpreter version, for example, 
    48            ``'3.9.6'``. 
    49        grpc_version (Optional[str]): The gRPC library version. 
    50        api_core_version (str): The google-api-core library version. 
    51        gapic_version (Optional[str]): The version of gapic-generated client 
    52            library, if the library was generated by gapic. 
    53        client_library_version (Optional[str]): The version of the client 
    54            library, generally used if the client library was not generated 
    55            by gapic or if additional functionality was built on top of 
    56            a gapic client library. 
    57        user_agent (Optional[str]): Prefix to the user agent header. This is 
    58            used to supply information such as application name or partner tool. 
    59            Recommended format: ``application-or-tool-ID/major.minor.version``. 
    60        rest_version (Optional[str]): A string with labeled versions of the 
    61            dependencies used for REST transport. 
    62        protobuf_runtime_version (Optional[str]): The protobuf runtime version. 
    63    """ 
    64 
    65    def __init__( 
    66        self, 
    67        python_version=_PY_VERSION, 
    68        grpc_version=_GRPC_VERSION, 
    69        api_core_version=_API_CORE_VERSION, 
    70        gapic_version=None, 
    71        client_library_version=None, 
    72        user_agent=None, 
    73        rest_version=None, 
    74        protobuf_runtime_version=None, 
    75    ): 
    76        self.python_version = python_version 
    77        self.grpc_version = grpc_version 
    78        self.api_core_version = api_core_version 
    79        self.gapic_version = gapic_version 
    80        self.client_library_version = client_library_version 
    81        self.user_agent = user_agent 
    82        self.rest_version = rest_version 
    83        self.protobuf_runtime_version = protobuf_runtime_version 
    84 
    85    def to_user_agent(self): 
    86        """Returns the user-agent string for this client info.""" 
    87 
    88        # Note: the order here is important as the internal metrics system 
    89        # expects these items to be in specific locations. 
    90        ua = "" 
    91 
    92        if self.user_agent is not None: 
    93            ua += "{user_agent} " 
    94 
    95        ua += "gl-python/{python_version} " 
    96 
    97        if self.grpc_version is not None: 
    98            ua += "grpc/{grpc_version} " 
    99 
    100        if self.rest_version is not None: 
    101            ua += "rest/{rest_version} " 
    102 
    103        ua += "gax/{api_core_version} " 
    104 
    105        if self.gapic_version is not None: 
    106            ua += "gapic/{gapic_version} " 
    107 
    108        if self.client_library_version is not None: 
    109            ua += "gccl/{client_library_version} " 
    110 
    111        if self.protobuf_runtime_version is not None: 
    112            ua += "pb/{protobuf_runtime_version} " 
    113 
    114        return ua.format(**self.__dict__).strip()