2323import pkg_resources
2424
2525_PY_VERSION = platform .python_version ()
26- _GRPC_VERSION = pkg_resources .get_distribution ('grpcio' ).version
2726_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+
2833METRICS_METADATA_KEY = 'x-goog-api-client'
2934
3035
@@ -38,7 +43,7 @@ class ClientInfo(object):
3843 Args:
3944 python_version (str): The Python interpreter version, for example,
4045 ``'2.7.13'``.
41- grpc_version (str): The gRPC library version.
46+ grpc_version (Optional[ str] ): The gRPC library version.
4247 api_core_version (str): The google-api-core library version.
4348 gapic_version (Optional[str]): The sversion of gapic-generated client
4449 library, if the library was generated by gapic.
@@ -66,15 +71,18 @@ def to_user_agent(self):
6671 # expects these items to be in specific locations.
6772 ua = 'gl-python/{python_version} '
6873
69- if self .client_library_version is not None :
70- ua += 'gccl/{client_library_version} '
74+ if self .grpc_version is not None :
75+ ua += 'grpc/{grpc_version} '
76+
77+ ua += 'gax/{api_core_version} '
7178
7279 if self .gapic_version is not None :
7380 ua += 'gapic/{gapic_version} '
7481
75- ua += 'gax/{api_core_version} grpc/{grpc_version}'
82+ if self .client_library_version is not None :
83+ ua += 'gccl/{client_library_version} '
7684
77- return ua .format (** self .__dict__ )
85+ return ua .format (** self .__dict__ ). strip ()
7886
7987 def to_grpc_metadata (self ):
8088 """Returns the gRPC metadata for this client info."""
0 commit comments