From 29f818cf433d2218683bbad069d0609c30eb1e17 Mon Sep 17 00:00:00 2001 From: Sergey Petrov Date: Mon, 1 Feb 2016 13:01:56 +0300 Subject: [PATCH] Add initial implementation for sending notifications --- apns2/client.py | 29 +++++++++++++++++++++++++++++ apns2/client.pyi | 15 +++++++++++++++ setup.py | 1 + 3 files changed, 45 insertions(+) create mode 100644 apns2/client.py create mode 100644 apns2/client.pyi diff --git a/apns2/client.py b/apns2/client.py new file mode 100644 index 0000000..6501d98 --- /dev/null +++ b/apns2/client.py @@ -0,0 +1,29 @@ +from enum import Enum +from json import dumps + +from hyper import HTTP20Connection +from hyper.tls import init_context + + +class NotificationPriority(Enum): + Immediate = 10 + Delayed = 5 + + +class APNsClient(object): + def __init__(self, cert_file, use_sandbox=False, use_alternate_port=False): + server = 'api.development.push.apple.com' if use_sandbox else 'api.push.apple.com' + port = 2197 if use_alternate_port else 443 + ssl_context = init_context() + ssl_context.load_cert_chain(cert_file) + self.__connection = HTTP20Connection(server, port, ssl_context=ssl_context) + + def send_notification(self, token_hex, notification, topic=None): + json_payload = dumps(notification.dict(), ensure_ascii=False, separators=(',',':')).encode('utf-8') + headers = {} + + if topic: + headers['apns-topic'] = topic + + url = '/3/device/{}'.format(token_hex) + self.__connection.request('POST', url, json_payload, headers) diff --git a/apns2/client.pyi b/apns2/client.pyi new file mode 100644 index 0000000..13c706a --- /dev/null +++ b/apns2/client.pyi @@ -0,0 +1,15 @@ +from typing import Optional + +from apns2.payload import Payload + + +class APNsClient(object): + def __init__(self, + cert_file: str, + use_sandbox: bool = False, + use_alternate_port: bool = False) -> None: ... + + def send_notification(self, + token_hex: str, + notification: Payload, + topic: Optional[str] = None) -> None: ... diff --git a/setup.py b/setup.py index b8ed968..8305e88 100644 --- a/setup.py +++ b/setup.py @@ -4,6 +4,7 @@ setup( name='apns2', version='0.1.0', packages=['apns2'], + requires=['hyper'], url='', license='MIT', author='Petrov Sergey',