-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygcm.py
104 lines (80 loc) · 2.9 KB
/
pygcm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import webapp2
from webapp2_extras import json
from google.appengine.api import urlfetch
"""
PyGCMRegister exceptions
"""
class PyGCMCannotParseJSON(Exception): pass
class PyGCMInvalidInputData(Exception): pass
class PyGCMRegParameterNotExists(Exception): pass
"""
PyGCMRequester exceptions
"""
class PyGCMNotADevicesList(Exception): pass
class PyGCMNotADictData(Exception): pass
class PyGCMCannotSendPushTimeout(Exception): pass
class PyGCMNotAValidDevicesList(Exception): pass
class PyGCMCannotSendPushTimeout(Exception): pass
class PyGCMServerCannotParseJSON(Exception): pass
class PyGCMServerCannotAuthenticateSenderAccount(Exception): pass
class PyGCMInternalGCMServerError(Exception): pass
class PyGCMCannotParseJSONGCMResponse(Exception): pass
"""
Class which handle the input requests giving the register keys of devices
@params reg_parameter -> json name-parameter which hosts the register key
@return the register key of device
"""
class PyGCMRegister(object):
def __init__(self, reg_parameter):
self.reg_parameter = reg_parameter
def HandleRegistration(self):
request = webapp2.get_request()
# If we can't parse the input json, throw an exception
try:
data = json.decode(request.body)
except ValueError:
raise PyGCMCannotParseJSON()
# We also ensure that the deserialized json is now a dict
if not isinstance(data, dict):
raise PyGCMInvalidInputData
# Return the final value (register key)
if self.reg_parameter in data:
return data[self.reg_parameter]
else:
raise PyGCMRegParameterNotExists
class PyGCMRequester(object):
headers = {
'Content-type': 'application/json',
}
def __init__(self, GCM_URL, GCM_KEY):
self.GCM_URL = GCM_URL
headers['Authorization'] = 'key=%s' % GCM_KEY
def sendPushNotification(self, devices, data):
if not isinstance(devices, list):
raise PyGCMNotADevicesList()
if not all(isinstance(key, str) for key in decices):
raise PyGCMNotAValidDevicesList()
if not isinstance(data, dict):
raise PyGCMNotADictData()
# TODO: encode data to JSON
# This uses the GAE fetch function to perform the request
try:
response = urlfetch.fetch(url=GCM_URL,
data=data,
method=urlfetch.POST,
headers=headers,
validate_certificate=True)
except DownloadError:
raise PyGCMCannotSendPushTimeout()
status_code = response.status_code
if status_code == 400:
raise PyGCMServerCannotParseJSON()
if status_code == 401:
raise PyGCMServerCannotAuthenticateSenderAccount()
if status_code >= 500 and status_code <= 599:
raise PyGCMInternalGCMServerError()
try:
data = json.decode(response.content)
except ValueError:
raise PyGCMCannotParseJSONGCMResponse()
# TODO: finish to handle response