Skip to content

Commit 29a6b86

Browse files
committed
feat: Allow empty payloads
closes #40
1 parent c80a56c commit 29a6b86

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ include *.md
22
include *.txt
33
include setup.*
44
include LICENSE
5-
recursive-include pywebpush
5+
recursive-include pywebpush *.py

circle.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# circle ci file
2+
machine:
3+
post:
4+
- pyenv global 2.7.13 3.5
5+
6+
dependencies:
7+
pre:
8+
- pip install -r test-requirements.txt
9+
10+
test:
11+
override:
12+
- nosetests -v pywebpush

pywebpush/__init__.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def encode(self, data):
152152
'body': encrypted,
153153
})
154154

155-
def send(self, data, headers=None, ttl=0, gcm_key=None, reg_id=None):
155+
def send(self, data=None, headers=None, ttl=0, gcm_key=None, reg_id=None):
156156
"""Encode and send the data to the Push Service.
157157
158158
:param data: A serialized block of data (see encode() ).
@@ -169,22 +169,25 @@ def send(self, data, headers=None, ttl=0, gcm_key=None, reg_id=None):
169169
# Encode the data.
170170
if headers is None:
171171
headers = dict()
172-
encoded = self.encode(data)
173-
# Append the p256dh to the end of any existing crypto-key
174-
headers = CaseInsensitiveDict(headers)
175-
crypto_key = headers.get("crypto-key", "")
176-
if crypto_key:
177-
# due to some confusion by a push service provider, we should
178-
# use ';' instead of ',' to append the headers.
179-
# see https://github.com/webpush-wg/webpush-encryption/issues/6
180-
crypto_key += ';'
181-
crypto_key += "keyid=p256dh;dh=" + encoded["crypto_key"].decode('utf8')
182-
headers.update({
183-
'crypto-key': crypto_key,
184-
'content-encoding': 'aesgcm',
185-
'encryption': "keyid=p256dh;salt=" +
186-
encoded['salt'].decode('utf8'),
187-
})
172+
encoded = None
173+
if data:
174+
encoded = self.encode(data)
175+
# Append the p256dh to the end of any existing crypto-key
176+
headers = CaseInsensitiveDict(headers)
177+
crypto_key = headers.get("crypto-key", "")
178+
if crypto_key:
179+
# due to some confusion by a push service provider, we should
180+
# use ';' instead of ',' to append the headers.
181+
# see https://github.com/webpush-wg/webpush-encryption/issues/6
182+
crypto_key += ';'
183+
crypto_key += (
184+
"keyid=p256dh;dh=" + encoded["crypto_key"].decode('utf8'))
185+
headers.update({
186+
'crypto-key': crypto_key,
187+
'content-encoding': 'aesgcm',
188+
'encryption': "keyid=p256dh;salt=" +
189+
encoded['salt'].decode('utf8'),
190+
})
188191
gcm_endpoint = 'https://android.googleapis.com/gcm/send'
189192
if self.subscription_info['endpoint'].startswith(gcm_endpoint):
190193
if not gcm_key:
@@ -194,13 +197,14 @@ def send(self, data, headers=None, ttl=0, gcm_key=None, reg_id=None):
194197
if not reg_id:
195198
reg_id = self.subscription_info['endpoint'].rsplit('/', 1)[-1]
196199
reg_ids.append(reg_id)
197-
data = dict()
198-
data['registration_ids'] = reg_ids
199-
data['raw_data'] = base64.b64encode(
200-
encoded.get('body')).decode('utf8')
201-
data['time_to_live'] = int(
200+
gcm_data = dict()
201+
gcm_data['registration_ids'] = reg_ids
202+
if data:
203+
gcm_data['raw_data'] = base64.b64encode(
204+
encoded.get('body')).decode('utf8')
205+
gcm_data['time_to_live'] = int(
202206
headers['ttl'] if 'ttl' in headers else ttl)
203-
encoded_data = json.dumps(data)
207+
encoded_data = json.dumps(gcm_data)
204208
headers.update({
205209
'Authorization': 'key='+gcm_key,
206210
'Content-Type': 'application/json',

0 commit comments

Comments
 (0)