diff --git a/apns.py b/apns.py old mode 100644 new mode 100755 index 1226def..532053b --- a/apns.py +++ b/apns.py @@ -267,7 +267,7 @@ def write(self, string): return self._connection().write(string) -class PayloadAlert(object): +class PayloadAlert(dict): """ Payload for APNS alert. https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html @@ -282,46 +282,28 @@ def __init__(self, launch_image=None, title_loc_key=None, title_loc_args=None): - - self.body = body - self.title = title - self.subtitle = subtitle - self.action_loc_key = action_loc_key - self.loc_key = loc_key - self.loc_args = loc_args - self.launch_image = launch_image - self.title_loc_key = title_loc_key - self.title_loc_args = title_loc_args - - self._dict = { - 'body': self.body, - 'title': self.title, - 'subtitle': self.subtitle, - 'action-loc-key': self.action_loc_key, - 'loc-key': self.loc_key, - 'loc-args': self.loc_args, - 'launch-image': self.launch_image, - 'title-loc-key': self.title_loc_key, - 'title-loc-args': self.title_loc_args - } - - def dict(self): - cleared = { - key: value - for (key, value) - in self._dict.items() - if value is not None + dict_ = { + 'body': body, + 'title': title, + 'subtitle': subtitle, + 'action-loc-key': action_loc_key, + 'loc-key': loc_key, + 'loc-args': loc_args, + 'launch-image': launch_image, + 'title-loc-key': title_loc_key, + 'title-loc-args': title_loc_args } - return cleared - def __eq__(self, other): - return self.dict() == other.dict() + # init dictionary with non None items + super(PayloadAlert, self).__init__( + { + key: value for (key, value) + in dict_.items() if value is not None + } + ) - def __ne__(self, other): - return self.dict() != other.dict() - - def __repr__(self): - return 'PayloadAlert(**{})'.format(self.dict()) + def dict(self): + return self class PayloadTooLargeError(Exception): diff --git a/setup.py b/setup.py index 3ff86d5..644ff24 100644 --- a/setup.py +++ b/setup.py @@ -10,5 +10,5 @@ py_modules = ['apns'], scripts = ['apns-send'], url = 'http://29.io/', - version = 'canwehatch.2.0.1', + version = 'canwehatch.2.0.2', ) diff --git a/tests.py b/tests.py old mode 100644 new mode 100755 index 97ca644..6a6254c --- a/tests.py +++ b/tests.py @@ -5,6 +5,7 @@ from random import random import hashlib +import json import os import time import unittest @@ -130,6 +131,11 @@ def testPayloadAlert(self): self.assertTrue('body' not in d) self.assertEqual(d['loc-key'], 'wibble') + def testPayloadAlertJSONSerializable(self): + pa = PayloadAlert('foo', action_loc_key='bar', loc_key='wibble', + loc_args=['king', 'kong'], launch_image='wobble') + self.assertEqual(pa, json.loads(json.dumps(pa))) + def testPayload(self): # Payload with just alert p = Payload(alert=PayloadAlert('foo')) @@ -219,5 +225,6 @@ def testPayloadTooLargeError(self): self.assertRaises(PayloadTooLargeError, Payload, u'\u0100' * (int(max_raw_payload_bytes / 2) + 1)) + if __name__ == '__main__': unittest.main()