Skip to content

Commit 8c9c32f

Browse files
committed
Fix consistency in MessageRequest.poll
The help, example and actual code had different opinions on how the function should behave. This commit make them agree. poll() will return the base Request object until the receipt has expired, been acknowledged or called_back upon. Once one of those three things has occurred it returns None. The example now has expire=120 and retry=60 since without those arguments it is unable to send a message with priority=2. The expired_at does not exist, it is called expires_at in the Pushover API. This is fixed as well. The values of expires_at, acknowledged_at and called_back_at can be found as attributes in the MessageRequest object like before.
1 parent 4a8fc29 commit 8c9c32f

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Contributors
44
* Sam Birch <sam.m.birch@gmail.com>
55
* Crupuk
66
* Thibaut Horel <thibaut.horel@gmail.com>
7+
* Filip Lundborg <filip@filipl.se>

pushover.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,12 @@ def __init__(self, payload):
129129
self.receipt = None
130130
if payload.get("priority", 0) == 2:
131131
self.receipt = self.answer["receipt"]
132-
self.parameters = ["expired", "called_back", "acknowledged"]
133-
for parameter in self.parameters:
134-
setattr(self, parameter, False)
135-
setattr(self, parameter + "_at", 0)
132+
self.parameters = {"expired": "expires_at",
133+
"called_back": "called_back_at",
134+
"acknowledged": "acknowledged_at"}
135+
for param, when in self.parameters.iteritems():
136+
setattr(self, param, False)
137+
setattr(self, when, 0)
136138

137139
def poll(self):
138140
"""If the message request has a priority of 2, Pushover will keep
@@ -147,18 +149,18 @@ def poll(self):
147149
acknowledged, so that a typical handling of a priority-2 notification
148150
can look like this::
149151
150-
request = client.send_message("Urgent notification", priority=2)
151-
while not request.poll():
152+
request = client.send_message("Urgent notification", priority=2,
153+
expire=120, retry=60)
154+
while request.poll():
152155
# do something
153156
time.sleep(5)
154157
"""
155158
if (self.receipt and not any(getattr(self, parameter)
156159
for parameter in self.parameters)):
157160
request = Request("get", RECEIPT_URL + self.receipt + ".json", {})
158-
for parameter in self.parameters:
159-
setattr(self, parameter, request.answer[parameter])
160-
setattr(self, parameter + "_at",
161-
request.answer[parameter + "_at"])
161+
for param, when in self.parameters.iteritems():
162+
setattr(self, param, bool(request.answer[param]))
163+
setattr(self, when, request.answer[when])
162164
return request
163165

164166

0 commit comments

Comments
 (0)