Skip to content

Commit

Permalink
Implementing topic.subscription factory.
Browse files Browse the repository at this point in the history
Also using `topic.subscription` factory in docs and regression
tests instead of instantiating a `Subscription`.

Fixes googleapis#929.
  • Loading branch information
dhermes committed Jun 17, 2015
1 parent b7eaf23 commit e001902
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
25 changes: 12 additions & 13 deletions docs/pubsub-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Create a new pull subscription for a topic:
>>> from gcloud import pubsub
>>> client = pubsub.Client()
>>> topic = client.topic('topic_name')
>>> subscription = pubsub.Subscription('subscription_name', topic)
>>> subscription = topic.subscription('subscription_name')
>>> subscription.create() # API request

Create a new pull subscription for a topic with a non-default ACK deadline:
Expand All @@ -131,8 +131,7 @@ Create a new pull subscription for a topic with a non-default ACK deadline:
>>> from gcloud import pubsub
>>> client = pubsub.Client()
>>> topic = client.topic('topic_name')
>>> subscription = pubsub.Subscription('subscription_name', topic,
... ack_deadline=90)
>>> subscription = topic.subscription('subscription_name', ack_deadline=90)
>>> subscription.create() # API request

Create a new push subscription for a topic:
Expand All @@ -143,8 +142,8 @@ Create a new push subscription for a topic:
>>> ENDPOINT = 'https://example.com/hook'
>>> client = pubsub.Client()
>>> topic = client.topic('topic_name')
>>> subscription = pubsub.Subscription('subscription_name', topic,
... push_endpoint=ENDPOINT)
>>> subscription = topic.subscription('subscription_name',
... push_endpoint=ENDPOINT)
>>> subscription.create() # API request

Check for the existence of a subscription:
Expand All @@ -154,7 +153,7 @@ Check for the existence of a subscription:
>>> from gcloud import pubsub
>>> client = pubsub.Client()
>>> topic = client.topic('topic_name')
>>> subscription = pubsub.Subscription('subscription_name', topic)
>>> subscription = topic.subscription('subscription_name')
>>> subscription.exists() # API request
True

Expand All @@ -166,7 +165,7 @@ Convert a pull subscription to push:
>>> ENDPOINT = 'https://example.com/hook'
>>> client = pubsub.Client()
>>> topic = client.topic('topic_name')
>>> subscription = pubsub.Subscription('subscription_name', topic)
>>> subscription = topic.subscription('subscription_name')
>>> subscription.modify_push_configuration(push_endpoint=ENDPOINT) # API request

Convert a push subscription to pull:
Expand All @@ -177,8 +176,8 @@ Convert a push subscription to pull:
>>> ENDPOINT = 'https://example.com/hook'
>>> client = pubsub.Client()
>>> topic = client.topic('topic_name')
>>> subscription = pubusb.Subscription('subscription_name', topic,
... push_endpoint=ENDPOINT)
>>> subscription = topic.subscription('subscription_name',
... push_endpoint=ENDPOINT)
>>> subscription.modify_push_configuration(push_endpoint=None) # API request

List subscriptions for a topic:
Expand Down Expand Up @@ -209,7 +208,7 @@ Delete a subscription:
>>> from gcloud import pubsub
>>> client = pubsub.Client()
>>> topic = client.topic('topic_name')
>>> subscription = pubsub.Subscription('subscription_name', topic)
>>> subscription = topic.subscription('subscription_name')
>>> subscription.delete() # API request


