-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Flesh out pubsub subscriptions #743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tseaver
merged 8 commits into
googleapis:master
from
tseaver:691-flesh_out_pubsub_subscription
Mar 19, 2015
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7799622
Add 'pubsub.subscription.Subscription' class.
tseaver 42b0dc3
Add 'pubsub.api.list_subscriptions'.
tseaver db75350
Link to API docs for 'list_topics'/'list_subscriptions'.
tseaver bdcc60b
Spell out ACK.
tseaver e1b581d
Document param to 'modify_push_configuration'.
tseaver 6a9eb19
Fix docstring for 'Subscription.refresh'.
tseaver ea1625c
Fix docstring for 'Subscription.modify_ack_deadline'.
tseaver 7821d88
Docstring typo fixes per @tmatsuo.
tseaver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" Define API Subscriptions.""" | ||
|
||
from gcloud.exceptions import NotFound | ||
|
||
|
||
class Subscription(object): | ||
"""Subscriptions receive messages published to their topics. | ||
|
||
See: | ||
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions | ||
|
||
:type name: string | ||
:param name: the name of the subscription | ||
|
||
:type topic: :class:`gcloud.pubsub.topic.Topic` | ||
:param topic: the topic to which the subscription belongs.. | ||
|
||
: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. | ||
""" | ||
def __init__(self, name, topic, ack_deadline=None, push_endpoint=None): | ||
self.name = name | ||
self.topic = topic | ||
self.ack_deadline = ack_deadline | ||
self.push_endpoint = push_endpoint | ||
|
||
@property | ||
def path(self): | ||
"""URL path for the subscription's APIs""" | ||
project = self.topic.project | ||
return '/projects/%s/subscriptions/%s' % (project, self.name) | ||
|
||
def create(self): | ||
"""API call: create the subscription via a PUT request | ||
|
||
See: | ||
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/create | ||
""" | ||
data = {'topic': self.topic.path} | ||
|
||
if self.ack_deadline is not None: | ||
data['ackDeadline'] = self.ack_deadline | ||
|
||
if self.push_endpoint is not None: | ||
data['pushConfig'] = {'pushEndpoint': self.push_endpoint} | ||
|
||
conn = self.topic.connection | ||
conn.api_request(method='PUT', path=self.path, data=data) | ||
|
||
def exists(self): | ||
"""API call: test existence of the subscription via a GET request | ||
|
||
See | ||
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/get | ||
""" | ||
conn = self.topic.connection | ||
try: | ||
conn.api_request(method='GET', | ||
path=self.path, | ||
query_params={'fields': 'name'}) | ||
except NotFound: | ||
return False | ||
else: | ||
return True | ||
|
||
def reload(self): | ||
"""API call: sync local subscription configuration via a GET request | ||
|
||
See | ||
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/get | ||
""" | ||
conn = self.topic.connection | ||
data = conn.api_request(method='GET', path=self.path) | ||
self.ack_deadline = data.get('ackDeadline') | ||
push_config = data.get('pushConfig', {}) | ||
self.push_endpoint = push_config.get('pushEndpoint') | ||
|
||
def modify_push_configuration(self, push_endpoint): | ||
"""API call: update the push endpoint for the subscription. | ||
|
||
See: | ||
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/modifyPushConfig | ||
|
||
:type push_endpoint: string | ||
:param push_endpoint: URL to which messages will be pushed by the | ||
back-end. If None, the application must pull | ||
messages. | ||
""" | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
data = {} | ||
config = data['pushConfig'] = {} | ||
if push_endpoint is not None: | ||
config['pushEndpoint'] = push_endpoint | ||
conn = self.topic.connection | ||
conn.api_request(method='POST', | ||
path='%s:modifyPushConfig' % self.path, | ||
data=data) | ||
self.push_endpoint = push_endpoint | ||
|
||
def pull(self, return_immediately=False, max_messages=1): | ||
"""API call: retrieve messages for the subscription. | ||
|
||
See: | ||
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/pull | ||
|
||
:type return_immediately: boolean | ||
:param return_immediately: if True, the back-end returns even if no | ||
messages are available; if False, the API | ||
call blocks until one or more messages are | ||
available. | ||
|
||
:type max_messages: int | ||
:param max_messages: the maximum number of messages to return. | ||
|
||
:rtype: list of dict | ||
:returns: sequence of mappings, each containing keys ``ackId`` (the | ||
ID to be used in a subsequent call to :meth:`acknowledge`) | ||
and ``message``. | ||
""" | ||
data = {'returnImmediately': return_immediately, | ||
'maxMessages': max_messages} | ||
conn = self.topic.connection | ||
response = conn.api_request(method='POST', | ||
path='%s:pull' % self.path, | ||
data=data) | ||
return response['receivedMessages'] | ||
|
||
def acknowledge(self, ack_ids): | ||
"""API call: acknowledge retrieved messages for the subscription. | ||
|
||
See: | ||
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/acknowledge | ||
|
||
:type ack_ids: list of string | ||
:param ack_ids: ack IDs of messages being acknowledged | ||
""" | ||
data = {'ackIds': ack_ids} | ||
conn = self.topic.connection | ||
conn.api_request(method='POST', | ||
path='%s:acknowledge' % self.path, | ||
data=data) | ||
|
||
def modify_ack_deadline(self, ack_id, ack_deadline): | ||
"""API call: update acknowledgement deadline for a retrieved message. | ||
|
||
See: | ||
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/acknowledge | ||
|
||
:type ack_id: string | ||
:param ack_id: ack ID of message being updated | ||
|
||
:type ack_deadline: int | ||
:param ack_deadline: new deadline for the message, in seconds | ||
""" | ||
data = {'ackId': ack_id, 'ackDeadlineSeconds': ack_deadline} | ||
conn = self.topic.connection | ||
conn.api_request(method='POST', | ||
path='%s:modifyAckDeadline' % self.path, | ||
data=data) | ||
|
||
def delete(self): | ||
"""API call: delete the subscription via a DELETE request. | ||
|
||
See: | ||
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/delete | ||
""" | ||
conn = self.topic.connection | ||
conn.api_request(method='DELETE', path=self.path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.