Exemplo n.º 1
0
    def _create_test_interceptor(self):
        """Creates and returns an ExceptionInterceptor instance

        Returns:
            An ExceptionInterceptor instance.
        """
        return ExceptionInterceptor(Client._DEFAULT_VERSION)
Exemplo n.º 2
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)