3
3
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
4
5
5
import base64
6
- import os
7
-
8
6
import json
7
+ import os
9
8
9
+ import six
10
10
import http_ece
11
11
import pyelliptic
12
12
import requests
@@ -42,6 +42,10 @@ def get(self, key, default=None):
42
42
except KeyError :
43
43
return default
44
44
45
+ def update (self , data ):
46
+ for key in data :
47
+ self .__setitem__ (key , data [key ])
48
+
45
49
46
50
class WebPusher :
47
51
"""WebPusher encrypts a data block using HTTP Encrypted Content Encoding
@@ -88,11 +92,6 @@ def __init__(self, subscription_info):
88
92
the client.
89
93
90
94
"""
91
- # Python 2 v. 3 hack
92
- try :
93
- self .basestr = basestring
94
- except NameError :
95
- self .basestr = str
96
95
if 'endpoint' not in subscription_info :
97
96
raise WebPushException ("subscription_info missing endpoint URL" )
98
97
if 'keys' not in subscription_info :
@@ -102,7 +101,7 @@ def __init__(self, subscription_info):
102
101
for k in ['p256dh' , 'auth' ]:
103
102
if keys .get (k ) is None :
104
103
raise WebPushException ("Missing keys value: %s" , k )
105
- if isinstance (keys [k ], self . basestr ):
104
+ if isinstance (keys [k ], six . string_types ):
106
105
keys [k ] = bytes (keys [k ].encode ('utf8' ))
107
106
receiver_raw = base64 .urlsafe_b64decode (self ._repad (keys ['p256dh' ]))
108
107
if len (receiver_raw ) != 65 and receiver_raw [0 ] != "\x04 " :
@@ -131,7 +130,7 @@ def encode(self, data):
131
130
# ID tag.
132
131
server_key_id = base64 .urlsafe_b64encode (server_key .get_pubkey ()[1 :])
133
132
134
- if isinstance (data , self . basestr ):
133
+ if isinstance (data , six . string_types ):
135
134
data = bytes (data .encode ('utf8' ))
136
135
137
136
# http_ece requires that these both be set BEFORE encrypt or
@@ -183,7 +182,6 @@ def send(self, data, headers={}, ttl=0, gcm_key=None, reg_id=None):
183
182
})
184
183
gcm_endpoint = 'https://android.googleapis.com/gcm/send'
185
184
if self .subscription_info ['endpoint' ].startswith (gcm_endpoint ):
186
-
187
185
if not gcm_key :
188
186
raise WebPushException ("API key not provided for gcm endpoint" )
189
187
endpoint = gcm_endpoint
@@ -193,7 +191,8 @@ def send(self, data, headers={}, ttl=0, gcm_key=None, reg_id=None):
193
191
reg_ids .append (reg_id )
194
192
data = {}
195
193
data ['registration_ids' ] = reg_ids
196
- data ['raw_data' ] = base64 .b64encode (encoded .get ('body' ))
194
+ data ['raw_data' ] = base64 .b64encode (
195
+ encoded .get ('body' )).decode ('utf8' )
197
196
encoded_data = json .dumps (data )
198
197
headers .update ({
199
198
'Authorization' : 'key=' + gcm_key ,
0 commit comments