Expand All @@ -223,7 +222,7 @@ Fetch pending messages for a pull subscription:
>>> from gcloud import pubsub
>>> client = pubsub.Client()
>>> topic = client.topic('topic_name')
>>> subscription = pubsub.Subscription('subscription_name', topic)
>>> subscription = topic.subscription('subscription_name')
>>> with topic.batch() as batch:
... batch.publish('this is the first message_payload')
... batch.publish('this is the second message_payload',
Expand Down Expand Up @@ -252,7 +251,7 @@ Fetch a limited number of pending messages for a pull subscription:
>>> from gcloud import pubsub
>>> client = pubsub.Client()
>>> topic = client.topic('topic_name')
>>> subscription = pubsub.Subscription('subscription_name', topic)
>>> subscription = topic.subscription('subscription_name')
>>> with topic.batch() as batch:
... batch.publish('this is the first message_payload')
... batch.publish('this is the second message_payload',
Expand All @@ -268,7 +267,7 @@ Fetch messages for a pull subscription without blocking (none pending):
>>> from gcloud import pubsub
>>> client = pubsub.Client()
>>> topic = client.topic('topic_name')
>>> subscription = pubsub.Subscription('subscription_name', topic)
>>> subscription = topic.subscription('subscription_name')
>>> received = subscription.pull(max_messages=1) # API request
>>> messages = [recv[1] for recv in received]
>>> [message.id for message in messages]
Expand Down
4 changes: 3 additions & 1 deletion gcloud/pubsub/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from gcloud.exceptions import NotFound
from gcloud.pubsub.message import Message
from gcloud.pubsub.topic import Topic


class Subscription(object):
Expand Down Expand Up @@ -63,6 +62,9 @@ def from_api_repr(cls, resource, client, topics=None):
:rtype: :class:`gcloud.pubsub.subscription.Subscription`
:returns: Subscription parsed from ``resource``.
"""
# pylint: disable=cyclic-import
from gcloud.pubsub.topic import Topic
# pylint: enable=cyclic-import
if topics is None:
topics = {}
t_name = resource['topic']
Expand Down
14 changes: 14 additions & 0 deletions gcloud/pubsub/test_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,20 @@ def test_delete_w_alternate_client(self):
self.assertEqual(req['method'], 'DELETE')
self.assertEqual(req['path'], '/%s' % PATH)

def test_subscription(self):
from gcloud.pubsub.subscription import Subscription
TOPIC_NAME = 'topic_name'
PROJECT = 'PROJECT'
CLIENT = _Client(project=PROJECT)
topic = self._makeOne(TOPIC_NAME,
client=CLIENT)

SUBSCRIPTION_NAME = 'subscription_name'
subscription = topic.subscription(SUBSCRIPTION_NAME)
self.assertTrue(isinstance(subscription, Subscription))
self.assertEqual(subscription.name, SUBSCRIPTION_NAME)
self.assertTrue(subscription.topic is topic)


class TestBatch(unittest2.TestCase):

Expand Down
21 changes: 21 additions & 0 deletions gcloud/pubsub/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ def __init__(self, name, client, timestamp_messages=False):
self._client = client
self.timestamp_messages = timestamp_messages

def subscription(self, name, ack_deadline=None, push_endpoint=None):
"""Creates a subscription bound to the current topic.
:type name: string
:param name: the name of the subscription
:type ack_deadline: int
:param ack_deadline: the deadline (in seconds) by which messages pulled
from the back-end must be acknowledged.
:type push_endpoint: string
:param push_endpoint: URL to which messages will be pushed by the
back-end. If not set, the application must pull
messages.
"""
# pylint: disable=cyclic-import
from gcloud.pubsub.subscription import Subscription
# pylint: enable=cyclic-import
return Subscription(name, self, ack_deadline=ack_deadline,
push_endpoint=push_endpoint)

@classmethod
def from_api_repr(cls, resource, client):
"""Factory: construct a topic given its API representation
Expand Down
7 changes: 3 additions & 4 deletions regression/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from gcloud import _helpers
from gcloud import pubsub
from gcloud.pubsub.subscription import Subscription


_helpers._PROJECT_ENV_VAR_NAME = 'GCLOUD_TESTS_PROJECT_ID'
Expand Down Expand Up @@ -68,7 +67,7 @@ def test_create_subscription(self):
topic.create()
self.to_delete.append(topic)
SUBSCRIPTION_NAME = 'subscribing-now'
subscription = Subscription(SUBSCRIPTION_NAME, topic)
subscription = topic.subscription(SUBSCRIPTION_NAME)
self.assertFalse(subscription.exists())
subscription.create()
self.to_delete.append(subscription)
Expand All @@ -88,7 +87,7 @@ def test_list_subscriptions(self):
'newest%d' % (1000 * time.time(),),
]
for subscription_name in subscriptions_to_create:
subscription = Subscription(subscription_name, topic)
subscription = topic.subscription(subscription_name)
subscription.create()
self.to_delete.append(subscription)

Expand All @@ -106,7 +105,7 @@ def test_message_pull_mode_e2e(self):
topic.create()
self.to_delete.append(topic)
SUBSCRIPTION_NAME = 'subscribing-now'
subscription = Subscription(SUBSCRIPTION_NAME, topic)
subscription = topic.subscription(SUBSCRIPTION_NAME)
self.assertFalse(subscription.exists())
subscription.create()
self.to_delete.append(subscription)
Expand Down

0 comments on commit e001902

Please sign in to comment.