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