88GCM_URL = 'https://android.googleapis.com/gcm/send'
99
1010
11- class GCMException (Exception ): pass
12- class GCMMalformedJsonException (GCMException ): pass
13- class GCMConnectionException (GCMException ): pass
14- class GCMAuthenticationException (GCMException ): pass
15- class GCMTooManyRegIdsException (GCMException ): pass
16- class GCMInvalidTtlException (GCMException ): pass
11+ class GCMException (Exception ):
12+ pass
13+
14+
15+ class GCMMalformedJsonException (GCMException ):
16+ pass
17+
18+
19+ class GCMConnectionException (GCMException ):
20+ pass
21+
22+
23+ class GCMAuthenticationException (GCMException ):
24+ pass
25+
26+
27+ class GCMTooManyRegIdsException (GCMException ):
28+ pass
29+
30+
31+ class GCMInvalidTtlException (GCMException ):
32+ pass
1733
1834# Exceptions from Google responses
19- class GCMMissingRegistrationException (GCMException ): pass
20- class GCMMismatchSenderIdException (GCMException ): pass
21- class GCMNotRegisteredException (GCMException ): pass
22- class GCMMessageTooBigException (GCMException ): pass
23- class GCMInvalidRegistrationException (GCMException ): pass
24- class GCMUnavailableException (GCMException ): pass
35+
36+
37+ class GCMMissingRegistrationException (GCMException ):
38+ pass
39+
40+
41+ class GCMMismatchSenderIdException (GCMException ):
42+ pass
43+
44+
45+ class GCMNotRegisteredException (GCMException ):
46+ pass
47+
48+
49+ class GCMMessageTooBigException (GCMException ):
50+ pass
51+
52+
53+ class GCMInvalidRegistrationException (GCMException ):
54+ pass
55+
56+
57+ class GCMUnavailableException (GCMException ):
58+ pass
2559
2660
2761# TODO: Refactor this to be more human-readable
@@ -79,12 +113,12 @@ def __init__(self, api_key, url=GCM_URL, proxy=None):
79113 proxy = {protocol : proxy }
80114
81115 auth = urllib2 .HTTPBasicAuthHandler ()
82- opener = urllib2 .build_opener (urllib2 .ProxyHandler (proxy ), auth , urllib2 .HTTPHandler )
116+ opener = urllib2 .build_opener (
117+ urllib2 .ProxyHandler (proxy ), auth , urllib2 .HTTPHandler )
83118 urllib2 .install_opener (opener )
84119
85-
86120 def construct_payload (self , registration_ids , data = None , collapse_key = None ,
87- delay_while_idle = False , time_to_live = None , is_json = True , dry_run = False ):
121+ delay_while_idle = False , time_to_live = None , is_json = True , dry_run = False ):
88122 """
89123 Construct the dictionary mapping of parameters.
90124 Encodes the dictionary into JSON if for json requests.
@@ -140,7 +174,8 @@ def make_request(self, data, is_json=True):
140174 headers = {
141175 'Authorization' : 'key=%s' % self .api_key ,
142176 }
143- # Default Content-Type is defaulted to application/x-www-form-urlencoded;charset=UTF-8
177+ # Default Content-Type is defaulted to
178+ # application/x-www-form-urlencoded;charset=UTF-8
144179 if is_json :
145180 headers ['Content-Type' ] = 'application/json'
146181
@@ -152,16 +187,19 @@ def make_request(self, data, is_json=True):
152187 response = urllib2 .urlopen (req ).read ()
153188 except urllib2 .HTTPError as e :
154189 if e .code == 400 :
155- raise GCMMalformedJsonException ("The request could not be parsed as JSON" )
190+ raise GCMMalformedJsonException (
191+ "The request could not be parsed as JSON" )
156192 elif e .code == 401 :
157- raise GCMAuthenticationException ("There was an error authenticating the sender account" )
193+ raise GCMAuthenticationException (
194+ "There was an error authenticating the sender account" )
158195 elif e .code == 503 :
159196 raise GCMUnavailableException ("GCM service is unavailable" )
160197 else :
161198 error = "GCM service error: %d" % e .code
162199 raise GCMUnavailableException (error )
163200 except urllib2 .URLError as e :
164- raise GCMConnectionException ("There was an internal error in the GCM server while trying to process the request" )
201+ raise GCMConnectionException (
202+ "There was an internal error in the GCM server while trying to process the request" )
165203
166204 if is_json :
167205 response = json .loads (response )
@@ -173,11 +211,14 @@ def raise_error(self, error):
173211 elif error == 'Unavailable' :
174212 # Plain-text requests will never return Unavailable as the error code.
175213 # http://developer.android.com/guide/google/gcm/gcm.html#error_codes
176- raise GCMUnavailableException ("Server unavailable. Resent the message" )
214+ raise GCMUnavailableException (
215+ "Server unavailable. Resent the message" )
177216 elif error == 'NotRegistered' :
178- raise GCMNotRegisteredException ("Registration id is not valid anymore" )
217+ raise GCMNotRegisteredException (
218+ "Registration id is not valid anymore" )
179219 elif error == 'MismatchSenderId' :
180- raise GCMMismatchSenderIdException ("A Registration ID is tied to a certain group of senders" )
220+ raise GCMMismatchSenderIdException (
221+ "A Registration ID is tied to a certain group of senders" )
181222 elif error == 'MessageTooBig' :
182223 raise GCMMessageTooBigException ("Message can't exceed 4096 bytes" )
183224
@@ -196,7 +237,8 @@ def handle_plaintext_response(self, response):
196237
197238 def handle_json_response (self , response , registration_ids ):
198239 errors = group_response (response , registration_ids , 'error' )
199- canonical = group_response (response , registration_ids , 'registration_id' )
240+ canonical = group_response (
241+ response , registration_ids , 'registration_id' )
200242
201243 info = {}
202244 if errors :
@@ -212,7 +254,7 @@ def extract_unsent_reg_ids(self, info):
212254 return []
213255
214256 def plaintext_request (self , registration_id , data = None , collapse_key = None ,
215- delay_while_idle = False , time_to_live = None , retries = 5 , dry_run = False ):
257+ delay_while_idle = False , time_to_live = None , retries = 5 , dry_run = False ):
216258 """
217259 Makes a plaintext request to GCM servers
218260
@@ -245,7 +287,7 @@ def plaintext_request(self, registration_id, data=None, collapse_key=None,
245287 raise IOError ("Could not make request after %d attempts" % attempt )
246288
247289 def json_request (self , registration_ids , data = None , collapse_key = None ,
248- delay_while_idle = False , time_to_live = None , retries = 5 , dry_run = False ):
290+ delay_while_idle = False , time_to_live = None , retries = 5 , dry_run = False ):
249291 """
250292 Makes a JSON request to GCM servers
251293
@@ -259,7 +301,8 @@ def json_request(self, registration_ids, data=None, collapse_key=None,
259301 if not registration_ids :
260302 raise GCMMissingRegistrationException ("Missing registration_ids" )
261303 if len (registration_ids ) > 1000 :
262- raise GCMTooManyRegIdsException ("Exceded number of registration_ids" )
304+ raise GCMTooManyRegIdsException (
305+ "Exceded number of registration_ids" )
263306
264307 attempt = 0
265308 backoff = self .BACKOFF_INITIAL_DELAY
0 commit comments