8000 First pass at stubbing out gcloud.pubsub. · googleapis/google-cloud-python@89f0d7e · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 89f0d7e

Browse files
committed
First pass at stubbing out gcloud.pubsub.
1 parent 55adeb3 commit 89f0d7e

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

gcloud/pubsub/__init__.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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)

gcloud/pubsub/connection.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from gcloud import connection
2+
3+
4+
class Connection(connection.JsonConnection):
5+
""""""
6+
7+
API_VERSION = 'v1beta1'
8+
""""""
9+
10+
API_URL_TEMPLATE = '{api_base}/pubsub/{api_version}'
11+
""""""
12+
13+
@classmethod
14+
def build_api_url(cls, resource_type, resource_id=None, method=None, base_url=None,
15+
api_version-None):
16+
""""""
17+
18+
api_url_base = cls.API_URL_TEMPLATE.format(
19+
api_base=(base_url or cls.API_BASE_URL),
20+
api_version=(api_version or cls.API_VERSION),
21+
resouce_type=resource_type, resource_id=resource_id,
22+
method=method)
23+
24+
# TODO: Do some error checking and throw a ValueError if the
25+
# parameters are invalid.
26+
27+
pieces = list(filter(None, resource_type, resource_id, method))
28+
return '/'.join([api_url_base] + pieces)
29+
30+
31+
def create_topic(self, name):
32+
pass
33+
34+
def delete_topic(self, name):
35+
pass
36+
37+
def get_topic(self, name):
38+
pass
39+
40+
def get_topics(self):
41+
pass
42+
43+
def create_subscription(self, topic_name, name, push_endpoint=None, ack_deadline=None):
44+
pass
45+
46+
def delete_subscription(self, name):
47+
pass
48+
49+
def get_subscription(self, name):
50+
pass
51+
52+
def get_subscriptions(self, query):
53+
pass
54+
55+
def publish_message(self, topic_name, message, labels=None):
56+
pass
57+
58+
def get_message(self, subscriptio 7802 n_name):
59+
pass
60+
61+
# TODO: Figure out how we're going to handle async subscriptions...
62+
# asyncio.Future (Python 3)? multiprocessing.Pool (Python 2)?

gcloud/pubsub/subscription.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Subscription(object):
2+
3+
def __init__(self, connection=None, topic=None, name=None):
4+
self.connection = connection
5+
self.topic = topic
6+
self.name = name
7+
8+
@classmethod
9+
def from_dict(cls, subscription_dict, connection=None):
10+
return cls(connection=connection, topic=subscription_dict['topic'],
11+
name=subscription_dict['name'])
12+
13+
def __repr__(self): # pragma NO COVER
14+
topic_name = self.topic.name if self.topic else None
15+
return '<Subscription: %s to topic %s>' % (self.name, topic_name)
16+
17+
def delete(self):
18+
pass
19+
20+
def get_message(self):
21+
return self.connection.get_message(self.name)

gcloud/pubsub/topic.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Topic(object):
2+
3+
def __init__(self, connection=None, name=None):
4+
self.connection = connection
5+
self.name = name
6+
7+
@classmethod
8+
def from_dict(cls, topic_dict, connection=None):
9+
return cls(connection=connection, name=topic_dict['name'])
10+
11+
def __repr__(self): # pragma NO COVER
12+
return '<Topic: %s>' % self.name
13+
14+
def delete(self):
15+
pass
16+
17+
def subscribe(self, name, *args, **kwargs):
18+
return self.connection.create_subscription(topic_name=self.name,
19+
name=name, *args, **kwargs)
20+
21+
def publish(self, message, labels=None):
22+
return self.connection.publish_message(topic_name=self.name,
23+
message=message, labels=labels)

0 commit comments

Comments
 (0)
0