24
24
RECEIPT_URL = BASE_URL + "receipts/"
25
25
GLANCE_URL = BASE_URL + "glances.json"
26
26
27
+
27
28
class RequestError (Exception ):
28
29
"""Exception which is raised when Pushover's API returns an error code.
29
30
@@ -38,7 +39,7 @@ def __str__(self):
38
39
return "\n ==> " + "\n ==> " .join (self .errors )
39
40
40
41
41
- class Request :
42
+ class Request ( object ) :
42
43
"""Base class to send a request to the Pushover server and check the return
43
44
status code. The request is sent on instantiation and raises
44
45
a :class:`RequestError` exception when the request is rejected.
@@ -70,9 +71,11 @@ class MessageRequest(Request):
70
71
can poll the status of the notification with the :func:`poll` function.
71
72
"""
72
73
73
- params = {"expired" : "expires_at" ,
74
- "called_back" : "called_back_at" ,
75
- "acknowledged" : "acknowledged_at" }
74
+ params = {
75
+ "expired" : "expires_at" ,
76
+ "called_back" : "called_back_at" ,
77
+ "acknowledged" : "acknowledged_at" ,
78
+ }
76
79
77
80
def __init__ (self , payload ):
78
81
Request .__init__ (self , "post" , MESSAGE_URL , payload )
@@ -122,18 +125,35 @@ def cancel(self):
122
125
cancels the notification early.
123
126
"""
124
127
if not self .status ["done" ]:
125
- return Request ("post" , self .url + "/cancel.json" , {"token" : self .payload ["token" ]})
128
+ return Request (
129
+ "post" , self .url + "/cancel.json" , {"token" : self .payload ["token" ]}
130
+ )
131
+ else :
132
+ return None
126
133
127
134
128
- class Pushover :
135
+ class Pushover ( object ) :
129
136
"""This is the main class of the module. It represents a Pushover app and
130
137
is tied to a unique API token.
131
138
132
139
* ``token``: Pushover API token
133
140
"""
134
141
135
142
_SOUNDS = None
136
- message_keywords = ["title" , "priority" , "sound" , "callback" , "timestamp" , "url" , "url_title" , "device" , "retry" , "expire" , "html" , "attachment" ]
143
+ message_keywords = [
144
+ "title" ,
145
+ "priority" ,
146
+ "sound" ,
147
+ "callback" ,
148
+ "timestamp" ,
149
+ "url" ,
150
+ "url_title" ,
151
+ "device" ,
152
+ "retry" ,
153
+ "expire" ,
154
+ "html" ,
155
+ "attachment" ,
156
+ ]
137
157
glance_keywords = ["title" , "text" , "subtext" , "count" , "percent" , "device" ]
138
158
139
159
def __init__ (self , token ):
@@ -149,7 +169,6 @@ def sounds(self):
149
169
Pushover ._SOUNDS = request .answer ["sounds" ]
150
170
return Pushover ._SOUNDS
151
171
152
-
153
172
def verify (self , user , device = None ):
154
173
"""Verify that the `user` and optional `device` exist. Returns
155
174
`None` when the user/device does not exist or a list of the user's
@@ -165,14 +184,13 @@ def verify(self, user, device=None):
165
184
else :
166
185
return request .answer ["devices" ]
167
186
168
-
169
187
def message (self , user , message , ** kwargs ):
170
188
"""Send `message` to the user specified by `user`. It is possible
171
189
to specify additional properties of the message by passing keyword
172
190
arguments. The list of valid keywords is ``title, priority, sound,
173
191
callback, timestamp, url, url_title, device, retry, expire and html``
174
192
which are described in the Pushover API documentation.
175
-
193
+
176
194
For convenience, you can simply set ``timestamp=True`` to set the
177
195
timestamp to the current timestamp.
178
196
@@ -187,9 +205,9 @@ def message(self, user, message, **kwargs):
187
205
if key not in Pushover .message_keywords :
188
206
raise ValueError ("{0}: invalid message parameter" .format (key ))
189
207
elif key == "timestamp" and value is True :
190
- payload [key ] = int (time .time ())
208
+ payload [key ] = int (time .time ())
191
209
elif key == "sound" and value not in self .sounds :
192
- raise ValueError ("{0}: invalid sound" .format (value ))
210
+ raise ValueError ("{0}: invalid sound" .format (value ))
193
211
else :
194
212
payload [key ] = value
195
213
0 commit comments