-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
#744: Add 'pubsub.message.Message' class #800
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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,56 @@ | ||
# 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 Topics.""" | ||
|
||
import base64 | ||
|
||
|
||
class Message(object): | ||
"""Messages can be published to a topic and received by subscribers. | ||
|
||
See: | ||
https://cloud.google.com/pubsub/reference/rest/google/pubsub/v1beta2/PubsubMessage | ||
|
||
:type name: bytes | ||
:param name: the payload of the message | ||
|
||
:type message_id: string | ||
:param message_id: An ID assigned to the message by the API. | ||
|
||
:type attrs: dict or None | ||
:param attrs: Extra metadata associated by the publisher with the message. | ||
""" | ||
def __init__(self, data, message_id, attributes=None): | ||
self.data = data | ||
self.message_id = message_id | ||
self._attrs = attributes | ||
|
||
@property | ||
def attrs(self): | ||
"""Lazily-constructed attribute dictionary""" | ||
if self._attrs is None: | ||
self._attrs = {} | ||
return self._attrs | ||
|
||
@classmethod | ||
def from_api_repr(cls, api_repr): | ||
"""Factory: construct message from API representation. | ||
|
||
:type api_repr: dict or None | ||
:param api_repr: The API representation of the message | ||
""" | ||
data = base64.b64decode(api_repr['data']) | ||
return cls(data=data, message_id=api_repr['messageId'], | ||
attributes=api_repr.get('attributes')) |
This file contains 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 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,68 @@ | ||
# 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. | ||
|
||
import unittest2 | ||
|
||
|
||
class TestMessage(unittest2.TestCase): | ||
|
||
def _getTargetClass(self): | ||
from gcloud.pubsub.message import Message | ||
return Message | ||
|
||
def _makeOne(self, *args, **kw): | ||
return self._getTargetClass()(*args, **kw) | ||
|
||
def test_ctor_no_attrs(self): | ||
DATA = b'DEADBEEF' | ||
MESSAGE_ID = b'12345' | ||
message = self._makeOne(data=DATA, message_id=MESSAGE_ID) | ||
self.assertEqual(message.data, DATA) | ||
self.assertEqual(message.message_id, MESSAGE_ID) | ||
self.assertEqual(message.attrs, {}) | ||
|
||
def test_ctor_w_attrs(self): | ||
DATA = b'DEADBEEF' | ||
MESSAGE_ID = b'12345' | ||
ATTRS = {'a': 'b'} | ||
message = self._makeOne(data=DATA, message_id=MESSAGE_ID, | ||
attributes=ATTRS) | ||
self.assertEqual(message.data, DATA) | ||
self.assertEqual(message.message_id, MESSAGE_ID) | ||
self.assertEqual(message.attrs, ATTRS) | ||
|
||
def test_from_api_repr_no_attrs(self): | ||
from base64 import b64encode as b64 | ||
DATA = b'DEADBEEF' | ||
B64_DATA = b64(DATA) | ||
MESSAGE_ID = '12345' | ||
api_repr = {'data': B64_DATA, 'messageId': MESSAGE_ID} | ||
message = self._getTargetClass().from_api_repr(api_repr) | ||
self.assertEqual(message.data, DATA) | ||
self.assertEqual(message.message_id, MESSAGE_ID) | ||
self.assertEqual(message.attrs, {}) | ||
|
||
def test_from_api_repr_w_attrs(self): | ||
from base64 import b64encode as b64 | ||
DATA = b'DEADBEEF' | ||
B64_DATA = b64(DATA) | ||
MESSAGE_ID = '12345' | ||
ATTRS = {'a': 'b'} | ||
api_repr = {'data': B64_DATA, | ||
'messageId': MESSAGE_ID, | ||
'attributes': ATTRS} | ||
message = self._getTargetClass().from_api_repr(api_repr) | ||
self.assertEqual(message.data, DATA) | ||
self.assertEqual(message.message_id, MESSAGE_ID) | ||
self.assertEqual(message.attrs, ATTRS) |
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.
This comment was marked as spam.
Sorry, something went wrong.