1313# limitations under the License.
1414"""A client and common configurations for the Google Ads API."""
1515
16- from grpc import intercept_channel
16+ import grpc
1717from importlib import import_module
1818import logging .config
1919
20- from google .ads .google_ads import config
21- from google .ads .google_ads import oauth2
20+ from google .ads .google_ads import config , oauth2 , util
2221from google .ads .google_ads .interceptors import MetadataInterceptor , \
2322 ExceptionInterceptor , LoggingInterceptor
2423
3534 ('grpc.max_metadata_size' , 16 * 1024 * 1024 ),
3635 ('grpc.max_receive_message_length' , 64 * 1024 * 1024 )]
3736
37+
38+ unary_stream_single_threading_option = util .get_nested_attr (
39+ grpc , 'experimental.ChannelOptions.SingleThreadedUnaryStream' , None )
40+
41+ if unary_stream_single_threading_option :
42+ _GRPC_CHANNEL_OPTIONS .append (
43+ (unary_stream_single_threading_option , 1 ))
44+
45+
3846class GoogleAdsClient (object ):
3947 """Google Ads client used to configure settings and fetch services."""
4048
@@ -69,7 +77,7 @@ def _get_api_services_by_version(cls, version):
6977 A module containing all services and types for the a API version.
7078 """
7179 try :
72- version_module = import_module ('google.ads.google_ads.%s' % version )
80+ version_module = import_module (f 'google.ads.google_ads.{ version } ' )
7381 except ImportError :
7482 raise ValueError ('Specified Google Ads API version "{}" does not '
7583 'exist. Valid API versions are: "{}"' .format (
@@ -167,12 +175,16 @@ def get_type(cls, name, version=_DEFAULT_VERSION):
167175 AttributeError: If the type for the specified name doesn't exist
168176 in the given version.
169177 """
178+ if name .lower ().endswith ('pb2' ):
179+ raise ValueError (f'Specified type "{ name } " must be a class,'
180+ f' not a module' )
181+
170182 try :
171183 type_classes = cls ._get_api_services_by_version (version ).types
172184 message_class = getattr (type_classes , name )
173185 except AttributeError :
174- raise ValueError ('Specified type "{}" does not exist in Google Ads '
175- 'API %s.' . format ( name , version ) )
186+ raise ValueError (f 'Specified type "{ name } " does not exist in Google Ads '
187+ f 'API { version } ' )
176188 return message_class ()
177189
178190 def __init__ (self , credentials , developer_token , endpoint = None ,
@@ -242,10 +254,10 @@ def get_service(self, name, version=_DEFAULT_VERSION, interceptors=[]):
242254 LoggingInterceptor (_logger , endpoint ),
243255 ExceptionInterceptor (version )]
244256
245- channel = intercept_channel (
257+ channel = grpc . intercept_channel (
246258 channel ,
247259 * interceptors )
248260
249261 service_transport = service_transport_class (channel = channel )
250262
251- return service_client (transport = service_transport )
263+ return service_client (transport = service_transport )
0 commit comments