generated from C4T-BuT-S4D/ad-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotifylib.py
332 lines (264 loc) · 9.24 KB
/
notifylib.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from typing import Optional
from urllib.parse import urljoin
from functools import cache
import requests
from checklib import Status
from extended_checker import ExtendedChecker
API_PORT = 7777
class Endpoints:
REGISTER = "POST /register"
LOGIN = "POST /login"
CREATE_NOTIFICATION = "POST /notifications"
GET_NOTIFICATION = "GET /notification/:id"
USER_INFO = "GET /user"
@dataclass(frozen=True)
class NotificationCreationRepetitions:
count: int
interval: int
def asdict(self) -> dict:
return {"c": self.count, "i": self.interval}
@staticmethod
def fromdict(d) -> "NotificationCreationRepetitions":
return NotificationCreationRepetitions(count=d["c"], interval=d["i"])
@dataclass(frozen=True)
class NotificationCreationOpts:
title: str
content: str
notify_at: datetime
repetitions: Optional[NotificationCreationRepetitions]
@cache
def to_private_info(self, id: str) -> "PrivateNotificationInfo":
return PrivateNotificationInfo(
id=id,
title=self.title,
content=self.content,
plan=self.repetitions_to_plan(),
)
@cache
def to_public_info(self) -> "PublicNotificationInfo":
return PublicNotificationInfo(title=self.title, plan=self.repetitions_to_plan())
@cache
def repetitions_to_plan(self) -> list["NotificationPlan"]:
plan = [NotificationPlan(planned_at=self.notify_at, sent_at=None)]
if self.repetitions is not None:
for _ in range(self.repetitions.count):
plan.append(
NotificationPlan(
planned_at=plan[-1].planned_at
+ timedelta(seconds=self.repetitions.interval),
sent_at=None,
)
)
return plan
def asdict(self) -> dict:
return {
"t": self.title,
"c": self.content,
"n": self.notify_at.timestamp(),
"r": self.repetitions.asdict() if self.repetitions is not None else None,
}
@staticmethod
def fromdict(d) -> "NotificationCreationOpts":
return NotificationCreationOpts(
title=d["t"],
content=d["c"],
notify_at=datetime.fromtimestamp(d["n"], tz=timezone.utc),
repetitions=NotificationCreationRepetitions.fromdict(d["r"]),
)
@dataclass
class NotificationPlan:
planned_at: datetime
sent_at: Optional[datetime]
@dataclass
class PrivateNotificationInfo:
id: str
title: str
content: str
plan: list[NotificationPlan]
@dataclass
class PublicNotificationInfo:
title: str
plan: list[NotificationPlan]
@dataclass
class UserInfo:
username: str
notifications: list[PrivateNotificationInfo]
# CheckMachine for the HTTP-based notify-api.
# timeouts are set so that the service isn't able to cheat the checker while it is waiting for notifications to be sent.
class CheckMachine:
def __init__(self, checker: ExtendedChecker):
self.c = checker
self.port = API_PORT
def url(self, path):
return urljoin(f"http://{self.c.host}:{self.port}/api/", path)
def register(self, session: requests.Session, username: str, password: str):
resp = session.post(
self.url("register"),
json={"username": username, "password": password},
timeout=1,
)
self.c.check_status(resp, 201, Endpoints.REGISTER, Status.MUMBLE)
self.c.assert_in(
"notify_session",
resp.cookies,
f"{Endpoints.REGISTER} didn't return session",
Status.MUMBLE,
)
def login(
self, session: requests.Session, username: str, password: str, status: Status
):
resp = session.post(
self.url("login"),
json={"username": username, "password": password},
timeout=1,
)
self.c.check_status(resp, 200, Endpoints.LOGIN, status)
self.c.assert_in(
"notify_session",
resp.cookies,
f"{Endpoints.LOGIN} didn't return session",
Status.MUMBLE,
)
def user_info(self, session: requests.Session) -> UserInfo:
resp = session.get(self.url("user"), timeout=1)
self.c.check_status(resp, 200, Endpoints.USER_INFO, Status.MUMBLE)
data = self.c.get_json_dict(
resp, f"{Endpoints.USER_INFO} returned invalid response", Status.MUMBLE
)
username = self.c.get_field(
data, "username", str, f"{Endpoints.USER_INFO} response", Status.MUMBLE
)
notifications = self.c.get_field(
data,
"notifications",
list[dict],
f"{Endpoints.USER_INFO} response",
Status.MUMBLE,
)
user = UserInfo(username, [])
for notification in notifications:
public_info = self.parse_public_notification(
notification, f"{Endpoints.USER_INFO} response"
)
notification_id = self.c.get_field(
notification,
"id",
str,
f"notifications in {Endpoints.USER_INFO} response",
Status.MUMBLE,
)
content = self.c.get_field(
notification,
"content",
str,
f"notifications in {Endpoints.USER_INFO} response",
Status.MUMBLE,
)
user.notifications.append(
PrivateNotificationInfo(
id=notification_id,
title=public_info.title,
content=content,
plan=public_info.plan,
)
)
return user
def create_notification(
self, session: requests.Session, notification: NotificationCreationOpts
) -> str:
request = {
"title": notification.title,
"content": notification.content,
"notify_at": notification.notify_at.isoformat(),
}
if notification.repetitions is not None:
request["repetitions"] = { # type: ignore
"count": notification.repetitions.count,
"interval": notification.repetitions.interval,
}
resp = session.post(self.url("notifications"), json=request, timeout=1)
self.c.check_status(resp, 201, Endpoints.CREATE_NOTIFICATION, Status.MUMBLE)
data = self.c.get_json_dict(
resp,
f"{Endpoints.CREATE_NOTIFICATION} returned invalid response",
Status.MUMBLE,
)
notification_id = self.c.get_field(
data,
"notification_id",
str,
f"{Endpoints.CREATE_NOTIFICATION} response",
Status.MUMBLE,
)
self.c.assert_neq(
notification_id,
"",
f"{Endpoints.CREATE_NOTIFICATION} returned empty notification_id",
Status.MUMBLE,
)
return notification_id
def get_notification(
self,
session: requests.Session,
notification_id: str,
status: Status,
) -> PublicNotificationInfo:
resp = session.get(self.url(f"notification/{notification_id}"), timeout=0.5)
self.c.check_status(resp, 200, Endpoints.GET_NOTIFICATION, status)
data = self.c.get_json_dict(
resp,
f"{Endpoints.GET_NOTIFICATION} returned invalid response",
Status.MUMBLE,
)
public_info = self.parse_public_notification(
data, f"{Endpoints.GET_NOTIFICATION} response"
)
return public_info
def parse_public_notification(
self, data: dict, location: str
) -> PublicNotificationInfo:
title = self.c.get_field(
data,
"title",
str,
location,
Status.MUMBLE,
)
plan = self.c.get_field(
data,
"plan",
list[dict],
location,
Status.MUMBLE,
)
parsed_plan: list[NotificationPlan] = []
for p in plan:
planned_at = self.c.get_field(
p,
"planned_at",
str,
location,
Status.MUMBLE,
)
planned_at = self.parse_datetime(
planned_at, f"invalid planned_at in {location}"
)
sent_at = p.get("sent_at")
if sent_at is not None:
self.c.assert_type(
sent_at, str, f"invalid sent_at in {location}", Status.MUMBLE
)
sent_at = self.parse_datetime(sent_at, f"invalid sent_at in {location}")
parsed_plan.append(NotificationPlan(planned_at, sent_at))
return PublicNotificationInfo(title=title, plan=parsed_plan)
def parse_datetime(self, v: str, public: str) -> datetime: # type: ignore
try:
return datetime.fromisoformat(v)
except Exception as e:
self.c.cquit(
Status.MUMBLE,
public,
f"Failed to parse string {v} as Iso8601: {e}",
)