Skip to content

Commit 90f802b

Browse files
committed
Usage docs for 'pubsub.client.Client'.
1 parent 47f0422 commit 90f802b

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

docs/pubsub-usage.rst

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,88 @@ Fetch messages for a pull subscription without blocking (none pending):
265265
>>> messages = [recv[1] for recv in received]
266266
>>> [message.id for message in messages]
267267
[]
268+
269+
Using Clients
270+
-------------
271+
272+
A :class:`gcloud.pubsub.client.Client` instance explicitly manages a
273+
:class:`gcloud.pubsub.connection.Connection` and an associated project
274+
ID. Applications can then use the APIs which might otherwise take a
275+
``connection`` or ``project`` parameter, knowing that the values configured
276+
in the client will be passed.
277+
278+
Create a client using the defaults from the environment:
279+
280+
.. doctest::
281+
282+
>>> from gcloud.pubsub.client import Client
283+
>>> client = Client()
284+
285+
Create a client using an explicit connection, but the default project:
286+
287+
.. doctest::
288+
289+
>>> from gcloud.pubsub.client import Client
290+
>>> from gcloud.pubsub.connection import Connection
291+
>>> connection = Connection.from_service_account_json('/path/to/creds.json')
292+
>>> client = Client(connection=connection)
293+
294+
Create a client using an explicit project ID, but the connetion inferred
295+
from the environment:
296+
297+
.. doctest::
298+
299+
>>> from gcloud.pubsub.client import Client
300+
>>> client = Client(project='your-project-id')
301+
302+
Listing topics using a client (note that the client's connection
303+
is used to make the request, and its project is passed as a parameter):
304+
305+
.. doctest::
306+
307+
>>> from gcloud.pubsub.client import Client
308+
>>> client = Client(project='your-project-id')
309+
>>> topics, next_page_token = client.list_topics() # API request
310+
311+
Listing subscriptions using a client (note that the client's connection
312+
is used to make the request, and its project is passed as a parameter):
313+
314+
.. doctest::
315+
316+
>>> from gcloud.pubsub.client import Client
317+
>>> client = Client(project='your-project-id')
318+
>>> topics, next_page_token = client.list_topics() # API request
319+
>>> subscription, next_page_tokens = list_subscriptions() # API request
320+
321+
Instantiate a topic using a client (note that the client's project is passed
322+
through to the topic constructor, and that the returned object is a proxy
323+
which ensures that an API requests made via the topic use the client's
324+
connection):
325+
326+
.. doctest::
327+
328+
>>> from gcloud.pubsub.client import Client
329+
>>> client = Client(project='your-project-id')
330+
>>> topic = client.topic('topic-name')
331+
>>> topic.exists() # API request
332+
False
333+
>>> topic.create() # API request
334+
>>> topic.exists() # API request
335+
True
336+
337+
Instantiate a subscription using a client (note that the client's project is
338+
passed through to the subscription constructor, and that the returned object
339+
is a proxy which ensures that an API requests made via the subscription use
340+
the client's connection):
341+
342+
.. doctest::
343+
344+
>>> from gcloud.pubsub.client import Client
345+
>>> client = Client(project='your-project-id')
346+
>>> topic = client.topic('topic-name')
347+
>>> subscription = topic.subscription('subscription-name')
348+
>>> subscription.exists() # API request
349+
False
350+
>>> subscription.create() # API request
351+
>>> subscription.exists() # API request
352+
True

0 commit comments

Comments
 (0)