def test_list_topic_subscriptions(self):
        # Setup Expected Response
        next_page_token = ''
        subscriptions_element = 'subscriptionsElement1698708147'
        subscriptions = [subscriptions_element]
        expected_response = {
            'next_page_token': next_page_token,
            'subscriptions': subscriptions
        }
        expected_response = pubsub_pb2.ListTopicSubscriptionsResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = publisher_client.PublisherClient(channel=channel)

        # Setup Request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')

        paged_list_response = client.list_topic_subscriptions(topic)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.subscriptions[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.ListTopicSubscriptionsRequest(
            topic=topic)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Beispiel #2
0
    def test_list_topic_subscriptions(self):
        # Setup Expected Response
        next_page_token = ""
        subscriptions_element = "subscriptionsElement1698708147"
        subscriptions = [subscriptions_element]
        expected_response = {
            "next_page_token": next_page_token,
            "subscriptions": subscriptions,
        }
        expected_response = pubsub_pb2.ListTopicSubscriptionsResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = publisher_client.PublisherClient()

        # Setup Request
        topic = client.topic_path("[PROJECT]", "[TOPIC]")

        paged_list_response = client.list_topic_subscriptions(topic)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.subscriptions[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = pubsub_pb2.ListTopicSubscriptionsRequest(
            topic=topic)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Beispiel #3
0
    def list_topic_subscriptions(
            self,
            topic,
            page_size=None,
            retry=google.api_core.gapic_v1.method.DEFAULT,
            timeout=google.api_core.gapic_v1.method.DEFAULT,
            metadata=None):
        """
        Lists the names of the subscriptions on this topic.

        Example:
            >>> from google.cloud import pubsub_v1
            >>>
            >>> client = pubsub_v1.PublisherClient()
            >>>
            >>> topic = client.topic_path('[PROJECT]', '[TOPIC]')
            >>>
            >>> # Iterate over all results
            >>> for element in client.list_topic_subscriptions(topic):
            ...     # process element
            ...     pass
            >>>
            >>>
            >>> # Alternatively:
            >>>
            >>> # Iterate over results one page at a time
            >>> for page in client.list_topic_subscriptions(topic).pages:
            ...     for element in page:
            ...         # process element
            ...         pass

        Args:
            topic (str): The name of the topic that subscriptions are attached to. Format is
                ``projects/{project}/topics/{topic}``.
            page_size (int): The maximum number of resources contained in the
                underlying API response. If page streaming is performed per-
                resource, this parameter does not affect the return value. If page
                streaming is performed per-page, this determines the maximum number
                of resources in a page.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            A :class:`~google.gax.PageIterator` instance. By default, this
            is an iterable of :class:`str` instances.
            This object can also be configured to iterate over the pages
            of the response through the `options` parameter.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        # Wrap the transport method to add retry and timeout logic.
        if 'list_topic_subscriptions' not in self._inner_api_calls:
            self._inner_api_calls[
                'list_topic_subscriptions'] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.list_topic_subscriptions,
                    default_retry=self.
                    _method_configs['ListTopicSubscriptions'].retry,
                    default_timeout=self.
                    _method_configs['ListTopicSubscriptions'].timeout,
                    client_info=self._client_info,
                )

        request = pubsub_pb2.ListTopicSubscriptionsRequest(
            topic=topic,
            page_size=page_size,
        )
        iterator = google.api_core.page_iterator.GRPCIterator(
            client=None,
            method=functools.partial(
                self._inner_api_calls['list_topic_subscriptions'],
                retry=retry,
                timeout=timeout,
                metadata=metadata),
            request=request,
            items_field='subscriptions',
            request_token_field='page_token',
            response_token_field='next_page_token',
        )
        return iterator