| 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | |
| 4 | import time |
| 5 | |
| 6 | from diaspy.models import Notification |
| 7 | |
| 8 | |
| 9 | """This module abstracts notifications. |
| 10 | """ |
| 11 | |
| 12 | |
| 13 | class 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 | |
| 23 | def __getitem__(self, n): |
| 24 | return self._notifications[n] |
| 25 | |
| 26 | def last(self): |
| 27 | """Returns list of most recent notifications. |
| 28 | """ |
| 29 | params = {'per_page': 5, '_': int(round(time.time(), 3)*1000)} |
| 30 | headers = {'x-csrf-token': self._connection.get_token()} |
| 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 | |
| 38 | def get(self, per_page=5, page=1): |
| 39 | """Returns list of notifications. |
| 40 | """ |
| 41 | params = {'per_page': per_page, 'page': page} |
| 42 | headers = {'x-csrf-token': self._connection.get_token()} |
| 43 | |
| 44 | request = self._connection.get('notifications.json', headers=headers, params=params) |
| 45 | |
| 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] |