Skip to content

Commit

Permalink
Merge pull request #1627 from tseaver/1623-expose-message-publish_tim…
Browse files Browse the repository at this point in the history
…estamp

Add 'Message.service_timestamp' property.
  • Loading branch information
tseaver committed Mar 18, 2016
2 parents 97f2e23 + f429221 commit 72d0bba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
18 changes: 16 additions & 2 deletions gcloud/pubsub/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Message(object):
:param attributes: Extra metadata associated by the publisher with the
message.
"""
_service_timesstamp = None

def __init__(self, data, message_id, attributes=None):
self.data = data
self.message_id = message_id
Expand Down Expand Up @@ -64,6 +66,15 @@ def timestamp(self):
raise ValueError('No timestamp')
return _rfc3339_to_datetime(stamp)

@property
def service_timestamp(self):
"""Return server-set timestamp.
:rtype: string
:returns: timestamp (in UTC timezone) in RFC 3339 format
"""
return self._service_timesstamp

@classmethod
def from_api_repr(cls, api_repr):
"""Factory: construct message from API representation.
Expand All @@ -72,5 +83,8 @@ def from_api_repr(cls, api_repr):
:param api_repr: The API representation of the message
"""
data = base64.b64decode(api_repr.get('data', b''))
return cls(data=data, message_id=api_repr['messageId'],
attributes=api_repr.get('attributes'))
instance = cls(
data=data, message_id=api_repr['messageId'],
attributes=api_repr.get('attributes'))
instance._service_timesstamp = api_repr.get('publishTimestamp')
return instance
22 changes: 18 additions & 4 deletions gcloud/pubsub/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_ctor_no_attributes(self):
self.assertEqual(message.data, DATA)
self.assertEqual(message.message_id, MESSAGE_ID)
self.assertEqual(message.attributes, {})
self.assertEqual(message.service_timestamp, None)

def test_ctor_w_attributes(self):
DATA = b'DEADBEEF'
Expand All @@ -41,6 +42,7 @@ def test_ctor_w_attributes(self):
self.assertEqual(message.data, DATA)
self.assertEqual(message.message_id, MESSAGE_ID)
self.assertEqual(message.attributes, ATTRS)
self.assertEqual(message.service_timestamp, None)

def test_timestamp_no_attributes(self):
DATA = b'DEADBEEF'
Expand Down Expand Up @@ -85,28 +87,40 @@ def test_from_api_repr_missing_data(self):
self.assertEqual(message.data, b'')
self.assertEqual(message.message_id, MESSAGE_ID)
self.assertEqual(message.attributes, {})
self.assertEqual(message.service_timestamp, None)

def test_from_api_repr_no_attributes(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}
TIMESTAMP = '2016-03-18-19:38:22.001393427Z'
api_repr = {
'data': B64_DATA,
'messageId': MESSAGE_ID,
'publishTimestamp': TIMESTAMP,
}
message = self._getTargetClass().from_api_repr(api_repr)
self.assertEqual(message.data, DATA)
self.assertEqual(message.message_id, MESSAGE_ID)
self.assertEqual(message.attributes, {})
self.assertEqual(message.service_timestamp, TIMESTAMP)

def test_from_api_repr_w_attributes(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}
TIMESTAMP = '2016-03-18-19:38:22.001393427Z'
api_repr = {
'data': B64_DATA,
'messageId': MESSAGE_ID,
'publishTimestamp': TIMESTAMP,
'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.service_timestamp, TIMESTAMP)
self.assertEqual(message.attributes, ATTRS)

0 comments on commit 72d0bba

Please sign in to comment.