django-webpush
django-webpush copied to clipboard
Example for sending notification throws error.
TypeError: unhashable type is now thrown when attempting to send a notifcation.
I am seeing the error attempt to send a notification using the following code:
from webpush import models, utils
sub = subscription=models.SubscriptionInfo.objects.get(browser='chrome')
push = models.PushInformation.objects.get(subscription=sub); utils._send_notification(push, {"header": "what", "body": "up"}, 100)
error:
Traceback (most recent call last):
File "", line 1, in
File "/home/jscarlett/venv/adm/local/lib/python2.7/site-packages/webpush/utils.py", line 30, in send_notification
req = WebPusher(subscription_data).send(data=payload, ttl=ttl, gcm_key=gcm_key)
File "/home/jscarlett/venv/adm/local/lib/python2.7/site-packages/pywebpush/__init_.py", line 175, in send
encoded = self.encode(data)
File "/home/jscarlett/venv/adm/local/lib/python2.7/site-packages/pywebpush/init.py", line 147, in encode
authSecret=self.auth_key)
File "/home/jscarlett/venv/adm/local/lib/python2.7/site-packages/http_ece/init.py", line 152, in encrypt
result += encryptRecord(key_, nonce_, counter, buffer[i:i+rs])
TypeError: unhashable type
I was able to fix it by adding this check to the encode method of pywebpush: https://gist.github.com/jamaalscarlett/b791886a206b0fb3ad1c348877298509#file-init-py-L23 but I think the validation should probably be done in this library.
@safwanrahman shouldn't the json.dumps(payload) be in the _send_notifications? Because the method will throw an error if you attempt to pass in a dict, I think the json.dumps() call should be moved, or at least comment the method to explain what type it is expecting.
I think _send_notifications
is an internal API. therefore from the user end, it should not be used.
Yap. Comment should be added what type its expecting. But it will be not a good idea to use it from outside.
shouldn't the json.dumps(payload) be in the _send_notifications?
I think it should not be there. Can you please explain why it should be there?
@safwanrahman what other types does payload allow? at the very least it should be something like:
if type(payload) is dict:
payload = json.dumps(payload)
or like I said put a comment in there enumerating the accepted data types. Someone could extend this package, for example, to allow subscriptions without authentication. It is not currently obvious that you need to call json.dumps on the payload to avoid the type error
@jamaalscarlett Actually, according to specification, payload only allows text. we make the dictionary to json so we can send the text to browser then parse the json from the text.
I think, a comment should be added there about the data types