Skip to content

Commit

Permalink
Merge pull request #1861 from lqhuang/fix-meta-attr-for-paginator
Browse files Browse the repository at this point in the history
Fix `AttributeError` by accessing `meta` field when `return_type` is dict
  • Loading branch information
Harmon758 committed Oct 25, 2022
2 parents 18a34f0 + 8d77925 commit ac882b3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
26 changes: 24 additions & 2 deletions tweepy/asynchronous/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@

from math import inf

import aiohttp

from tweepy.client import Response


class AsyncPaginator:
""":class:`AsyncPaginator` can be used to paginate for any
:class:`AsyncClient` methods that support pagination
.. note::
When the returned response from the method being passed is of type
:class:`aiohttp.ClientResponse`, it will be deserialized in order to
parse the pagination tokens, likely negating any potential performance
benefits from using a :class:`aiohttp.ClientResponse` return type.
.. versionadded:: 4.11
Parameters
Expand Down Expand Up @@ -101,8 +112,19 @@ async def __anext__(self):

response = await 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, aiohttp.ClientResponse):
meta = (await response.json()).get("meta", {})
else:
raise NotImplementedError(
f"Unknown {type(response)} return type for {self.method}"
)

self.previous_token = meta.get("previous_token")
self.next_token = meta.get("next_token")
self.count += 1

return response
26 changes: 24 additions & 2 deletions tweepy/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@

from math import inf

import requests

from tweepy.client import Response


class Paginator:
""":class:`Paginator` can be used to paginate for any :class:`Client`
methods that support pagination
.. note::
When the returned response from the method being passed is of type
:class:`requests.Response`, it will be deserialized in order to parse
the pagination tokens, likely negating any potential performance
benefits from using a :class:`requests.Response` return type.
.. versionadded:: 4.0
Parameters
Expand Down Expand Up @@ -97,8 +108,19 @@ def __next__(self):

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(
f"Unknown {type(response)} return type for {self.method}"
)

self.previous_token = meta.get("previous_token")
self.next_token = meta.get("next_token")
self.count += 1

return response

0 comments on commit ac882b3

Please sign in to comment.