|
| 1 | +"""Shortcut methods for getting set up with Google Cloud Pub/Sub. |
| 2 | +
|
| 3 | +You'll typically use these to get started with the API: |
| 4 | +
|
| 5 | +>>> from gcloud import pubsub |
| 6 | +>>> connection = pubsub.get_connection('long-email@googleapis.com', |
| 7 | +... '/path/to/private.key') |
| 8 | +>>> # Then do other things... |
| 9 | +>>> topic = connection.create_topic('topic-name-here') |
| 10 | +>>> topic.publish_message('My message', labels=['label1', 1234, 'label2'] |
| 11 | +
|
| 12 | +The main concepts with this API are: |
| 13 | +
|
| 14 | +- :class:`gcloud.pubsub.connection.Connection` |
| 15 | + which represents a connection between your machine and Cloud Pub/Sub. |
| 16 | +
|
| 17 | +- :class:`gcloud.pubsub.topic.Topic` |
| 18 | + which represents a particular topic. |
| 19 | +
|
| 20 | +- :class:`gcloud.pubsub.subscription.Subscription` |
| 21 | + which represents a subscription to a topic. |
| 22 | +
|
| 23 | +- :class:`gcloud.pubsub.message.Message` |
| 24 | + which represents a message pulled from a Subscription. |
| 25 | +""" |
| 26 | + |
| 27 | +__version__ = '0.0.1' |
| 28 | + |
| 29 | +SCOPE = ('https://www.googleapis.com/auth/pubsub', |
| 30 | + 'https://www.googleapis.com/auth/cloud-platform') |
| 31 | +"""The scope required for authenticating as a Cloud Pub/Sub consumer.""" |
| 32 | + |
| 33 | + |
| 34 | +def get_connection(client_email, private_key_path): |
| 35 | + """Shortcut method to establish a connection to Cloud Pub/Sub. |
| 36 | +
|
| 37 | + Use this to quickly establish a connection to the Pub/Sub API. |
| 38 | +
|
| 39 | + >>> from gcloud import pubsub |
| 40 | + >>> connection = pubsub.get_connection(email, key_path) |
| 41 | + >>> topic = connection.get_topic('topic-name') |
| 42 | +
|
| 43 | + :type client_email: string |
| 44 | + :param client_email: The e-mail attached to the service account. |
| 45 | +
|
| 46 | + :type private_key_path: string |
| 47 | + :param private_key_path: The path to a private key file (this file was |
| 48 | + given to you when you created the service |
| 49 | + account). |
| 50 | +
|
| 51 | + :rtype: :class:`gcloud.pubsub.connection.Connection` |
| 52 | + :returns: A connection defined with the proper credentials. |
| 53 | + """ |
| 54 | + from gcloud.credentials import Credentials |
| 55 | + from gcloud.pubsub.connection import Connection |
| 56 | + |
| 57 | + credentials = Credentials.get_for_service_account( |
| 58 | + client_email, private_key_path, scope=SCOPE) |
| 59 | + return Connection(credentials=credentials) |
0 commit comments