Skip to content

Commit

Permalink
JSON serializable PayloadAlert
Browse files Browse the repository at this point in the history
  • Loading branch information
moden-py committed Sep 12, 2018
1 parent 07e7659 commit d0a8c17
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 39 deletions.
58 changes: 20 additions & 38 deletions apns.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
py_modules = ['apns'],
scripts = ['apns-send'],
url = 'http://29.io/',
version = 'canwehatch.2.0.1',
version = 'canwehatch.2.0.2',
)
7 changes: 7 additions & 0 deletions tests.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from random import random

import hashlib
import json
import os
import time
import unittest
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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()

0 comments on commit d0a8c17

Please sign in to comment.