11from __future__ import annotations
22
3+ import time
4+
35from ..errors import WhatsAppError
46
57"""Types for other objects."""
@@ -1123,15 +1125,26 @@ class Result(Generic[_T]):
11231125
11241126 >>> from pywa import WhatsApp, types
11251127 >>> wa = WhatsApp(...)
1126- >>> all_blocked_users = []
11271128 >>> res = wa.get_blocked_users(pagination=types.Pagination(limit=100))
1128- >>> while True:
1129- ... all_blocked_users.extend(res)
1130- ... if not res.has_next:
1131- ... break
1132- ... res = res.next()
1133- >>> all_blocked_users
1134- [User(...), User(...), User(...), ...]
1129+ >>> for user in res:
1130+ ... print(user.name, user.wa_id)
1131+ ...
1132+ >>> if res.has_next:
1133+ ... next_res = res.next()
1134+ ...
1135+ >>> print(res.all())
1136+
1137+ Methods:
1138+ next: Get the next page of results. if there is no next page, it returns empty Result.
1139+ previous: Get the previous page of results. if there is no previous page, it returns empty Result.
1140+ all: Get all results from the current page, previous pages, and next pages.
1141+ empty: Returns an empty Result instance.
1142+
1143+ Properties:
1144+ has_next: Check if there is a next page of results.
1145+ has_previous: Check if there is a previous page of results.
1146+ before: Cursor that points to the start of the page of data that has been returned.
1147+ after: Cursor that points to the end of the page of data that has been returned.
11351148 """
11361149
11371150 def __init__ (
@@ -1169,18 +1182,38 @@ def after(self) -> str | None:
11691182 """Cursor that points to the end of the page of data that has been returned."""
11701183 return self ._cursors .get ("after" )
11711184
1172- def next (self ) -> Result [_T ] | None :
1173- """Get the next page of results."""
1185+ @property
1186+ def empty (self ) -> Result [_T ]:
1187+ """Returns an empty Result instance."""
1188+ return Result (
1189+ wa = self ._wa ,
1190+ response = {
1191+ "data" : [],
1192+ "paging" : {"next" : self ._next_url , "cursors" : self ._cursors },
1193+ },
1194+ item_factory = self ._item_factory ,
1195+ )
1196+
1197+ def next (self ) -> Result [_T ]:
1198+ """
1199+ Get the next page of results. if there is no next page, it returns empty Result.
1200+
1201+ - Check if there is a next page using the :attr:`~pywa.types.others.Result.has_next` property before calling this method.
1202+ """
11741203 if self .has_next :
11751204 # noinspection PyProtectedMember
11761205 response = self ._wa .api ._make_request (method = "GET" , endpoint = self ._next_url )
11771206 return Result (
11781207 wa = self ._wa , response = response , item_factory = self ._item_factory
11791208 )
1180- return None
1209+ return self .empty
1210+
1211+ def previous (self ) -> Result [_T ]:
1212+ """
1213+ Get the previous page of results. if there is no previous page, it returns empty Result.
11811214
1182- def previous ( self ) -> Result [ _T ] | None :
1183- """Get the previous page of results."""
1215+ - Check if there is a previous page using the :attr:`~pywa.types.others.Result.has_previous` property before calling this method.
1216+ """
11841217 if self .has_previous :
11851218 # noinspection PyProtectedMember
11861219 response = self ._wa .api ._make_request (
@@ -1189,22 +1222,53 @@ def previous(self) -> Result[_T] | None:
11891222 return Result (
11901223 wa = self ._wa , response = response , item_factory = self ._item_factory
11911224 )
1192- return None
1225+ return self .empty
1226+
1227+ def all (
1228+ self ,
1229+ * ,
1230+ sleep : float = 0.0 ,
1231+ ) -> list [_T ]:
1232+ """
1233+ Get all results from the current page, previous pages, and next pages.
1234+
1235+ - Make sure to provide higher limit in the ``Pagination`` parameter to avoid hitting rate limits.
1236+ - Also consider using the ``sleep`` parameter to avoid hitting rate limits.
1237+
1238+ Args:
1239+ sleep: The number of seconds to sleep between requests to avoid hitting rate limits. Default is 0.0 (no sleep).
1240+
1241+ Returns:
1242+ A list of all results from the current page, previous pages, and next pages.
1243+ """
1244+ before_data = []
1245+ after_data = []
1246+
1247+ prev = self
1248+ while prev .has_previous :
1249+ if sleep > 0 :
1250+ time .sleep (sleep )
1251+ prev = prev .previous ()
1252+ before_data = prev ._data + before_data
1253+
1254+ next_page = self
1255+ while next_page .has_next :
1256+ if sleep > 0 :
1257+ time .sleep (sleep )
1258+ next_page = next_page .next ()
1259+ after_data += next_page ._data
1260+
1261+ return before_data + self ._data + after_data
11931262
11941263 def __iter__ (self ) -> Iterator [_T ]:
1195- yield from self ._data
1264+ return iter ( self ._data )
11961265
11971266 def __len__ (self ) -> int :
11981267 return len (self ._data )
11991268
12001269 def __getitem__ (self , index : int ) -> _T :
12011270 return self ._data [index ]
12021271
1203- def __next__ (self ) -> _T :
1204- if self ._data :
1205- return self ._data .pop (0 )
1206- raise StopIteration
1207-
12081272 def __bool__ (self ) -> bool :
12091273 return bool (self ._data )
12101274
0 commit comments