Notifications can now be marked as `read` or `unread`
[diaspy.git] / diaspy / notifications.py
1 #!/usr/bin/env python3
2
3
4 import time
5 from diaspy.models import Notification
6
7
8 """This module abstracts notifications.
9 """
10
11
12 class Notifications():
13 """This class represents notifications of a user.
14 """
15 def __init__(self, connection):
16 self._connection = connection
17 self._notifications = self.get()
18
19 def __iter__(self):
20 return iter(self._notifications)
21
22 def last(self):
23 """Returns list of most recent notifications.
24 """
25 params = {'per_page': 5, '_': int(round(time.time(), 3)*1000)}
26 headers = {'x-csrf-token': self._connection.get_token()}
27
28 request = self._connection.get('notifications.json', headers=headers, params=params)
29
30 if request.status_code != 200:
31 raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code))
32 return [Notification(self._connection, n) for n in request.json()]
33
34
35 def get(self, per_page=5, page=1):
36 """Returns list of notifications.
37 """
38 params = {'per_page': per_page, 'page': page}
39 headers = {'x-csrf-token': self._connection.get_token()}
40
41 request = self._connection.get('notifications.json', headers=headers, params=params)
42
43 if request.status_code != 200:
44 raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code))
45
46 notifications = request.json()
47 return [Notification(self._connection, n) for n in notifications]