Fix `AttributeError` by accessing `meta` field when return_type is dict
authorLanqing Huang <lqhuang@outlook.com>
Wed, 13 Apr 2022 08:06:01 +0000 (16:06 +0800)
committerLanqing Huang <lqhuang@outlook.com>
Mon, 24 Oct 2022 02:42:55 +0000 (10:42 +0800)
tweepy/pagination.py

index f61c192a83d499263b030678b873b90ba982da0b..1aea5a7f6833475d86eedb85ef59ba4f9948a717 100644 (file)
@@ -2,8 +2,13 @@
 # Copyright 2009-2022 Joshua Roesslein
 # See LICENSE for details.
 
+from typing import Union
 from math import inf
 
+import requests
+
+from tweepy.client import Response
+
 
 class Paginator:
     """:class:`Paginator` can be used to paginate for any :class:`Client`
@@ -77,7 +82,7 @@ class PaginationIterator:
     def __iter__(self):
         return self
 
-    def __next__(self):
+    def __next__(self) -> Union[Response, dict, requests.Response]:
         if self.reverse:
             pagination_token = self.previous_token
         else:
@@ -97,8 +102,17 @@ class PaginationIterator:
 
         response = self.method(*self.args, **self.kwargs)
 
-        self.previous_token = response.meta.get("previous_token")
-        self.next_token = response.meta.get("next_token")
+        if isinstance(response, Response):
+            meta = response.meta
+        elif isinstance(response, dict):
+            meta = response.get("meta", {})
+        elif isinstance(response, requests.Response):
+            meta = response.json().get("meta", {})
+        else:
+            raise NotImplementedError("Unknown `response` type to parse `meta` field")
+
+        self.previous_token = meta.get("previous_token")
+        self.next_token = meta.get("next_token")
         self.count += 1
 
         return response