-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #800 from tseaver/744-pubsub-message_class
#744: Add 'pubsub.message.Message' class
- Loading branch information
Showing
9 changed files
with
328 additions
and
71 deletions.
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.