Skip to content

Commit 879d326

Browse files
committed
Document HTTPAsyncIterator
1 parent a64ae90 commit 879d326

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

docs/references/utils.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ HTTP
4747

4848
.. autoclass:: twitchio.Route()
4949

50+
.. autoclass:: twitchio.HTTPAsyncIterator()

twitchio/http.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,50 @@ def update_headers(self, headers: dict[str, str]) -> None:
299299

300300

301301
class HTTPAsyncIterator(Generic[T]):
302+
"""TwitchIO async iterator for HTTP requests.
303+
304+
When a method or function returns this iterator you should call the returning function in one of the following two ways:
305+
306+
``await method(...)``
307+
308+
**or**
309+
310+
``async for item in method(...)``
311+
312+
When awaited the iterator will return a flattened list of all the items returned via the request for the first page only.
313+
If the endpoint is paginated, it is preferred you use ``async for item in method(...)`` in a list comprehension.
314+
315+
When used with ``async for`` the iterator will return the next item available until no items remain or you break from the
316+
loop manually, E.g. with ``break`` or ``return`` etc.
317+
318+
``async for item in method(...)`` will continue making requests on paginated endpoints to the next page as needed
319+
and when available.
320+
321+
You can create a flattened list of all pages with a list comprehension.
322+
323+
Examples
324+
--------
325+
326+
.. code-block:: python3
327+
328+
# Flatten and return first page (20 results)
329+
streams = await bot.fetch_streams()
330+
331+
# Flatten and return up to 1000 results (max 100 per page) which equates to 10 requests...
332+
streams = [async for stream in bot.fetch_streams(first=100, max_results=1000)]
333+
334+
# Loop over results until we manually stop...
335+
async for item in bot.fetch_streams(first=100, max_results=1000):
336+
# Some logic...
337+
...
338+
break
339+
340+
341+
.. important::
342+
343+
Everything in this class is private internals, and should not be modified.
344+
"""
345+
302346
__slots__ = (
303347
"_buffer",
304348
"_converter",

0 commit comments

Comments
 (0)