Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
client_info.py108 linesDownload Raw Back to api_core
1# Copyright 2017 Google LLC2#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 at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# 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 platform22from typing import Union23 24from google.api_core import version as api_core_version25 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 grpc33 34    _GRPC_VERSION = grpc.__version__35except ImportError:  # pragma: NO COVER36    _GRPC_VERSION = None37 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 the43    receiving service to do analytics on which versions of Python and Google44    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 client52            library, if the library was generated by gapic.53        client_library_version (Optional[str]): The version of the client54            library, generally used if the client library was not generated55            by gapic or if additional functionality was built on top of56            a gapic client library.57        user_agent (Optional[str]): Prefix to the user agent header. This is58            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_version74        self.grpc_version = grpc_version75        self.api_core_version = api_core_version76        self.gapic_version = gapic_version77        self.client_library_version = client_library_version78        self.user_agent = user_agent79        self.rest_version = rest_version80 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 system85        # 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 
codekingpro/portable-devtools · Team Ai