def test_create_topic(self): new_topic_name = 'a-new-topic' topic = Topic(new_topic_name) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) self.assertTrue(topic.exists()) self.assertEqual(topic.name, new_topic_name)
def test_create_topic(self): TOPIC_NAME = 'a-new-topic' topic = Topic(TOPIC_NAME) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) self.assertTrue(topic.exists()) self.assertEqual(topic.name, TOPIC_NAME)
def test_create_subscription(self): TOPIC_NAME = 'subscribe-me' topic = Topic(TOPIC_NAME) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) SUBSCRIPTION_NAME = 'subscribing-now' subscription = Subscription(SUBSCRIPTION_NAME, topic) self.assertFalse(subscription.exists()) subscription.create() self.to_delete.append(subscription) self.assertTrue(subscription.exists()) self.assertEqual(subscription.name, SUBSCRIPTION_NAME) self.assertTrue(subscription.topic is topic)
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 :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:`gcloud.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``). """ params = {} if page_size is not None: params['pageSize'] = page_size if page_token is not None: params['pageToken'] = page_token path = '/projects/%s/topics' % (self.project,) resp = self.connection.api_request(method='GET', path=path, query_params=params) topics = [Topic.from_api_repr(resource, self) for resource in resp.get('topics', ())] return topics, resp.get('nextPageToken')
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:`gcloud.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 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 :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:`gcloud.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 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/v1beta2/projects/topics/list :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:`gcloud.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``). """ params = {} if page_size is not None: params['pageSize'] = page_size if page_token is not None: params['pageToken'] = page_token path = '/projects/%s/topics' % (self.project,) resp = self.connection.api_request(method='GET', path=path, query_params=params) topics = [Topic.from_api_repr(resource, self) for resource in resp['topics']] return topics, resp.get('nextPageToken')
def from_api_repr(cls, resource, client, topics=None): """Factory: construct a topic given its API representation :type resource: dict :param resource: topic resource representation returned from the API :type client: :class:`gcloud.pubsub.client.Client` :param client: Client which holds credentials and project configuration for a topic. :type topics: dict or None :param topics: A mapping of topic names -> topics. If not passed, the subscription will have a newly-created topic. :rtype: :class:`gcloud.pubsub.subscription.Subscription` :returns: Subscription parsed from ``resource``. """ if topics is None: topics = {} t_name = resource['topic'] topic = topics.get(t_name) if topic is None: topic = topics[t_name] = Topic.from_api_repr({'name': t_name}, client) _, _, _, name = resource['name'].split('/') ack_deadline = resource.get('ackDeadlineSeconds') push_config = resource.get('pushConfig', {}) push_endpoint = push_config.get('pushEndpoint') return cls(name, topic, ack_deadline, push_endpoint)
def test_message_pull_mode_e2e(self): TOPIC_NAME = 'subscribe-me' topic = Topic(TOPIC_NAME, timestamp_messages=True) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) SUBSCRIPTION_NAME = 'subscribing-now' subscription = Subscription(SUBSCRIPTION_NAME, topic) self.assertFalse(subscription.exists()) subscription.create() self.to_delete.append(subscription) MESSAGE_1 = b'MESSAGE ONE' MESSAGE_2 = b'MESSAGE ONE' EXTRA_1 = 'EXTRA 1' EXTRA_2 = 'EXTRA 2' topic.publish(MESSAGE_1, extra=EXTRA_1) topic.publish(MESSAGE_2, extra=EXTRA_2) received = subscription.pull(max_messages=2) ack_ids = [recv[0] for recv in received] subscription.acknowledge(ack_ids) messages = [recv[1] for recv in received] def _by_timestamp(message): return message.timestamp message1, message2 = sorted(messages, key=_by_timestamp) self.assertEqual(message1.data, MESSAGE_1) self.assertEqual(message1.attributes['extra'], EXTRA_1) self.assertEqual(message2.data, MESSAGE_2) self.assertEqual(message2.attributes['extra'], EXTRA_2)
def test_list_topics(self): topics_to_create = [ 'new%d' % (1000 * time.time(),), 'newer%d' % (1000 * time.time(),), 'newest%d' % (1000 * time.time(),), ] for topic_name in topics_to_create: topic = Topic(topic_name) topic.create() self.to_delete.append(topic) # Retrieve the topics. all_topics, _ = pubsub.list_topics() project_id = pubsub.get_default_project() created = [topic for topic in all_topics if topic.name in topics_to_create and topic.project == project_id] self.assertEqual(len(created), len(topics_to_create))
def test_list_topics(self): topics_to_create = [ 'new%d' % (1000 * time.time(), ), 'newer%d' % (1000 * time.time(), ), 'newest%d' % (1000 * time.time(), ), ] for topic_name in topics_to_create: topic = Topic(topic_name) topic.create() self.to_delete.append(topic) # Retrieve the topics. all_topics, _ = pubsub.list_topics() project = pubsub.get_default_project() created = [ topic for topic in all_topics if topic.name in topics_to_create and topic.project == project ] self.assertEqual(len(created), len(topics_to_create))
def test_list_subscriptions(self): TOPIC_NAME = 'subscribe-me' topic = Topic(TOPIC_NAME) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) subscriptions_to_create = [ 'new%d' % (1000 * time.time(),), 'newer%d' % (1000 * time.time(),), 'newest%d' % (1000 * time.time(),), ] for subscription_name in subscriptions_to_create: subscription = Subscription(subscription_name, topic) subscription.create() self.to_delete.append(subscription) # Retrieve the subscriptions. all_subscriptions, _ = pubsub.list_subscriptions() created = [subscription for subscription in all_subscriptions if subscription.name in subscriptions_to_create and subscription.topic.name == TOPIC_NAME] self.assertEqual(len(created), len(subscriptions_to_create))
def topic(self, name, timestamp_messages=False): """Creates a topic bound to the current client. :type name: string :param name: the name of the topic to be constructed. :type timestamp_messages: boolean :param timestamp_messages: To be passed to ``Topic`` constructor. :rtype: :class:`gcloud.pubsub.topic.Topic` :returns: Topic created with the current client. """ return Topic(name, client=self, timestamp_messages=timestamp_messages)
def list_topics(page_size=None, page_token=None, project=None, connection=None): """List topics for a given project. See: https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics/list :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. :type project: string :param project: project ID to query. If not passed, defaults to the project ID inferred from the environment. :type connection: :class:`gcloud.pubsub.connection.Connection` :param connection: connection to use for the query. If not passed, defaults to the connection inferred from the environment. :rtype: tuple, (list, str) :returns: list of :class:`gcloud.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``). """ if project is None: project = get_default_project() if connection is None: connection = get_default_connection() params = {} if page_size is not None: params['pageSize'] = page_size if page_token is not None: params['pageToken'] = page_token path = '/projects/%s/topics' % project resp = connection.api_request(method='GET', path=path, query_params=params) topics = [Topic.from_api_repr(resource, connection) for resource in resp['topics']] return topics, resp.get('nextPageToken')
def test_list_subscriptions(self): TOPIC_NAME = 'subscribe-me' topic = Topic(TOPIC_NAME) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) subscriptions_to_create = [ 'new%d' % (1000 * time.time(), ), 'newer%d' % (1000 * time.time(), ), 'newest%d' % (1000 * time.time(), ), ] for subscription_name in subscriptions_to_create: subscription = Subscription(subscription_name, topic) subscription.create() self.to_delete.append(subscription) # Retrieve the subscriptions. all_subscriptions, _ = pubsub.list_subscriptions() created = [ subscription for subscription in all_subscriptions if subscription.name in subscriptions_to_create and subscription.topic.name == TOPIC_NAME ] self.assertEqual(len(created), len(subscriptions_to_create))
def list_topics(page_size=None, page_token=None, project=None, connection=None): """List topics for a given project. See: https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/topics/list :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. :type project: string :param project: project ID to query. If not passed, defaults to the project ID inferred from the environment. :type connection: :class:`gcloud.pubsub.connection.Connection` :param connection: connection to use for the query. If not passed, defaults to the connection inferred from the environment. :rtype: tuple, (list, str) :returns: list of :class:`gcloud.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``). """ if project is None: project = get_default_project() connection = _require_connection(connection) params = {} if page_size is not None: params['pageSize'] = page_size if page_token is not None: params['pageToken'] = page_token path = '/projects/%s/topics' % project resp = connection.api_request(method='GET', path=path, query_params=params) topics = [Topic.from_api_repr(resource) for resource in resp['topics']] return topics, resp.get('nextPageToken')
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: string :param name: the name of the topic to be constructed. :type timestamp_messages: boolean :param timestamp_messages: To be passed to ``Topic`` constructor. :rtype: :class:`gcloud.pubsub.topic.Topic` :returns: Topic created with the current client. """ return Topic(name, client=self, timestamp_messages=timestamp_messages)
def from_api_repr(cls, resource, topics=None): """Factory: construct a topic given its API representation :type resource: dict :param resource: topic resource representation returned from the API :type topics: dict or None :param topics: A mapping of topic names -> topics. If not passed, the subscription will have a newly-created topic. :rtype: :class:`gcloud.pubsub.subscription.Subscription` """ if topics is None: topics = {} t_name = resource['topic'] topic = topics.get(t_name) if topic is None: topic = topics[t_name] = Topic.from_api_repr({'name': t_name}) _, _, _, name = resource['name'].split('/') ack_deadline = resource.get('ackDeadlineSeconds') push_config = resource.get('pushConfig', {}) push_endpoint = push_config.get('pushEndpoint') return cls(name, topic, ack_deadline, push_endpoint)
def test_message_pull_mode_e2e(self): TOPIC_NAME = 'subscribe-me' topic = Topic(TOPIC_NAME) self.assertFalse(topic.exists()) topic.create() self.to_delete.append(topic) SUBSCRIPTION_NAME = 'subscribing-now' subscription = Subscription(SUBSCRIPTION_NAME, topic) self.assertFalse(subscription.exists()) subscription.create() self.to_delete.append(subscription) MESSAGE = b'MESSAGE' EXTRA = 'EXTRA' topic.publish(MESSAGE, extra=EXTRA) received = subscription.pull() ack_ids = [recv[0] for recv in received] subscription.acknowledge(ack_ids) messages = [recv[1] for recv in received] message, = messages self.assertEqual(message.data, MESSAGE) self.assertEqual(message.attributes, {'extra': EXTRA})
def topic(self, name, timestamp_messages=False): from gcloud.pubsub.topic import Topic return Topic(name, client=self, timestamp_messages=timestamp_messages)