Skip to content

Commit

Permalink
refactor, make hosts and ports configurable via constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobcr committed Oct 18, 2012
1 parent 0f2f5d2 commit 02c4bb1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
42 changes: 24 additions & 18 deletions apns.py
Expand Up @@ -43,15 +43,25 @@
class APNs(object): class APNs(object):
"""A class representing an Apple Push Notification service connection""" """A class representing an Apple Push Notification service connection"""


def __init__(self, use_sandbox=False, cert_file=None, key_file=None): feedback = ('feedback.push.apple.com', 'feedback.sandbox.push.apple.com')
gateway = ('gateway.push.apple.com', 'gateway.sandbox.push.apple.com')

def __init__(self, use_sandbox=False, cert_file=None, key_file=None,
host=None, port=2195,
feedback_host=None, feedback_port=2196):
""" """
Set use_sandbox to True to use the sandbox (test) APNs servers. Set use_sandbox to True to use the sandbox (test) APNs servers.
Default is False. Default is False.
""" """
super(APNs, self).__init__()
self.use_sandbox = use_sandbox
self.cert_file = cert_file self.cert_file = cert_file
self.key_file = key_file self.key_file = key_file

self.host = host if host else self.gateway[use_sandbox]
self.port = port

self.feedback_host = feedback_host if feedback_host else self.feedback[use_sandbox]
self.feedback_port = feedback_port

self._feedback_connection = None self._feedback_connection = None
self._gateway_connection = None self._gateway_connection = None


Expand Down Expand Up @@ -87,8 +97,10 @@ def unpacked_uint_big_endian(bytes):
@property @property
def feedback_server(self): def feedback_server(self):
if not self._feedback_connection: if not self._feedback_connection:

self._feedback_connection = FeedbackConnection( self._feedback_connection = FeedbackConnection(
use_sandbox = self.use_sandbox, host = self.feedback_host,
port = self.feedback_port,
cert_file = self.cert_file, cert_file = self.cert_file,
key_file = self.key_file key_file = self.key_file
) )
Expand All @@ -98,7 +110,8 @@ def feedback_server(self):
def gateway_server(self): def gateway_server(self):
if not self._gateway_connection: if not self._gateway_connection:
self._gateway_connection = GatewayConnection( self._gateway_connection = GatewayConnection(
use_sandbox = self.use_sandbox, host = self.host,
port = self.port,
cert_file = self.cert_file, cert_file = self.cert_file,
key_file = self.key_file key_file = self.key_file
) )
Expand All @@ -110,7 +123,6 @@ class APNsConnection(object):
A generic connection class for communicating with the APNs A generic connection class for communicating with the APNs
""" """
def __init__(self, cert_file=None, key_file=None): def __init__(self, cert_file=None, key_file=None):
super(APNsConnection, self).__init__()
self.cert_file = cert_file self.cert_file = cert_file
self.key_file = key_file self.key_file = key_file
self._socket = None self._socket = None
Expand Down Expand Up @@ -144,7 +156,6 @@ def write(self, string):
class PayloadAlert(object): class PayloadAlert(object):
def __init__(self, body, action_loc_key=None, loc_key=None, def __init__(self, body, action_loc_key=None, loc_key=None,
loc_args=None, launch_image=None): loc_args=None, launch_image=None):
super(PayloadAlert, self).__init__()
self.body = body self.body = body
self.action_loc_key = action_loc_key self.action_loc_key = action_loc_key
self.loc_key = loc_key self.loc_key = loc_key
Expand All @@ -170,7 +181,6 @@ def __init__(self):
class Payload(object): class Payload(object):
"""A class representing an APNs message payload""" """A class representing an APNs message payload"""
def __init__(self, alert=None, badge=None, sound=None, custom={}): def __init__(self, alert=None, badge=None, sound=None, custom={}):
super(Payload, self).__init__()
self.alert = alert self.alert = alert
self.badge = badge self.badge = badge
self.sound = sound self.sound = sound
Expand Down Expand Up @@ -213,12 +223,10 @@ class FeedbackConnection(APNsConnection):
""" """
A class representing a connection to the APNs Feedback server A class representing a connection to the APNs Feedback server
""" """
def __init__(self, use_sandbox=False, **kwargs): def __init__(self, host, port, **kwargs):
super(FeedbackConnection, self).__init__(**kwargs) super(FeedbackConnection, self).__init__(**kwargs)
self.server = ( self.server = host
'feedback.push.apple.com', self.port = port
'feedback.sandbox.push.apple.com')[use_sandbox]
self.port = 2196


def _chunks(self): def _chunks(self):
BUF_SIZE = 4096 BUF_SIZE = 4096
Expand Down Expand Up @@ -267,12 +275,10 @@ class GatewayConnection(APNsConnection):
""" """
A class that represents a connection to the APNs gateway server A class that represents a connection to the APNs gateway server
""" """
def __init__(self, use_sandbox=False, **kwargs): def __init__(self, host, port, **kwargs):
super(GatewayConnection, self).__init__(**kwargs) super(GatewayConnection, self).__init__(**kwargs)
self.server = ( self.server = host
'gateway.push.apple.com', self.port = port
'gateway.sandbox.push.apple.com')[use_sandbox]
self.port = 2195


def _get_notification(self, token_hex, payload): def _get_notification(self, token_hex, payload):
""" """
Expand Down
1 change: 0 additions & 1 deletion tests.py
Expand Up @@ -5,7 +5,6 @@
from random import random from random import random


import hashlib import hashlib
import os
import time import time
import unittest import unittest


Expand Down

0 comments on commit 02c4bb1

Please sign in to comment.