Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#744: return Topic instances from pubsub.api.list_topics() #790

Merged
merged 2 commits into from
Mar 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gcloud/pubsub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from gcloud.connection import get_scoped_connection
from gcloud.pubsub import _implicit_environ
from gcloud.pubsub._implicit_environ import get_default_connection
from gcloud.pubsub.api import list_topics
from gcloud.pubsub.connection import Connection


Expand Down
14 changes: 10 additions & 4 deletions gcloud/pubsub/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from gcloud._helpers import get_default_project
from gcloud.pubsub._implicit_environ import get_default_connection
from gcloud.pubsub.topic import Topic


def list_topics(page_size=None, page_token=None,
Expand All @@ -42,9 +43,9 @@ def list_topics(page_size=None, page_token=None,
defaults to the connection inferred from the
environment.

:rtype: dict
:returns: keys include ``topics`` (a list of topic mappings) and
``nextPageToken`` (a string: if non-empty, indicates that
: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``).
"""
Expand All @@ -63,7 +64,12 @@ def list_topics(page_size=None, page_token=None,
params['pageToken'] = page_token

path = '/projects/%s/topics' % project
return connection.api_request(method='GET', path=path, query_params=params)
resp = connection.api_request(method='GET', path=path, query_params=params)
topics = []
for full_name in [topic['name'] for topic in resp['topics']]:
_, t_project, _, name = full_name.split('/')
topics.append(Topic(name, t_project, connection))
return topics, resp.get('nextPageToken')


def list_subscriptions(page_size=None, page_token=None, topic_name=None,
Expand Down
39 changes: 22 additions & 17 deletions gcloud/pubsub/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ def _callFUT(self, *args, **kw):
return list_topics(*args, **kw)

def test_w_explicit_connection_no_paging(self):
from gcloud.pubsub.topic import Topic
TOPIC_NAME = 'topic_name'
PROJECT = 'PROJECT'
TOKEN = 'TOKEN'
returned = {'topics': [{'name': TOPIC_NAME}],
'nextPageToken': TOKEN}
TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
returned = {'topics': [{'name': TOPIC_PATH}]}
conn = _Connection(returned)
response = self._callFUT(project=PROJECT, connection=conn)
topics = response['topics']
topics, next_page_token = self._callFUT(project=PROJECT,
connection=conn)
self.assertEqual(len(topics), 1)
self.assertEqual(topics[0], {'name': TOPIC_NAME})
self.assertEqual(response['nextPageToken'], TOKEN)
self.assertTrue(isinstance(topics[0], Topic))
self.assertEqual(topics[0].name, TOPIC_NAME)
self.assertEqual(next_page_token, None)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'GET')
Expand All @@ -42,39 +43,43 @@ def test_w_explicit_connection_no_paging(self):
def test_w_implicit_connection_and_project_wo_paging(self):
from gcloud._testing import _monkey_defaults as _monkey_base_defaults
from gcloud.pubsub._testing import _monkey_defaults
from gcloud.pubsub.topic import Topic
TOPIC_NAME = 'topic_name'
PROJECT = 'PROJECT'
TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
TOKEN = 'TOKEN'
returned = {'topics': [{'name': TOPIC_NAME}],
returned = {'topics': [{'name': TOPIC_PATH}],
'nextPageToken': TOKEN}
conn = _Connection(returned)
with _monkey_base_defaults(project=PROJECT):
with _monkey_defaults(connection=conn):
response = self._callFUT()
topics = response['topics']
topics, next_page_token = self._callFUT()
self.assertEqual(len(topics), 1)
self.assertEqual(topics[0], {'name': TOPIC_NAME})
self.assertEqual(response['nextPageToken'], TOKEN)
self.assertTrue(isinstance(topics[0], Topic))
self.assertEqual(topics[0].name, TOPIC_NAME)
self.assertEqual(next_page_token, TOKEN)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/projects/%s/topics' % PROJECT)
self.assertEqual(req['query_params'], {})

def test_w_explicit_connection_and_project_w_paging(self):
from gcloud.pubsub.topic import Topic
TOPIC_NAME = 'topic_name'
PROJECT = 'PROJECT'
TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
TOKEN1 = 'TOKEN1'
TOKEN2 = 'TOKEN2'
SIZE = 1
returned = {'topics': [{'name': TOPIC_NAME}],
returned = {'topics': [{'name': TOPIC_PATH}],
'nextPageToken': TOKEN2}
conn = _Connection(returned)
response = self._callFUT(SIZE, TOKEN1, PROJECT, conn)
topics = response['topics']
topics, next_page_token = self._callFUT(SIZE, TOKEN1, PROJECT, conn)
self.assertEqual(len(topics), 1)
self.assertEqual(topics[0], {'name': TOPIC_NAME})
self.assertEqual(response['nextPageToken'], TOKEN2)
self.assertTrue(isinstance(topics[0], Topic))
self.assertEqual(topics[0].name, TOPIC_NAME)
self.assertEqual(next_page_token, TOKEN2)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'GET')
Expand Down
22 changes: 22 additions & 0 deletions regression/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import time

import unittest2

from gcloud import _helpers
Expand Down Expand Up @@ -40,3 +42,23 @@ def test_create_topic(self):
self.to_delete.append(topic)
self.assertTrue(topic.exists())
self.assertEqual(topic.name, new_topic_name)

def test_list_topics(self):
topics_to_create = [
'new%d' % (1000 * time.time(),),
'newer%d' % (1000 * time.time(),),
'newest%d' % (1000 * time.time(),),
]
created_topics = []
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_topics = [topic for topic in all_topics
if topic.name in topics_to_create and
topic.project == project_id]
self.assertEqual(len(created_topics), len(topics_to_create))