Ejemplo n.º 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.
        """
        api_module = self._get_api_services_by_version(version)
        interceptors = interceptors or []

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

        try:
            service_transport_class = getattr(
                api_module, _SERVICE_GRPC_TRANSPORT_TEMPLATE.format(name))
        except AttributeError:
            raise ValueError('Grpc transport does not exist for the specified '
                             'service "{}".'.format(name))

        endpoint = (self.endpoint if self.endpoint
                    else service_client.SERVICE_ADDRESS)

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

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

        channel = grpc.intercept_channel(
            channel,
            *interceptors)

        service_transport = service_transport_class(channel=channel)

        return service_client(transport=service_transport)
    def _create_test_interceptor(self,
                                 logger=mock.Mock(),
                                 endpoint=_MOCK_ENDPOINT):
        """Creates a LoggingInterceptor instance.

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

        Returns:
            A LoggingInterceptor instance.

        Args:
            config: A dict configuration
            endpoint: A str representing an endpoint
        """
        return LoggingInterceptor(logger, endpoint)
Ejemplo n.º 3
0
    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()