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
24import pkg_resources
25
26from google.api_core import version as api_core_version
27
28_PY_VERSION = platform.python_version()
29_API_CORE_VERSION = api_core_version.__version__
30
31_GRPC_VERSION: Union[str, None]
32
33try:
34    _GRPC_VERSION = pkg_resources.get_distribution("grpcio").version
35except pkg_resources.DistributionNotFound:  # 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 sversion 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]): The requests library version.
61    """
62
63    def __init__(
64        self,
65        python_version=_PY_VERSION,
66        grpc_version=_GRPC_VERSION,
67        api_core_version=_API_CORE_VERSION,
68        gapic_version=None,
69        client_library_version=None,
70        user_agent=None,
71        rest_version=None,
72    ):
73        self.python_version = python_version
74        self.grpc_version = grpc_version
75        self.api_core_version = api_core_version
76        self.gapic_version = gapic_version
77        self.client_library_version = client_library_version
78        self.user_agent = user_agent
79        self.rest_version = rest_version
80
81    def to_user_agent(self):
82        """Returns the user-agent string for this client info."""
83
84        # Note: the order here is important as the internal metrics system
85        # expects these items to be in specific locations.
86        ua = ""
87
88        if self.user_agent is not None:
89            ua += "{user_agent} "
90
91        ua += "gl-python/{python_version} "
92
93        if self.grpc_version is not None:
94            ua += "grpc/{grpc_version} "
95
96        if self.rest_version is not None:
97            ua += "rest/{rest_version} "
98
99        ua += "gax/{api_core_version} "
100
101        if self.gapic_version is not None:
102            ua += "gapic/{gapic_version} "
103
104        if self.client_library_version is not None:
105            ua += "gccl/{client_library_version} "
106
107        return ua.format(**self.__dict__).strip()
108