Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/api_core/client_info.py: 59%
34 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:30 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:30 +0000
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.
15"""Helpers for providing client information.
17Client information is used to send information about the calling client,
18such as the library and Python version, to API services.
19"""
21import platform
22from typing import Union
24from google.api_core import version as api_core_version
26_PY_VERSION = platform.python_version()
27_API_CORE_VERSION = api_core_version.__version__
29_GRPC_VERSION: Union[str, None]
31try:
32 import grpc
34 _GRPC_VERSION = grpc.__version__
35except ImportError: # pragma: NO COVER
36 _GRPC_VERSION = None
39class ClientInfo(object):
40 """Client information used to generate a user-agent for API calls.
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.
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 """
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
81 def to_user_agent(self):
82 """Returns the user-agent string for this client info."""
84 # Note: the order here is important as the internal metrics system
85 # expects these items to be in specific locations.
86 ua = ""
88 if self.user_agent is not None:
89 ua += "{user_agent} "
91 ua += "gl-python/{python_version} "
93 if self.grpc_version is not None:
94 ua += "grpc/{grpc_version} "
96 if self.rest_version is not None:
97 ua += "rest/{rest_version} "
99 ua += "gax/{api_core_version} "
101 if self.gapic_version is not None:
102 ua += "gapic/{gapic_version} "
104 if self.client_library_version is not None:
105 ua += "gccl/{client_library_version} "
107 return ua.format(**self.__dict__).strip()