Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactor, make hosts and ports configurable via constructor

  • Loading branch information...
commit 02c4bb19a3115204f115dbd3c2fea1329fea89ee 1 parent 0f2f5d2
jacobcr authored

Showing 2 changed files with 24 additions and 19 deletions. Show diff stats Hide diff stats

  1. +24 18 apns.py
  2. +0 1  tests.py
42 apns.py
@@ -43,15 +43,25 @@
43 43 class APNs(object):
44 44 """A class representing an Apple Push Notification service connection"""
45 45
46   - def __init__(self, use_sandbox=False, cert_file=None, key_file=None):
  46 + feedback = ('feedback.push.apple.com', 'feedback.sandbox.push.apple.com')
  47 + gateway = ('gateway.push.apple.com', 'gateway.sandbox.push.apple.com')
  48 +
  49 + def __init__(self, use_sandbox=False, cert_file=None, key_file=None,
  50 + host=None, port=2195,
  51 + feedback_host=None, feedback_port=2196):
47 52 """
48 53 Set use_sandbox to True to use the sandbox (test) APNs servers.
49 54 Default is False.
50 55 """
51   - super(APNs, self).__init__()
52   - self.use_sandbox = use_sandbox
53 56 self.cert_file = cert_file
54 57 self.key_file = key_file
  58 +
  59 + self.host = host if host else self.gateway[use_sandbox]
  60 + self.port = port
  61 +
  62 + self.feedback_host = feedback_host if feedback_host else self.feedback[use_sandbox]
  63 + self.feedback_port = feedback_port
  64 +
55 65 self._feedback_connection = None
56 66 self._gateway_connection = None
57 67
@@ -87,8 +97,10 @@ def unpacked_uint_big_endian(bytes):
87 97 @property
88 98 def feedback_server(self):
89 99 if not self._feedback_connection:
  100 +
90 101 self._feedback_connection = FeedbackConnection(
91   - use_sandbox = self.use_sandbox,
  102 + host = self.feedback_host,
  103 + port = self.feedback_port,
92 104 cert_file = self.cert_file,
93 105 key_file = self.key_file
94 106 )
@@ -98,7 +110,8 @@ def feedback_server(self):
98 110 def gateway_server(self):
99 111 if not self._gateway_connection:
100 112 self._gateway_connection = GatewayConnection(
101   - use_sandbox = self.use_sandbox,
  113 + host = self.host,
  114 + port = self.port,
102 115 cert_file = self.cert_file,
103 116 key_file = self.key_file
104 117 )
@@ -110,7 +123,6 @@ class APNsConnection(object):
110 123 A generic connection class for communicating with the APNs
111 124 """
112 125 def __init__(self, cert_file=None, key_file=None):
113   - super(APNsConnection, self).__init__()
114 126 self.cert_file = cert_file
115 127 self.key_file = key_file
116 128 self._socket = None
@@ -144,7 +156,6 @@ def write(self, string):
144 156 class PayloadAlert(object):
145 157 def __init__(self, body, action_loc_key=None, loc_key=None,
146 158 loc_args=None, launch_image=None):
147   - super(PayloadAlert, self).__init__()
148 159 self.body = body
149 160 self.action_loc_key = action_loc_key
150 161 self.loc_key = loc_key
@@ -170,7 +181,6 @@ def __init__(self):
170 181 class Payload(object):
171 182 """A class representing an APNs message payload"""
172 183 def __init__(self, alert=None, badge=None, sound=None, custom={}):
173   - super(Payload, self).__init__()
174 184 self.alert = alert
175 185 self.badge = badge
176 186 self.sound = sound
@@ -213,12 +223,10 @@ class FeedbackConnection(APNsConnection):
213 223 """
214 224 A class representing a connection to the APNs Feedback server
215 225 """
216   - def __init__(self, use_sandbox=False, **kwargs):
  226 + def __init__(self, host, port, **kwargs):
217 227 super(FeedbackConnection, self).__init__(**kwargs)
218   - self.server = (
219   - 'feedback.push.apple.com',
220   - 'feedback.sandbox.push.apple.com')[use_sandbox]
221   - self.port = 2196
  228 + self.server = host
  229 + self.port = port
222 230
223 231 def _chunks(self):
224 232 BUF_SIZE = 4096
@@ -267,12 +275,10 @@ class GatewayConnection(APNsConnection):
267 275 """
268 276 A class that represents a connection to the APNs gateway server
269 277 """
270   - def __init__(self, use_sandbox=False, **kwargs):
  278 + def __init__(self, host, port, **kwargs):
271 279 super(GatewayConnection, self).__init__(**kwargs)
272   - self.server = (
273   - 'gateway.push.apple.com',
274   - 'gateway.sandbox.push.apple.com')[use_sandbox]
275   - self.port = 2195
  280 + self.server = host
  281 + self.port = port
276 282
277 283 def _get_notification(self, token_hex, payload):
278 284 """
1  tests.py
@@ -5,7 +5,6 @@
5 5 from random import random
6 6
7 7 import hashlib
8   -import os
9 8 import time
10 9 import unittest
11 10

0 comments on commit 02c4bb1

Please sign in to comment.
Something went wrong with that request. Please try again.