def list_topics(self, page_size=None, page_token=None): """List topics for the project associated with this client. See: https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/list Example: .. literalinclude:: pubsub_snippets.py :start-after: [START client_list_topics] :end-before: [END client_list_topics] :type page_size: int :param page_size: maximum number of topics to return, If not passed, defaults to a value set by the API. :type page_token: string :param page_token: opaque marker for the next "page" of topics. If not passed, the API will return the first page of topics. :rtype: tuple, (list, str) :returns: list of :class:`google.cloud.pubsub.topic.Topic`, plus a "next page token" string: if not None, indicates that more topics can be retrieved with another call (pass that value as ``page_token``). """ api = self.publisher_api resources, next_token = api.list_topics(self.project, page_size, page_token) topics = [ Topic.from_api_repr(resource, self) for resource in resources ] return topics, next_token
def test_topic_list_subscriptions_no_paging(self): from google.cloud.pubsub.topic import Topic from google.cloud.pubsub.subscription import Subscription local_sub_path = 'projects/%s/subscriptions/%s' % ( self.PROJECT, self.SUB_NAME) RETURNED = {'subscriptions': [local_sub_path]} connection = _Connection(RETURNED) client = _Client(connection, self.PROJECT) api = self._make_one(client) topic = Topic(self.TOPIC_NAME, client) iterator = api.topic_list_subscriptions(topic) subscriptions = list(iterator) next_token = iterator.next_page_token self.assertIsNone(next_token) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertEqual(subscription.topic, topic) self.assertIs(subscription._client, client) self.assertEqual(connection._called_with['method'], 'GET') path = '/%s' % (self.LIST_TOPIC_SUBSCRIPTIONS_PATH,) self.assertEqual(connection._called_with['path'], path) self.assertEqual(connection._called_with['query_params'], {})
def test_topic_list_subscriptions_with_paging(self): from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' local_sub_path = '%s/subscriptions/%s' % (self.PROJECT_PATH, self.SUB_NAME) response = _GAXPageIterator([local_sub_path], page_token=NEW_TOKEN) gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response) client = _Client(self.PROJECT) api = self._make_one(gax_api, client) topic = Topic(self.TOPIC_NAME, client) iterator = api.topic_list_subscriptions(topic, page_size=SIZE, page_token=TOKEN) subscriptions = list(iterator) next_token = iterator.next_page_token self.assertEqual(next_token, NEW_TOKEN) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertEqual(subscription.topic, topic) self.assertIs(subscription._client, client) name, page_size, options = ( gax_api._list_topic_subscriptions_called_with) self.assertEqual(name, self.TOPIC_PATH) self.assertEqual(page_size, SIZE) self.assertEqual(options.page_token, TOKEN)
def list_topics(self, page_size=None, page_token=None): """List topics for the project associated with this client. See: https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/list Example: .. literalinclude:: pubsub_snippets.py :start-after: [START client_list_topics] :end-before: [END client_list_topics] :type page_size: int :param page_size: maximum number of topics to return, If not passed, defaults to a value set by the API. :type page_token: string :param page_token: opaque marker for the next "page" of topics. If not passed, the API will return the first page of topics. :rtype: tuple, (list, str) :returns: list of :class:`google.cloud.pubsub.topic.Topic`, plus a "next page token" string: if not None, indicates that more topics can be retrieved with another call (pass that value as ``page_token``). """ api = self.publisher_api resources, next_token = api.list_topics( self.project, page_size, page_token) topics = [Topic.from_api_repr(resource, self) for resource in resources] return topics, next_token
def test_topic_list_subscriptions_no_paging(self): from google.gax import INITIAL_PAGE from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic local_sub_path = '%s/subscriptions/%s' % (self.PROJECT_PATH, self.SUB_NAME) response = _GAXPageIterator([local_sub_path]) gax_api = _GAXPublisherAPI(_list_topic_subscriptions_response=response) client = _Client(self.PROJECT) api = self._makeOne(gax_api, client) topic = Topic(self.TOPIC_NAME, client) iterator = api.topic_list_subscriptions(topic) subscriptions = list(iterator) next_token = iterator.next_page_token self.assertIsNone(next_token) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertEqual(subscription.topic, topic) self.assertIs(subscription._client, client) topic_path, page_size, options = ( gax_api._list_topic_subscriptions_called_with) self.assertEqual(topic_path, self.TOPIC_PATH) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def _item_to_topic(iterator, resource): """Convert a JSON topic to the native object. :type iterator: :class:`~google.cloud.iterator.Iterator` :param iterator: The iterator that is currently in use. :type resource: dict :param resource: A topic returned from the API. :rtype: :class:`~google.cloud.pubsub.topic.Topic` :returns: The next topic in the page. """ return Topic.from_api_repr(resource, iterator.client)
def _item_to_topic(iterator, resource): """Convert a JSON topic to the native object. :type iterator: :class:`~google.cloud.iterator.Iterator` :param iterator: The iterator that is currently in use. :type resource: dict :param resource: A topic returned from the API. :rtype: :class:`~google.cloud.pubsub.topic.Topic` :returns: The next topic in the page. """ return Topic.from_api_repr(resource, iterator.client)
def _item_to_topic(iterator, resource): """Convert a protobuf topic to the native object. :type iterator: :class:`~google.api.core.page_iterator.Iterator` :param iterator: The iterator that is currently in use. :type resource: :class:`.pubsub_pb2.Topic` :param resource: A topic returned from the API. :rtype: :class:`~google.cloud.pubsub.topic.Topic` :returns: The next topic in the page. """ return Topic.from_api_repr({'name': resource.name}, iterator.client)
def test_topic_list_subscriptions_miss(self): from google.cloud.exceptions import NotFound from google.cloud.pubsub.topic import Topic connection = _Connection() client = _Client(connection, self.PROJECT) api = self._make_one(client) with self.assertRaises(NotFound): topic = Topic(self.TOPIC_NAME, client) list(api.topic_list_subscriptions(topic)) self.assertEqual(connection._called_with['method'], 'GET') path = '/%s' % (self.LIST_TOPIC_SUBSCRIPTIONS_PATH,) self.assertEqual(connection._called_with['path'], path) self.assertEqual(connection._called_with['query_params'], {})
def test_topic_list_subscriptions_error(self): from google.gax import INITIAL_PAGE from google.gax.errors import GaxError from google.cloud.pubsub.topic import Topic gax_api = _GAXPublisherAPI(_random_gax_error=True) client = _Client(self.PROJECT) api = self._make_one(gax_api, client) with self.assertRaises(GaxError): topic = Topic(self.TOPIC_NAME, client) api.topic_list_subscriptions(topic) topic_path, page_size, options = ( gax_api._list_topic_subscriptions_called_with) self.assertEqual(topic_path, self.TOPIC_PATH) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def test_topic_list_subscriptions_miss(self): from google.gax import INITIAL_PAGE from google.cloud.exceptions import NotFound from google.cloud.pubsub.topic import Topic gax_api = _GAXPublisherAPI() client = _Client(self.PROJECT) api = self._make_one(gax_api, client) with self.assertRaises(NotFound): topic = Topic(self.TOPIC_NAME, client) api.topic_list_subscriptions(topic) topic_path, page_size, options = ( gax_api._list_topic_subscriptions_called_with) self.assertEqual(topic_path, self.TOPIC_PATH) self.assertEqual(page_size, 0) self.assertIs(options.page_token, INITIAL_PAGE)
def topic(self, name, timestamp_messages=False): """Creates a topic bound to the current client. Example: .. literalinclude:: pubsub_snippets.py :start-after: [START client_topic] :end-before: [END client_topic] :type name: str :param name: the name of the topic to be constructed. :type timestamp_messages: bool :param timestamp_messages: To be passed to ``Topic`` constructor. :rtype: :class:`google.cloud.pubsub.topic.Topic` :returns: Topic created with the current client. """ return Topic(name, client=self, timestamp_messages=timestamp_messages)
def test_list_topics_no_paging(self): from google.cloud.pubsub.topic import Topic creds = _Credentials() client = self._makeOne(project=self.PROJECT, credentials=creds) client.connection = object() api = _FauxPublisherAPI(items=[Topic(self.TOPIC_NAME, client)]) client._publisher_api = api iterator = client.list_topics() topics = list(iterator) next_page_token = iterator.next_page_token self.assertEqual(len(topics), 1) self.assertIsInstance(topics[0], Topic) self.assertEqual(topics[0].name, self.TOPIC_NAME) self.assertIsNone(next_page_token) self.assertEqual(api._listed_topics, (self.PROJECT, None, None))
def test_topic_list_subscriptions_missing_key(self): from google.cloud.pubsub.topic import Topic connection = _Connection({}) client = _Client(connection, self.PROJECT) api = self._make_one(client) topic = Topic(self.TOPIC_NAME, client) iterator = api.topic_list_subscriptions(topic) subscriptions = list(iterator) next_token = iterator.next_page_token self.assertEqual(len(subscriptions), 0) self.assertIsNone(next_token) self.assertEqual(connection._called_with['method'], 'GET') path = '/%s' % (self.LIST_TOPIC_SUBSCRIPTIONS_PATH,) self.assertEqual(connection._called_with['path'], path) self.assertEqual(connection._called_with['query_params'], {})
def test_topic_list_subscriptions_with_paging(self): import six from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic TOKEN1 = 'TOKEN1' TOKEN2 = 'TOKEN2' SIZE = 1 local_sub_path = 'projects/%s/subscriptions/%s' % (self.PROJECT, self.SUB_NAME) RETURNED = { 'subscriptions': [local_sub_path], 'nextPageToken': TOKEN2, } connection = _Connection(RETURNED) client = _Client(connection, self.PROJECT) api = self._makeOne(client) topic = Topic(self.TOPIC_NAME, client) iterator = api.topic_list_subscriptions(topic, page_token=TOKEN1, page_size=SIZE) page = six.next(iterator.pages) subscriptions = list(page) next_token = iterator.next_page_token self.assertEqual(next_token, TOKEN2) self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertEqual(subscription.topic, topic) self.assertIs(subscription._client, client) self.assertEqual(connection._called_with['method'], 'GET') path = '/%s' % (self.LIST_TOPIC_SUBSCRIPTIONS_PATH, ) self.assertEqual(connection._called_with['path'], path) self.assertEqual(connection._called_with['query_params'], { 'pageToken': TOKEN1, 'pageSize': SIZE })
def test_list_topics_with_paging(self): from google.cloud.pubsub.topic import Topic TOKEN1 = 'TOKEN1' TOKEN2 = 'TOKEN2' SIZE = 1 creds = _Credentials() client = self._makeOne(project=self.PROJECT, credentials=creds) client.connection = object() api = _FauxPublisherAPI([Topic(self.TOPIC_NAME, client)], TOKEN2) client._publisher_api = api iterator = client.list_topics(SIZE, TOKEN1) topics = list(iterator) next_page_token = iterator.next_page_token self.assertEqual(len(topics), 1) self.assertIsInstance(topics[0], Topic) self.assertEqual(topics[0].name, self.TOPIC_NAME) self.assertEqual(next_page_token, TOKEN2) self.assertEqual(api._listed_topics, (self.PROJECT, 1, TOKEN1))
def topic(self, name): from google.cloud.pubsub.topic import Topic return Topic(name, client=self)
def topic(self, name, timestamp_messages=False): from google.cloud.pubsub.topic import Topic return Topic(name, client=self, timestamp_messages=timestamp_messages)