Skip to content

Commit b4ae3a7

Browse files
authored
Merge pull request #44 from alexmv/master
More complete types, custom error handler, and fix a deadlock
2 parents 10757c1 + f86b9f0 commit b4ae3a7

File tree

6 files changed

+113
-83
lines changed

6 files changed

+113
-83
lines changed

aioapns/client.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ssl import SSLContext
2-
from typing import Optional
2+
from typing import Awaitable, Callable, Optional
33

4+
from aioapns.common import NotificationRequest, NotificationResult
45
from aioapns.connection import (
56
APNsBaseConnectionPool,
67
APNsCertConnectionPool,
@@ -22,8 +23,14 @@ def __init__(
2223
use_sandbox: bool = False,
2324
no_cert_validation: bool = False,
2425
ssl_context: Optional[SSLContext] = None,
25-
):
26+
err_func: Optional[
27+
Callable[
28+
[NotificationRequest, NotificationResult], Awaitable[None]
29+
]
30+
] = None,
31+
) -> None:
2632
self.pool: APNsBaseConnectionPool
33+
self.err_func = err_func
2734
if client_cert is not None and key is not None:
2835
raise ValueError("cannot specify both client_cert and key")
2936
elif client_cert:
@@ -53,13 +60,18 @@ def __init__(
5360
"the key credentials"
5461
)
5562

56-
async def send_notification(self, request):
63+
async def send_notification(
64+
self, request: NotificationRequest
65+
) -> NotificationResult:
5766
response = await self.pool.send_notification(request)
5867
if not response.is_successful:
59-
logger.error(
60-
"Status of notification %s is %s (%s)",
61-
request.notification_id,
62-
response.status,
63-
response.description,
64-
)
68+
if self.err_func is not None:
69+
await self.err_func(request, response)
70+
else:
71+
logger.error(
72+
"Status of notification %s is %s (%s)",
73+
request.notification_id,
74+
response.status,
75+
response.description,
76+
)
6577
return response

aioapns/common.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
from enum import Enum
3+
from typing import Any, Dict, Optional
34
from uuid import uuid4
45

56
PRIORITY_NORMAL = "5"
@@ -29,14 +30,14 @@ class NotificationRequest:
2930

3031
def __init__(
3132
self,
32-
device_token,
33-
message,
34-
notification_id=None,
35-
time_to_live=None,
36-
priority=None,
37-
collapse_key=None,
38-
push_type=None,
39-
):
33+
device_token: str,
34+
message: Dict[str, Any],
35+
notification_id: Optional[str] = None,
36+
time_to_live: Optional[int] = None,
37+
priority: Optional[int] = None,
38+
collapse_key: Optional[str] = None,
39+
push_type: Optional[PushType] = None,
40+
) -> None:
4041
self.device_token = device_token
4142
self.message = message
4243
self.notification_id = notification_id or str(uuid4())
@@ -49,23 +50,30 @@ def __init__(
4950
class NotificationResult:
5051
__slots__ = ("notification_id", "status", "description")
5152

52-
def __init__(self, notification_id, status, description=None):
53+
def __init__(
54+
self,
55+
notification_id: str,
56+
status: str,
57+
description: Optional[str] = None,
58+
):
5359
self.notification_id = notification_id
5460
self.status = status
5561
self.description = description
5662

5763
@property
58-
def is_successful(self):
64+
def is_successful(self) -> bool:
5965
return self.status == APNS_RESPONSE_CODE.SUCCESS
6066

6167

6268
class DynamicBoundedSemaphore(asyncio.BoundedSemaphore):
69+
_bound_value: int
70+
6371
@property
64-
def bound(self):
72+
def bound(self) -> int:
6573
return self._bound_value
6674

6775
@bound.setter
68-
def bound(self, new_bound):
76+
def bound(self, new_bound: int) -> None:
6977
if new_bound > self._bound_value:
7078
if self._value > 0:
7179
self._value += new_bound - self._bound_value
@@ -76,13 +84,13 @@ def bound(self, new_bound):
7684
self._value -= self._bound_value - new_bound
7785
self._bound_value = new_bound
7886

79-
def release(self):
87+
def release(self) -> None:
8088
self._value += 1
8189
if self._value > self._bound_value:
8290
self._value = self._bound_value
8391
self._wake_up_next()
8492

85-
def destroy(self, exc):
93+
def destroy(self, exc: Exception) -> None:
8694
while self._waiters:
8795
waiter = self._waiters.popleft()
8896
if not waiter.done():

0 commit comments

Comments
 (0)