Add error handling for sending notifications

This commit is contained in:
Sergey Petrov
2016-02-09 16:45:43 +03:00
parent 61f38d391e
commit 3f61fac159
3 changed files with 167 additions and 1 deletions
+9 -1
View File
@@ -1,9 +1,12 @@
from enum import Enum
from json import dumps
import json
from hyper import HTTP20Connection
from hyper.tls import init_context
from apns2.errors import exception_class_for_reason
class NotificationPriority(Enum):
Immediate = 10
@@ -26,4 +29,9 @@ class APNsClient(object):
headers['apns-topic'] = topic
url = '/3/device/{}'.format(token_hex)
self.__connection.request('POST', url, json_payload, headers)
stream_id = self.__connection.request('POST', url, json_payload, headers)
resp = self.__connection.get_response(stream_id)
if resp.status != 200:
raw_data = resp.read().decode('utf-8')
data = json.loads(raw_data)
raise exception_class_for_reason(data['reason'])