Example #1
0
    def get_service(self, name, version=_DEFAULT_VERSION, interceptors=None):
        """Returns a service client instance for the specified service_name.

        Args:
            name: a str indicating the name of the service for which a
                service client is being retrieved; e.g. you may specify
                "CampaignService" to retrieve a CampaignServiceClient instance.
            version: a str indicating the version of the Google Ads API to be
                used.
            interceptors: an optional list of interceptors to include in
                requests. NOTE: this parameter is not intended for non-Google
                use and is not officially supported.

        Returns:
            A service client instance associated with the given service_name.

        Raises:
            AttributeError: If the specified name doesn't exist.
        """
        # If version is specified when the instance is created,
        # override any version specified as an argument.
        version = self.version if self.version else version
        api_module = self._get_api_services_by_version(version)
        interceptors = interceptors or []

        try:
            service_client_class = getattr(
                api_module, _SERVICE_CLIENT_TEMPLATE.format(name))
        except AttributeError:
            raise ValueError('Specified service {}" does not exist in Google '
                             "Ads API {}.".format(name, version))

        service_transport_class = service_client_class.get_transport_class()

        endpoint = (self.endpoint if self.endpoint else
                    service_client_class.DEFAULT_ENDPOINT)

        channel = service_transport_class.create_channel(
            host=endpoint,
            credentials=self.credentials,
            options=_GRPC_CHANNEL_OPTIONS,
        )

        interceptors = interceptors + [
            MetadataInterceptor(
                self.developer_token,
                self.login_customer_id,
                self.linked_customer_id,
            ),
            LoggingInterceptor(_logger, version, endpoint),
            ExceptionInterceptor(version, use_proto_plus=self.use_proto_plus),
        ]

        channel = grpc.intercept_channel(channel, *interceptors)

        service_transport = service_transport_class(channel=channel,
                                                    client_info=_CLIENT_INFO)

        return service_client_class(transport=service_transport)
    def test_intercept_unary_stream_unconfigured(self):
        """No _logger methods should be called.

        When intercepting requests, no logging methods should be called if
        LoggingInterceptor was initialized without a configuration.
        """
        mock_client_call_details = self._get_mock_client_call_details()
        mock_continuation_fn = self._get_mock_continuation_fn()
        mock_request = self._get_mock_request()
        # Since logging configuration is global it needs to be reset here
        # so that state from previous tests does not affect these assertions
        logging.disable(logging.CRITICAL)
        logger_spy = mock.Mock(wraps=Client._logger)
        interceptor = LoggingInterceptor(logger_spy, default_version)
        interceptor.intercept_unary_stream(
            mock_continuation_fn, mock_client_call_details, mock_request
        )

        logger_spy.debug.assert_not_called()
        logger_spy.info.assert_not_called()
        logger_spy.warning.assert_not_called()
    def _create_test_interceptor(
        self, logger=mock.Mock(), version=None, endpoint=_MOCK_ENDPOINT
    ):
        """Creates a LoggingInterceptor instance.

        Accepts parameters that are used to override defaults when needed
        for testing.

        Args:
            config: A dict configuration
            endpoint: A str representing an endpoint

        Returns:
            A LoggingInterceptor instance.
        """
        if not version:
            version = default_version

        return LoggingInterceptor(logger, version, endpoint)