1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """Module for abstract class.
22
23 """
24
25 import logging
26 import requests
27
28
30 """Abstract client for sending push notifications. Inherit from this
31 class but don't call it directly.
32
33 Member Vars:
34 developerkey: A string containing a valid developer key for the
35 client's application.
36 application: A string containing the name of the application on
37 behalf of whom the client will be sending messages.
38 apikeys: A dictionary where the keys are strings containing
39 valid user API keys, and the values are lists of strings,
40 each containing a valid user device key.
41
42 """
43
44 - def __init__(self, developerkey='', application=''):
45 """Initialize the client.
46
47 Args:
48 developerkey: A string containing a valid developer key for
49 the client's application.
50 application: A string containing the name of the application
51 on behalf of whom the client will be sending messages.
52
53 """
54
55 self.logger = logging.getLogger('{0}.{1}'.format(
56 self.__module__, self.__class__.__name__))
57
58 if not application:
59 application = 'pushnotify'
60
61 self.developerkey = developerkey
62 self.application = application
63 self.apikeys = {}
64
65 self._browser = requests.Session()
66 self._last = {}
67 self._urls = {'notify': '', 'verify': ''}
68
69 - def _get(self, url, data):
70
71 self.logger.debug('_get requesting url: {0}'.format(url))
72
73 try:
74 response = self._browser.get(url, params=data)
75 except requests.exceptions.RequestException, exc:
76 return exc
77 else:
78 return response
79
80 - def _post(self, url, data):
81
82 self.logger.debug('_post sending data: {0}'.format(data))
83 self.logger.debug('_post sending to url: {0}'.format(url))
84
85 try:
86 response = self._browser.post(url, data=data)
87 except requests.exceptions.RequestException, exc:
88 return exc
89 else:
90 return response
91
92 - def add_key(self, apikey, device_key=''):
93 """Add the given key to self.apikeys.
94
95 Args:
96 apikey: A string containing a valid user's API key for the
97 client's application.
98 device_key: A string containing a valid device key to go
99 along with the API key. (default: '')
100 """
101
102 if apikey not in self.apikeys:
103 self.apikeys[apikey] = []
104
105 if device_key and device_key not in self.apikeys[apikey]:
106 self.apikeys[apikey].append(device_key)
107
108 - def del_key(self, apikey, device_key=''):
109 """Delete the given API key or device key from self.apikeys.
110
111 If device_key is not set, delete apikey and all of its device
112 keys. Otherwise only delete the device key.
113
114 Args:
115 apikey: A string containing a valid user's API key that is
116 in self.apikeys.
117 device_key: A string containing a valid device key that is
118 in self.apikeys[apikey]. (default: '')
119
120 """
121
122 if device_key:
123 self.apikeys[apikey] = [value for value in self.apikeys[apikey]
124 if value != device_key]
125 else:
126 del(self.apikeys[apikey])
127
128 - def notify(self, description, event, split=True, kwargs=None):
129 """Send a notification to each user/device combination in
130 self.apikeys.
131
132 Args:
133 description: A string containing the main notification text.
134 The maximum length varies by application. See each
135 client's documentation for details.
136 event: A string containing a subject or brief description of
137 the event. The maximum length varies by application. See
138 each client's documentation for details.
139 split: A boolean indicating whether to split long
140 descriptions among multiple notifications (True) or to
141 raise an exception if it is too long (False).
142 (default True)
143 kwargs: A dictionary for application specific options. See
144 each client's documentation for details.
145 (default: None)
146
147 Raises:
148 pushnotify.exceptions.ApiKeyError
149 pushnotify.exceptions.FormatError
150 pushnotify.exceptions.RateLimitExceeded
151 pushnotify.exceptions.ServerError
152 pushnotify.exceptions.UnknownError
153 pushnotify.exceptions.UnrecognizedResponseError
154
155 """
156
157 raise NotImplementedError
158
160
161 raise NotImplementedError
162
164
165 raise NotImplementedError
166
168 """Verify a device identifier for the user given by apikey.
169
170 Args:
171 apikey: A string containing a user identifer.
172 device_key: A string containing a device identifier.
173
174 Raises:
175 pushnotify.exceptions.ApiKeyError
176
177 Returns:
178 A boolean containing True if device_key is valid for
179 apikey, and False if it is not.
180
181 """
182
183 raise NotImplementedError
184
186 """Verify a user's API key.
187
188 Args:
189 apikey: A string containing a user's API key.
190
191 Raises:
192 pushnotify.exceptions.RateLimitExceeded
193 pushnotify.exceptions.ServerError
194 pushnotify.exceptions.UnknownError
195 pushnotify.exceptions.UnrecognizedResponseError
196
197 Returns:
198 A boolean containing True if the user's API key is valid,
199 and False if it is not.
200
201 """
202
203 raise NotImplementedError
204