Skip to content

Commit 01d697e

Browse files
committed
Merge pull request #742 from tseaver/691-flesh_out_pubsub_topics
Flesh out pubsub topics
2 parents aa684be + 703d56b commit 01d697e

File tree

10 files changed

+1226
-0
lines changed

10 files changed

+1226
-0
lines changed

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
extensions = [
3131
'sphinx.ext.autodoc',
3232
'sphinx.ext.autosummary',
33+
'sphinx.ext.doctest',
3334
'sphinx.ext.todo',
3435
'sphinx.ext.viewcode',
3536
]

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
storage-blobs
1515
storage-buckets
1616
storage-acl
17+
pubsub-api
1718

1819

1920
Getting started

docs/pubsub-api.rst

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
``gcloud.pubsub`` API
2+
=====================
3+
4+
Connection / Authorization
5+
--------------------------
6+
7+
- Inferred defaults used to create connection if none configured explicitly:
8+
9+
- credentials (derived from GAE / GCE environ if present).
10+
11+
- ``project_id`` (derived from GAE / GCE environ if present).
12+
13+
- ``scopes``
14+
15+
16+
Manage topics for a project
17+
---------------------------
18+
19+
Create a new topic for the default project:
20+
21+
.. doctest::
22+
23+
>>> from gcloud.pubsub.topic import Topic
24+
>>> topic = Topic('topic_name')
25+
>>> topic.create() # API request
26+
27+
Create a new topic for an explicit project:
28+
29+
.. doctest::
30+
31+
>>> from gcloud.pubsub.topic import Topic
32+
>>> topic = Topic('topic_name', project_id='my.project')
33+
>>> topic.create() # API request
34+
35+
Check for the existance of a topic:
36+
37+
.. doctest::
38+
39+
>>> from gcloud.pubsub.topic import Topic
40+
>>> topic = Topic('topic_name')
41+
>>> topic.exists() # API request
42+
True
43+
44+
List topics for the default project:
45+
46+
.. doctest::
47+
48+
>>> from gcloud import pubsub
49+
>>> [topic.name for topic in pubsub.list_topics()] # API request
50+
['topic_name']
51+
52+
List topics for an explicit project:
53+
54+
.. doctest::
55+
56+
>>> from gcloud import pubsub
57+
>>> topics = pubsub.list_topics(project_id='my.project') # API request
58+
>>> [topic.name for topic in topics]
59+
['topic_name']
60+
61+
Delete a topic:
62+
63+
.. doctest::
64+
65+
>>> from gcloud.pubsub.topic import Topic
66+
>>> topic = Topic('topic_name')
67+
>>> topic.delete() # API request
68+
69+
70+
Publish messages to a topic
71+
---------------------------
72+
73+
Publish a single message to a topic, without attributes:
74+
75+
.. doctest::
76+
77+
>>> from gcloud.pubsub.topic import Topic
78+
>>> topic = Topic('topic_name')
79+
>>> topic.publish('this is the message_payload') # API request
80+
<message_id>
81+
82+
Publish a single message to a topic, with attributes:
83+
84+
.. doctest::
85+
86+
>>> from gcloud.pubsub.topic import Topic
87+
>>> topic = Topic('topic_name')
88+
>>> topic.publish('this is another message_payload',
89+
... attr1='value1', attr2='value2') # API request
90+
<message_id>
91+
92+
Publish a set of messages to a topic (as a single request):
93+
94+
.. doctest::
95+
96+
>>> from gcloud.pubsub.topic import Topic
97+
>>> topic = Topic('topic_name')
98+
>>> with topic.batch() as batch:
99+
... batch.publish('this is the first message_payload')
100+
... batch.publish('this is the second message_payload',
101+
... attr1='value1', attr2='value2')
102+
>>> list(batch)
103+
[<message_id1>, <message_id2>]
104+
105+
.. note::
106+
107+
The only API request happens during the ``__exit__()`` of the topic
108+
used as a context manager.
109+
110+
111+
Manage subscriptions to topics
112+
------------------------------
113+
114+
Create a new pull subscription for a topic:
115+
116+
.. doctest::
117+
118+
>>> from gcloud.pubsub.topic import Topic
119+
>>> from gcloud.pubsub.subscription import Subscription
120+
>>> topic = Topic('topic_name')
121+
>>> subscription = Subscription('subscription_name', topic)
122+
>>> subscription.create() # API request
123+
124+
Create a new pull subscription for a topic with a non-default ACK deadline:
125+
126+
.. doctest::
127+
128+
>>> from gcloud.pubsub.topic import Topic
129+
>>> from gcloud.pubsub.subscription import Subscription
130+
>>> topic = Topic('topic_name')
131+
>>> subscription = Subscription('subscription_name', ack_deadline=90)
132+
>>> subscription.create() # API request
133+
134+
Create a new push subscription for a topic:
135+
136+
.. doctest::
137+
138+
>>> ENDPOINT = 'https://example.com/hook'
139+
>>> from gcloud.pubsub.topic import Topic
140+
>>> from gcloud.pubsub.subscription import Subscription
141+
>>> topic = Topic('topic_name')
142+
>>> subscription = Subscription('subscription_name', push_endpoint=ENDPOINT)
143+
>>> subscription.create() # API request
144+
145+
Check for the existence of a subscription:
146+
147+
.. doctest::
148+
149+
>>> from gcloud.pubsub.topic import Topic
150+
>>> from gcloud.pubsub.subscription import Subscription
151+
>>> topic = Topic('topic_name')
152+
>>> subscription = Subscription('subscription_name', topic)
153+
>>> subscription.exists() # API request
154+
True
155+
156+
Convert a pull subscription to push:
157+
158+
.. doctest::
159+
160+
>>> ENDPOINT = 'https://example.com/hook'
161+
>>> from gcloud.pubsub.topic import Topic
162+
>>> from gcloud.pubsub.subscription import Subscription
163+
>>> topic = Topic('topic_name')
164+
>>> subscription = Subscription('subscription_name', topic)
165+
>>> subscription.modify_push_configuration(push_endpoint=ENDPOINT) # API request
166+
167+
Convert a push subscription to pull:
168+
169+
.. doctest::
170+
171+
>>> ENDPOINT = 'https://example.com/hook'
172+
>>> from gcloud.pubsub.topic import Topic
173+
>>> topic = Topic('topic_name')
174+
>>> subscription = Subscription('subscription_name', topic,
175+
... push_endpoint=ENDPOINT)
176+
>>> subscription.modify_push_configuration(push_endpoint=None) # API request
177+
178+
List subscriptions for a topic:
179+
180+
.. doctest::
181+
182+
>>> from gcloud.pubsub.topic import Topic
183+
>>> topic = Topic('topic_name')
184+
>>> subscriptions = topic.list_subscriptions() # API request
185+
>>> [subscription.name for subscription in subscriptions]
186+
['subscription_name']
187+
188+
Delete a subscription:
189+
190+
.. doctest::
191+
192+
>>> from gcloud.pubsub.topic import Topic
193+
>>> from gcloud.pubsub.subscription import Subscription
194+
>>> topic = Topic('topic_name')
195+
>>> subscription = Subscription('subscription_name', topic)
196+
>>> subscription.delete() # API request
197+
198+
199+
Pull messages from a subscription
200+
---------------------------------
201+
202+
Fetch pending messages for a pull subscription
203+
204+
.. note::
205+
206+
The messages will have been ACKed already.
207+
208+
.. doctest::
209+
210+
>>> from gcloud.pubsub.topic import Topic
211+
>>> from gcloud.pubsub.subscription import Subscription
212+
>>> topic = Topic('topic_name')
213+
>>> subscription = Subscription('subscription_name', topic)
214+
>>> with topic:
215+
... topic.publish('this is the first message_payload')
216+
... topic.publish('this is the second message_payload',
217+
... attr1='value1', attr2='value2')
218+
>>> messages = subscription.pull() # API request
219+
>>> [message.id for message in messages]
220+
[<message_id1>, <message_id2>]
221+
>>> [message.data for message in messages]
222+
['this is the first message_payload', 'this is the second message_payload']
223+
>>> [message.attrs for message in messages]
224+
[{}, {'attr1': 'value1', 'attr2': 'value2'}]
225+
226+
Fetch a limited number of pending messages for a pull subscription:
227+
228+
.. doctest::
229+
230+
>>> from gcloud.pubsub.topic import Topic
231+
>>> from gcloud.pubsub.subscription import Subscription
232+
>>> topic = Topic('topic_name')
233+
>>> subscription = Subscription('subscription_name', topic)
234+
>>> with topic:
235+
... topic.publish('this is the first message_payload')
236+
... topic.publish('this is the second message_payload',
237+
... attr1='value1', attr2='value2')
238+
>>> [message.id for message in subscription.pull(max_messages=1)]
239+
[<message_id1>]
240+
241+
Fetch messages for a pull subscription without blocking (none pending):
242+
243+
.. doctest::
244+
245+
>>> from gcloud.pubsub.topic import Topic
246+
>>> from gcloud.pubsub.subscription import Subscription
247+
>>> topic = Topic('topic_name')
248+
>>> subscription = Subscription('subscription_name', topic)
249+
>>> [message.id for message in subscription.pull(return_immediately=True)]
250+
[]
251+

