From ccfaf753985ab0dea30d3a3434c5b24c8395e800 Mon Sep 17 00:00:00 2001 From: Sergey Petrov Date: Mon, 7 May 2018 19:28:06 +0300 Subject: [PATCH] Add timestamp field for Unregistered exception Fix #61 --- apns2/client.py | 15 +++++++++++++-- apns2/errors.py | 5 +++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/apns2/client.py b/apns2/client.py index a44cbbf..08e5f3d 100644 --- a/apns2/client.py +++ b/apns2/client.py @@ -54,7 +54,11 @@ class APNsClient(object): stream_id = self.send_notification_async(token_hex, notification, topic, priority, expiration, collapse_id) result = self.get_notification_result(stream_id) if result != 'Success': - raise exception_class_for_reason(result) + if isinstance(result, tuple): + reason, info = result + raise exception_class_for_reason(reason)(info) + else: + raise exception_class_for_reason(result) def send_notification_async(self, token_hex, notification, topic=None, priority=NotificationPriority.Immediate, expiration=None, collapse_id=None): @@ -83,13 +87,20 @@ class APNsClient(object): return stream_id def get_notification_result(self, stream_id): + """ + Get result for specified stream + The function returns: 'Success' or 'failure reason' or ('Unregistered', timestamp) + """ with self._connection.get_response(stream_id) as response: if response.status == 200: return 'Success' else: raw_data = response.read().decode('utf-8') data = json.loads(raw_data) - return data['reason'] + if response.status == 410: + return data['reason'], data['timestamp'] + else: + return data['reason'] def send_notification_batch(self, notifications, topic=None, priority=NotificationPriority.Immediate, expiration=None, collapse_id=None): diff --git a/apns2/errors.py b/apns2/errors.py index f769660..c1e7034 100644 --- a/apns2/errors.py +++ b/apns2/errors.py @@ -128,6 +128,11 @@ class MethodNotAllowed(InternalException): class Unregistered(APNsException): """The device token is inactive for the specified topic.""" + def __init__(self, timestamp=None): + super().__init__() + + self.timestamp = timestamp + class PayloadTooLarge(BadPayloadException): """The message payload was too large. The maximum payload size is 4096 bytes."""