@@ -187,11 +187,13 @@ class _PageIterator(Generic[ResponseType], Iterator[ResponseType]):
187187 # Make the API call with both positional and keyword arguments
188188 response = self.cursor.method(*self.cursor.args, **params)
189189
190- # Extract next token
191- self._next_token = self._extract_next_token(response)
190+ # Extract next token AFTER returning this response
191+ # This allows us to check if there are more pages
192+ extracted_token = self._extract_next_token(response)
193+ self._next_token = extracted_token
192194
193- # Check if we're done
194- if not self._next_token :
195+ # Check if we're done - if no token was extracted, we've reached the end
196+ if not extracted_token :
195197 self.exhausted = True
196198
197199 self.count += 1
@@ -210,9 +212,27 @@ class _PageIterator(Generic[ResponseType], Iterator[ResponseType]):
210212 def _extract_next_token(self, response: ResponseType) -> Optional[str]:
211213 """Extract the next_token from the response."""
212214 # Try common patterns for next_token
213- if hasattr(response, 'meta') and response.meta:
214- if hasattr(response.meta, 'next_token'):
215- return response.meta.next_token
215+ # Pattern 1: response.meta.next_token (most common)
216+ try:
217+ if hasattr(response, 'meta') and response.meta is not None:
218+ # Try to get next_token from meta
219+ meta = response.meta
220+ if hasattr(meta, 'next_token'):
221+ token = getattr(meta, 'next_token', None)
222+ if token:
223+ return str(token)
224+ except (AttributeError, TypeError):
225+ pass
226+
227+ # Pattern 2: response.next_token (some APIs)
228+ try:
229+ if hasattr(response, 'next_token'):
230+ token = getattr(response, 'next_token', None)
231+ if token:
232+ return str(token)
233+ except (AttributeError, TypeError):
234+ pass
235+
216236 return None
217237
218238
@@ -259,8 +279,11 @@ class _ItemIterator(Iterator[Any]):
259279 """Extract items from response, trying common field names."""
260280 # Try common field names for data arrays
261281 for field_name in ['data', 'results', 'items']:
262- if hasattr(response, field_name):
263- items = getattr(response, field_name)
264- if isinstance(items, list):
265- return items
282+ try:
283+ if hasattr(response, field_name):
284+ items = getattr(response, field_name, None)
285+ if isinstance(items, list):
286+ return items
287+ except (AttributeError, TypeError):
288+ continue
266289 return None
0 commit comments