gcloud/pubsub/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""GCloud Pubsub API wrapper."""

gcloud/pubsub/api.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
""" Define API functions (not bound to classes)."""
16+
17+
18+
def list_topics(page_size=None, page_token=None,
19+
project=None, connection=None):
20+
"""List topics for a given project.
21+
22+
:type page_size: int
23+
:param page_size: maximum number of topics to return, If not passed,
24+
defaults to a value set by the API.
25+
26+
:type page_token: string
27+
:param page_token: opaque marker for the next "page" of topics. If not
28+
passed, the API will return the first page of topics.
29+
30+
:type project: string
31+
:param project: project ID to query. If not passed, defaults to the
32+
project ID inferred from the environment.
33+
34+
:type connection: :class:`gcloud.pubsub.connection.Connection`
35+
:param connection: connection to use for the query. If not passed,
36+
defaults to the connection inferred from the
37+
environment.
38+
39+
:rtype: dict
40+
:returns: keys include ``topics`` (a list of topic mappings) and
41+
``nextPageToken`` (a string: if non-empty, indicates that
42+
more topics can be retrieved with another call (pass that
43+
value as ``page_token``).
44+
"""
45+
params = {}
46+
47+
if page_size is not None:
48+
params['pageSize'] = page_size
49+
50+
if page_token is not None:
51+
params['pageToken'] = page_token
52+
53+
path = '/projects/%s/topics' % project
54+
return connection.api_request(method='GET', path=path, query_params=params)

0 commit comments

Comments
 (0)