diff --git a/messagebird/base.py b/messagebird/base.py index d6990a2..e64707e 100644 --- a/messagebird/base.py +++ b/messagebird/base.py @@ -1,5 +1,8 @@ from datetime import datetime +import dateutil.parser + + class Base(object): def load(self, data): for name, value in list(data.items()): @@ -7,16 +10,8 @@ def load(self, data): setattr(self, name, value) return self - - @staticmethod - def strip_nanoseconds_from_date(value): - if str(value).find(".") != -1: - return value[:-11] + value[-1:] - - return value @staticmethod def value_to_time(value, format='%Y-%m-%dT%H:%M:%S+00:00'): if value is not None: - value = Base.strip_nanoseconds_from_date(value) - return datetime.strptime(value, format) \ No newline at end of file + return dateutil.parser.parse(value).replace(microsecond=0) diff --git a/setup.py b/setup.py index c1135ed..229a812 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def get_description(): url = 'https://github.com/messagebird/python-rest-api', download_url = 'https://github.com/messagebird/python-rest-api/tarball/1.4.1', keywords = ['messagebird', 'sms'], - install_requires = ['requests>=2.4.1'], + install_requires = ['requests>=2.4.1', 'python-dateutil>=2.6.0'], license = 'BSD-2-Clause', classifiers = [ 'Programming Language :: Python', diff --git a/tests/test_conversation.py b/tests/test_conversation.py index dc27e64..77e0224 100644 --- a/tests/test_conversation.py +++ b/tests/test_conversation.py @@ -1,5 +1,8 @@ import unittest from datetime import datetime + +from dateutil.tz import tzutc + from messagebird import Client try: @@ -31,8 +34,8 @@ def test_conversation_start(self): self.assertEqual('1234', msg.id) self.assertEqual(99999999999, msg.contact.msisdn) - self.assertEqual(datetime(2019, 4, 2, 8, 19, 37), msg.contact.createdDatetime) - self.assertEqual(datetime(2019, 4, 2, 8, 19, 38), msg.contact.updatedDatetime) + self.assertEqual(datetime(2019, 4, 2, 8, 19, 37, tzinfo=tzutc()), msg.contact.createdDatetime) + self.assertEqual(datetime(2019, 4, 2, 8, 19, 38, tzinfo=tzutc()), msg.contact.updatedDatetime) self.assertEqual('channel-name', msg.channels[0].name) def test_conversation_list(self): @@ -52,9 +55,9 @@ def test_conversation_read(self): http_client.request.assert_called_once_with('conversations/conversation-id', 'GET', None) self.assertEqual('57b96dbe0fda40f0a814f5e3268c30a9', conversation.id) - self.assertEqual(datetime(2019, 4, 2, 8, 54, 38), conversation.createdDatetime) - self.assertEqual(datetime(2019, 4, 2, 14, 24, 9), conversation.updatedDatetime) - self.assertEqual(datetime(2019, 4, 2, 14, 24), conversation.lastReceivedDatetime) + self.assertEqual(datetime(2019, 4, 2, 8, 54, 38, tzinfo=tzutc()), conversation.createdDatetime) + self.assertEqual(datetime(2019, 4, 2, 14, 24, 9, tzinfo=tzutc()), conversation.updatedDatetime) + self.assertEqual(datetime(2019, 4, 2, 14, 24, 9, tzinfo=tzutc()), conversation.lastReceivedDatetime) self.assertEqual('8846d44229094c20813cf9eea596e680', conversation.contact.id) self.assertEqual('c0dae31e440145e094c4708b7d908842', conversation.channels[0].id) self.assertEqual(2, conversation.messages.totalCount) diff --git a/tests/test_conversation_message.py b/tests/test_conversation_message.py index 4c6836b..908c094 100644 --- a/tests/test_conversation_message.py +++ b/tests/test_conversation_message.py @@ -1,5 +1,8 @@ import unittest from datetime import datetime + +from dateutil.tz import tzutc + from messagebird import Client try: @@ -45,7 +48,7 @@ def test_create_message(self): msg = Client('', http_client).conversation_create_message('conversation-id', data) - self.assertEqual(datetime(2019, 4, 2, 11, 57, 53), msg.updatedDatetime) - self.assertEqual(datetime(2019, 4, 2, 11, 57, 52), msg.createdDatetime) + self.assertEqual(datetime(2019, 4, 2, 11, 57, 53, tzinfo=tzutc()), msg.updatedDatetime) + self.assertEqual(datetime(2019, 4, 2, 11, 57, 52, tzinfo=tzutc()), msg.createdDatetime) http_client.request.assert_called_once_with('conversations/conversation-id/messages', 'POST', data) diff --git a/tests/test_conversation_webhook.py b/tests/test_conversation_webhook.py index d320c69..f329194 100644 --- a/tests/test_conversation_webhook.py +++ b/tests/test_conversation_webhook.py @@ -1,6 +1,9 @@ import unittest import json from datetime import datetime + +from dateutil.tz import tzutc + from messagebird import Client from messagebird.conversation_webhook import \ CONVERSATION_WEBHOOK_EVENT_CONVERSATION_CREATED, \ @@ -60,7 +63,7 @@ def test_conversation_webhook_read(self): web_hook = Client('', http_client).conversation_read_webhook('webhook-id') http_client.request.assert_called_once_with('webhooks/webhook-id', 'GET', None) - self.assertEqual(datetime(2019, 4, 3, 8, 41, 37), web_hook.createdDatetime) + self.assertEqual(datetime(2019, 4, 3, 8, 41, 37, tzinfo=tzutc()), web_hook.createdDatetime) self.assertEqual(None, web_hook.updatedDatetime) self.assertEqual(['conversation.created', 'conversation.updated'], web_hook.events) diff --git a/tests/test_voice_recording.py b/tests/test_voice_recording.py index ae7f125..d140b72 100644 --- a/tests/test_voice_recording.py +++ b/tests/test_voice_recording.py @@ -1,4 +1,7 @@ import unittest + +from dateutil.tz import tzutc + from messagebird import Client, ErrorException from datetime import datetime @@ -23,8 +26,8 @@ def test_voice_recording_view(self): self.assertEqual('12345678-9012-3456-7890-123456789012', voice_recording.id) self.assertEqual('done', voice_recording.status) self.assertEqual('wav', voice_recording.format) - self.assertEqual(datetime(2018, 1, 1, 0, 0, 1), voice_recording.createdAt) - self.assertEqual(datetime(2018, 1, 1, 0, 0, 5), voice_recording.updatedAt) + self.assertEqual(datetime(2018, 1, 1, 0, 0, 1, tzinfo=tzutc()), voice_recording.createdAt) + self.assertEqual(datetime(2018, 1, 1, 0, 0, 5, tzinfo=tzutc()), voice_recording.updatedAt) self.assertEqual(2, len(voice_recording._links)) self.assertIsInstance(str(voice_recording), str) @@ -47,8 +50,8 @@ def test_voice_recording_list(self): self.assertEqual(recording_specific['duration'], item.duration) self.assertEqual('done', item.status) self.assertEqual('wav', item.format) - self.assertEqual(datetime(recording_specific['year'], 1, 1, 0, 0, 1), item.createdAt) - self.assertEqual(datetime(recording_specific['year'], 1, 1, 0, 0, 5), item.updatedAt) + self.assertEqual(datetime(recording_specific['year'], 1, 1, 0, 0, 1, tzinfo=tzutc()), item.createdAt) + self.assertEqual(datetime(recording_specific['year'], 1, 1, 0, 0, 5, tzinfo=tzutc()), item.updatedAt) self.assertEqual(2, len(item._links)) self.assertIsInstance(str(voice_recordings), str)