diff options
-rw-r--r-- | docs/index.rst | 10 | ||||
-rw-r--r-- | mastodon/Mastodon.py | 15 | ||||
-rw-r--r-- | mastodon/__init__.py | 4 | ||||
-rw-r--r-- | tests/test_pagination.py | 4 |
4 files changed, 19 insertions, 14 deletions
diff --git a/docs/index.rst b/docs/index.rst index 32d928f..a883e67 100644 --- a/docs/index.rst +++ b/docs/index.rst | |||
@@ -102,7 +102,12 @@ instance may choose to return less results than you requested. | |||
102 | The responses returned by paginated endpoints contain a "link" header that specifies | 102 | The responses returned by paginated endpoints contain a "link" header that specifies |
103 | which parameters to use to get the next and previous pages. Mastodon.py parses these | 103 | which parameters to use to get the next and previous pages. Mastodon.py parses these |
104 | and stores them (if present) in the first (for the previous page) and last (for the | 104 | and stores them (if present) in the first (for the previous page) and last (for the |
105 | next page) item of the returned list as _pagination_prev and _pagination_next. | 105 | next page) item of the returned list as _pagination_prev and _pagination_next. They |
106 | are accessible only via attribute-style access, and are not considered in determining | ||
107 | dict equality (meaning their presence will not affect any tests of wheter two dicts | ||
108 | are the same, or whether a specific dict is in a list). Note that this also means that | ||
109 | if you want to persist pagination info with your data, you'll have to take care of that | ||
110 | manually (or persist objects, not just dicts). | ||
106 | 111 | ||
107 | There are convenience functions available for fetching the previous and next page of | 112 | There are convenience functions available for fetching the previous and next page of |
108 | a paginated request as well as for fetching all pages starting from a first page. | 113 | a paginated request as well as for fetching all pages starting from a first page. |
@@ -188,7 +193,8 @@ you can also just write | |||
188 | 193 | ||
189 | description = mastodon.account_verify_credentials().source.note | 194 | description = mastodon.account_verify_credentials().source.note |
190 | 195 | ||
191 | and everything will work as intended. | 196 | and everything will work as intended. The class used for this is exposed as |
197 | `AttribAccessDict`. | ||
192 | 198 | ||
193 | 199 | ||
194 | User dicts | 200 | User dicts |
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 2adc82e..e365ce1 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py | |||
@@ -82,7 +82,7 @@ class AttribAccessDict(dict): | |||
82 | return self[attr] | 82 | return self[attr] |
83 | else: | 83 | else: |
84 | raise AttributeError("Attribute not found: " + str(attr)) | 84 | raise AttributeError("Attribute not found: " + str(attr)) |
85 | 85 | ||
86 | def __setattr__(self, attr, val): | 86 | def __setattr__(self, attr, val): |
87 | if attr in self: | 87 | if attr in self: |
88 | raise AttributeError("Attribute-style access is read only") | 88 | raise AttributeError("Attribute-style access is read only") |
@@ -1480,8 +1480,8 @@ class Mastodon: | |||
1480 | Returns the next page or None if no further data is available. | 1480 | Returns the next page or None if no further data is available. |
1481 | """ | 1481 | """ |
1482 | if isinstance(previous_page, list) and len(previous_page) != 0: | 1482 | if isinstance(previous_page, list) and len(previous_page) != 0: |
1483 | if '_pagination_next' in previous_page[-1]: | 1483 | if hasattr(previous_page[-1], '_pagination_next'): |
1484 | params = copy.deepcopy(previous_page[-1]['_pagination_next']) | 1484 | params = copy.deepcopy(previous_page[-1]._pagination_next) |
1485 | else: | 1485 | else: |
1486 | return None | 1486 | return None |
1487 | else: | 1487 | else: |
@@ -1504,8 +1504,8 @@ class Mastodon: | |||
1504 | Returns the previous page or None if no further data is available. | 1504 | Returns the previous page or None if no further data is available. |
1505 | """ | 1505 | """ |
1506 | if isinstance(next_page, list) and len(next_page) != 0: | 1506 | if isinstance(next_page, list) and len(next_page) != 0: |
1507 | if '_pagination_prev' in next_page[0]: | 1507 | if hasattr(next_page[0], '_pagination_prev'): |
1508 | params = copy.deepcopy(next_page[0]['_pagination_prev']) | 1508 | params = copy.deepcopy(next_page[0]._pagination_prev) |
1509 | else: | 1509 | else: |
1510 | return None | 1510 | return None |
1511 | else: | 1511 | else: |
@@ -1655,7 +1655,6 @@ class Mastodon: | |||
1655 | Internal API request helper. | 1655 | Internal API request helper. |
1656 | """ | 1656 | """ |
1657 | response = None | 1657 | response = None |
1658 | headers = None | ||
1659 | remaining_wait = 0 | 1658 | remaining_wait = 0 |
1660 | # "pace" mode ratelimiting: Assume constant rate of requests, sleep a little less long than it | 1659 | # "pace" mode ratelimiting: Assume constant rate of requests, sleep a little less long than it |
1661 | # would take to not hit the rate limit at that request rate. | 1660 | # would take to not hit the rate limit at that request rate. |
@@ -1804,7 +1803,7 @@ class Mastodon: | |||
1804 | next_params['max_id'] = int(matchgroups.group(1)) | 1803 | next_params['max_id'] = int(matchgroups.group(1)) |
1805 | if "since_id" in next_params: | 1804 | if "since_id" in next_params: |
1806 | del next_params['since_id'] | 1805 | del next_params['since_id'] |
1807 | response[-1]['_pagination_next'] = next_params | 1806 | response[-1]._pagination_next = next_params |
1808 | 1807 | ||
1809 | if url['rel'] == 'prev': | 1808 | if url['rel'] == 'prev': |
1810 | # Be paranoid and extract since_id specifically | 1809 | # Be paranoid and extract since_id specifically |
@@ -1818,7 +1817,7 @@ class Mastodon: | |||
1818 | prev_params['since_id'] = int(matchgroups.group(1)) | 1817 | prev_params['since_id'] = int(matchgroups.group(1)) |
1819 | if "max_id" in prev_params: | 1818 | if "max_id" in prev_params: |
1820 | del prev_params['max_id'] | 1819 | del prev_params['max_id'] |
1821 | response[0]['_pagination_prev'] = prev_params | 1820 | response[0]._pagination_prev = prev_params |
1822 | 1821 | ||
1823 | 1822 | ||
1824 | return response | 1823 | return response |
diff --git a/mastodon/__init__.py b/mastodon/__init__.py index 787d4e8..21de9ae 100644 --- a/mastodon/__init__.py +++ b/mastodon/__init__.py | |||
@@ -1,4 +1,4 @@ | |||
1 | from mastodon.Mastodon import Mastodon, MastodonError, MastodonVersionError, MastodonIllegalArgumentError, MastodonIOError, MastodonFileNotFoundError, MastodonNetworkError, MastodonAPIError, MastodonNotFoundError, MastodonUnauthorizedError, MastodonRatelimitError, MastodonMalformedEventError | 1 | from mastodon.Mastodon import Mastodon, AttribAccessDict, MastodonError, MastodonVersionError, MastodonIllegalArgumentError, MastodonIOError, MastodonFileNotFoundError, MastodonNetworkError, MastodonAPIError, MastodonNotFoundError, MastodonUnauthorizedError, MastodonRatelimitError, MastodonMalformedEventError |
2 | from mastodon.streaming import StreamListener, CallbackStreamListener | 2 | from mastodon.streaming import StreamListener, CallbackStreamListener |
3 | 3 | ||
4 | __all__ = ['Mastodon', 'StreamListener', 'CallbackStreamListener', 'MastodonError', 'MastodonVersionError', 'MastodonIllegalArgumentError', 'MastodonIOError', 'MastodonFileNotFoundError', 'MastodonNetworkError', 'MastodonAPIError', 'MastodonNotFoundError', 'MastodonUnauthorizedError', 'MastodonRatelimitError', 'MastodonMalformedEventError'] | 4 | __all__ = ['Mastodon', 'AttribAccessDict', 'StreamListener', 'CallbackStreamListener', 'MastodonError', 'MastodonVersionError', 'MastodonIllegalArgumentError', 'MastodonIOError', 'MastodonFileNotFoundError', 'MastodonNetworkError', 'MastodonAPIError', 'MastodonNotFoundError', 'MastodonUnauthorizedError', 'MastodonRatelimitError', 'MastodonMalformedEventError'] |
diff --git a/tests/test_pagination.py b/tests/test_pagination.py index 44fafa1..599b2f4 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py | |||
@@ -31,9 +31,9 @@ def test_fetch_next_previous_from_pagination_info(api): | |||
31 | account = api.account_verify_credentials() | 31 | account = api.account_verify_credentials() |
32 | with many_statuses(api): | 32 | with many_statuses(api): |
33 | statuses = api.account_statuses(account['id'], limit=5) | 33 | statuses = api.account_statuses(account['id'], limit=5) |
34 | next_statuses = api.fetch_next(statuses[-1]['_pagination_next']) | 34 | next_statuses = api.fetch_next(statuses[-1]._pagination_next) |
35 | assert next_statuses | 35 | assert next_statuses |
36 | previous_statuses = api.fetch_previous(next_statuses[0]['_pagination_prev']) | 36 | previous_statuses = api.fetch_previous(next_statuses[0]._pagination_prev) |
37 | assert previous_statuses | 37 | assert previous_statuses |
38 | 38 | ||
39 | 39 | ||