Fetching notifications works again, fixes #31
[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
8a18ad2c 18 self._data = {}
178faa46
MM
19 self._notifications = self.get()
20
21 def __iter__(self):
22 return iter(self._notifications)
23
2ec93347
MM
24 def __getitem__(self, n):
25 return self._notifications[n]
26
178faa46
MM
27 def last(self):
28 """Returns list of most recent notifications.
29 """
30 params = {'per_page': 5, '_': int(round(time.time(), 3)*1000)}
bc49cc41 31 headers = {'x-csrf-token': repr(self._connection)}
178faa46
MM
32
33 request = self._connection.get('notifications.json', headers=headers, params=params)
34
35 if request.status_code != 200:
36 raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code))
37 return [Notification(self._connection, n) for n in request.json()]
38
178faa46
MM
39 def get(self, per_page=5, page=1):
40 """Returns list of notifications.
41 """
42 params = {'per_page': per_page, 'page': page}
bc49cc41 43 headers = {'x-csrf-token': repr(self._connection)}
178faa46
MM
44
45 request = self._connection.get('notifications.json', headers=headers, params=params)
2ec93347 46
178faa46
MM
47 if request.status_code != 200:
48 raise Exception('status code: {0}: cannot retreive notifications'.format(request.status_code))
49
50 notifications = request.json()
8a18ad2c
MM
51 self._data['unread_count'] = notifications['unread_count']
52 self._data['unread_count_by_type'] = notifications['unread_count_by_type']
53 return [Notification(self._connection, n) for n in notifications.get('notification_list', [])]