Skip to content

Commit 41440bc

Browse files
authored
Add interface to inject external interceptors into service requests (#174)
1 parent 337f4ea commit 41440bc

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

google/ads/google_ads/client.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""A client and common configurations for the Google Ads API."""
1515

16-
import grpc
16+
from grpc import intercept_channel
1717
from importlib import import_module
1818
import logging.config
1919

@@ -194,7 +194,7 @@ def __init__(self, credentials, developer_token, endpoint=None,
194194
self.endpoint = endpoint
195195
self.login_customer_id = login_customer_id
196196

197-
def get_service(self, name, version=_DEFAULT_VERSION):
197+
def get_service(self, name, version=_DEFAULT_VERSION, interceptors=[]):
198198
"""Returns a service client instance for the specified service_name.
199199
200200
Args:
@@ -203,6 +203,9 @@ def get_service(self, name, version=_DEFAULT_VERSION):
203203
"CampaignService" to retrieve a CampaignServiceClient instance.
204204
version: a str indicating the version of the Google Ads API to be
205205
used.
206+
interceptors: an optional list of interceptors to include in
207+
requests. NOTE: this parameter is not intended for non-Google
208+
use and is not officially supported.
206209
207210
Returns:
208211
A service client instance associated with the given service_name.
@@ -234,12 +237,14 @@ def get_service(self, name, version=_DEFAULT_VERSION):
234237
credentials=self.credentials,
235238
options=_GRPC_CHANNEL_OPTIONS)
236239

237-
channel = grpc.intercept_channel(
238-
channel,
240+
interceptors = interceptors + [
239241
MetadataInterceptor(self.developer_token, self.login_customer_id),
240242
LoggingInterceptor(_logger, endpoint),
241-
ExceptionInterceptor(version)
242-
)
243+
ExceptionInterceptor(version)]
244+
245+
channel = intercept_channel(
246+
channel,
247+
*interceptors)
243248

244249
service_transport = service_transport_class(channel=channel)
245250

tests/client_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,22 @@ def test_get_service_with_version(self):
400400
except Exception:
401401
self.fail('get_service with a valid version raised an error')
402402

403+
def test_get_service_with_interceptor(self):
404+
client = self._create_test_client()
405+
406+
class Interceptor:
407+
pass
408+
409+
interceptor = Interceptor()
410+
411+
with mock.patch.object(
412+
Client,
413+
'intercept_channel'
414+
) as mock_intercept_channel:
415+
client.get_service('GoogleAdsService', interceptors=[interceptor])
416+
first_interceptor = mock_intercept_channel.call_args[0][1]
417+
self.assertEqual(first_interceptor, interceptor)
418+
403419
def test_get_type(self):
404420
for ver in valid_versions:
405421
# Retrieve names for all types defined in pb2 files.

0 commit comments

Comments
 (0)