Ë
    8ø»jƒ&  ã                  óæ  — d dl mZ d dlmZmZmZmZ d dlmZm	Z	m
Z
mZ erd dlmZmZmZmZ  ed«      ZdZ	  G d„ dee   «      Zd	d	d	d
œ	 	 	 	 	 	 	 	 	 dd„Zd	d	d	d
œ	 	 	 	 	 	 	 	 	 dd„Zed	d	d	dœ	 	 	 	 	 	 	 	 	 dd„«       Zed	d	d	dœ	 	 	 	 	 	 	 	 	 dd„«       Zd	d	d	dœ	 	 	 	 	 	 	 	 	 dd„Zed	d	d	dœ	 	 	 	 	 	 	 	 	 dd„«       Zed	d	d	dœ	 	 	 	 	 	 	 	 	 dd„«       Zd	d	d	dœ	 	 	 	 	 	 	 	 	 dd„Zdd„Zdd„Zy	) é    )Úannotations)ÚTYPE_CHECKINGÚProtocolÚTypeVarÚoverload)ÚKeyValueStoreKeyÚ
ListOfKeysÚListOfRequestsÚRequest)ÚAsyncIteratorÚ	AwaitableÚCallableÚIteratorÚTiè  c                  ó   — e Zd ZU dZded<   y)ÚHasItemsaã  Structural contract for a single page of results from a paginated API endpoint.

    Implementations must expose `items`. They may optionally expose `count` - the number of rows the API scanned to
    produce this page, which `len(items)` can land below (filters drop items) or above (`unwind` splits one row into
    several items). The iterator helpers consult `count` opportunistically via `getattr` for offset bookkeeping and
    fall back to `len(items)` when it is absent.
    zlist[T]ÚitemsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__© ó    úO/var/www/html/GAP/venv/lib/python3.12/site-packages/apify_client/_pagination.pyr   r      s   … ñð „Nr   r   N)ÚlimitÚoffsetÚ
chunk_sizec             #  óÌ   K  — |xs d}|xs d}|xs d}d}	 t        |||«      } | |||z   ¬«      }	|	j                  E d{  –—†  t        |	|«      }
||
z  }|
r|r||k\  ryŒJ7 Œ ­w)a  Yield individual items from offset-based paginated API responses.

    The `callback` is invoked lazily to fetch each page from the API. It must accept `limit` and `offset` keyword
    arguments and return an object whose `items` attribute is a list. If the object also exposes a `count` attribute, it
    is used for offset bookkeeping - `_page_scanned_rows` describes how the next offset is derived.

    Iteration stops when a page scans no rows or when the user-requested `limit` is reached. A page can scan rows while
    returning no items - filters like `clean` drop items from `items` but still count toward `count` - so terminating on
    scanned rather than returned rows keeps the iterator advancing across fully-filtered pages. The `total` field is
    intentionally not consulted, because it can change between calls.

    Args:
        callback: Function returning a single page of items.
        limit: Maximum total number of rows scanned across all pages. On the dataset items endpoint `unwind` can
            turn one row into several items, so more items than this can be yielded. `None` or `0` means no limit.
        offset: Starting offset for the first page.
        chunk_size: Per-page cap, sent to the API as its `limit`. `None` or `0` lets the API decide.
    r   ©r   r   N©Ú_next_page_limitr   Ú_page_scanned_rows)Úcallbackr   r   r   Úeffective_chunkÚinitial_offsetÚinitial_limitÚfetched_itemsÚ
page_limitÚcurrent_pageÚpage_scanneds              r   Úget_items_iteratorr,      sŽ   è ø€ ð2 !’o A€OØ’[˜q€NØ’J˜Q€MØ€Mà
Ü% m°]ÀOÓTˆ
ÙØØ! MÑ1ô
ˆð  ×%Ñ%×%Ð%ä)¨,¸
ÓCˆØ˜Ñ%ˆá¡°-À=Ò2PØð ð 	&ús   ‚?A$ÁA"Á!A$c              óà   K  — |xs d}|xs d}|xs d}d}	 t        |||«      } | |||z   ¬«      ƒ d{  –—† }	|	j                  D ]  }
|
­–— Œ	 t        |	|«      }||z  }|r|r||k\  ryŒT7 Œ6­w)z|Async variant of :func:`get_items_iterator`.

    The `callback` must be an awaitable returning a single page of items.
    r   r    Nr!   )r$   r   r   r   r%   r&   r'   r(   r)   r*   Úitemr+   s               r   Úget_items_iterator_asyncr/   L   s    è ø€ ð !’o A€OØ’[˜q€NØ’J˜Q€MØ€Mà
Ü% m°]ÀOÓTˆ
Ù%ØØ! MÑ1ô
÷ 
ˆð !×&Ñ&ò 	ˆDØŒJð	ô *¨,¸
ÓCˆØ˜Ñ%ˆá¡°-À=Ò2PØð ð
ús   ‚3A.µA,¶7A.)Úcursorr   r   c                ó   — y ©Nr   ©r$   r0   r   r   s       r   Úget_cursor_iteratorr4   l   s   € ð "%r   c                ó   — y r2   r   r3   s       r   r4   r4   t   s   € ð r   c             #  ó  K  — |xs d}|xs d}d}	  | t        |||«      |¬«      }|j                  E d{  –—†  |t        |j                  «      z  }t        |t        «      r|j
                  n|j                  }|�|r||k\  ryŒt7 ŒO­w)aÐ  Yield individual items from a cursor-paginated API response.

    This iterator supports the two API responses that use cursor pagination. `ListOfKeys` is used for key-value store
    keys, while `ListOfRequests` is used for request queue requests.

    Pagination continues until either:

    - the API returns no next cursor, or
    - the requested `limit` is reached.

    An empty page does not explicitly stop the iteration. In practice, both supported endpoints return a next cursor
    only when the current page contains items, so an empty page always has a `None` cursor and naturally ends the
    iteration.

    The endpoints determine the next cursor differently:

    - For key-value store keys, the cursor is the last key returned on the current page.
    - For request queue requests, a cursor is returned only when the current page is full.

    Args:
        callback: Function that returns one page of items and accepts `cursor` and `limit` keyword arguments.
        cursor: Cursor to use for the first request. If `None`, iteration starts from the beginning.
        limit: Maximum total number of items to yield across all pages.
        chunk_size: Maximum number of items to request in a single API call.
    r   ©r   r0   N©r"   r   ÚlenÚ
isinstancer	   Únext_exclusive_start_keyÚnext_cursor)r$   r0   r   r   r%   r'   r(   r*   s           r   r4   r4   |   sŸ   è ø€ ð@ !’o A€OØ’J˜Q€MØ€Mà
ÙÜ" =°-ÀÓQØô
ˆð  ×%Ñ%×%Ð%àœ˜\×/Ñ/Ó0Ñ0ˆä5?ÀÌjÔ5YˆL×1Ò1Ð_k×_wÑ_wð 	ð ˆ>™m°ÀÒ0NØð ð
 	&ús   ‚4B¶B·ABc                ó   — y r2   r   r3   s       r   Úget_cursor_iterator_asyncr>   °   s   € ð '*r   c                ó   — y r2   r   r3   s       r   r>   r>   ¸   s   € ð !r   c              ó(  K  — |xs d}|xs d}d}	  | t        |||«      |¬«      ƒ d{  –—† }|j                  D ]  }|­–— Œ	 |t        |j                  «      z  }t        |t        «      r|j
                  n|j                  }|�|r||k\  ryŒ~7 Œe­w)z-Async variant of :func:`get_cursor_iterator`.r   r7   Nr8   )	r$   r0   r   r   r%   r'   r(   r*   r.   s	            r   r>   r>   À   s°   è ø€ ð !’o A€OØ’J˜Q€MØ€Mà
Ù%Ü" =°-ÀÓQØô
÷ 
ˆð !×&Ñ&ò 	ˆDØŒJð	ð 	œ˜\×/Ñ/Ó0Ñ0ˆä5?ÀÌjÔ5YˆL×1Ò1Ð_k×_wÑ_wð 	ð ˆ>™m°ÀÒ0NØð ð
ús   ‚(BªB«A&Bc                ó4   — | s|S | |z
  }|s|S t        ||«      S )a%  Compute the `limit` value for the next API call.

    `0` means no limit on the wire (matches the Apify API contract). When both an overall `initial_limit` and a per-page
    `effective_chunk` are set, the call is clamped to whichever is smaller; if either is unset (`0`), the other wins.
    )Úmin)r'   r(   r%   Ú	remainings       r   r"   r"   Ý   s-   € ñ ØÐØ Ñ-€IÙØÐÜˆy˜/Ó*Ð*r   c                óv   — t        t        | dd«      t        | j                  «      «      }|rt	        ||«      S |S )aÌ  Compute how far the offset advances past `page`, in dataset rows.

    Neither reported number is right on its own. `count` follows the rows the API scanned, but it is derived from a
    dataset's item count, which is incremented by a throttled write and so lags a fresh push. `len(items)` counts the
    items the API shaped out of those rows: filters (`clean`, `skip_empty`, `skip_hidden`) drop some, and `unwind`
    splits one row into several. The larger of the two absorbs a `count` that lags behind the items returned, and
    capping it at the rows the call asked for keeps an unwound page from advancing past rows the next call would then
    never read. The cap is a valid bound because the endpoint applies the `limit` it is sent verbatim; on a page
    covering fewer rows than that, the advance can still overshoot into rows a concurrent push appends afterwards. A
    `requested_limit` of `0` means the call sent no limit, leaving the advance unbounded.
    Úcountr   )ÚmaxÚgetattrr9   r   rB   )ÚpageÚrequested_limitÚscanned_rowss      r   r#   r#   ë   s5   € ô ”w˜t W¨aÓ0´#°d·j±j³/ÓB€LÙ1@Œ3ˆ|˜_Ó-ÐRÀlÐRr   )
r$   zCallable[..., HasItems[T]]r   ú
int | Noner   rK   r   rK   ÚreturnzIterator[T])
r$   z%Callable[..., Awaitable[HasItems[T]]]r   rK   r   rK   r   rK   rL   zAsyncIterator[T])
r$   zCallable[..., ListOfKeys]r0   ú
str | Noner   rK   r   rK   rL   zIterator[KeyValueStoreKey])
r$   zCallable[..., ListOfRequests]r0   rM   r   rK   r   rK   rL   zIterator[Request])
r$   z*Callable[..., ListOfKeys | ListOfRequests]r0   rM   r   rK   r   rK   rL   z.Iterator[KeyValueStoreKey] | Iterator[Request])
r$   z$Callable[..., Awaitable[ListOfKeys]]r0   rM   r   rK   r   rK   rL   zAsyncIterator[KeyValueStoreKey])
r$   z(Callable[..., Awaitable[ListOfRequests]]r0   rM   r   rK   r   rK   rL   zAsyncIterator[Request])
r$   z5Callable[..., Awaitable[ListOfKeys | ListOfRequests]]r0   rM   r   rK   r   rK   rL   z8AsyncIterator[KeyValueStoreKey] | AsyncIterator[Request])r'   Úintr(   rN   r%   rN   rL   rN   )rH   zHasItems[T]rI   rN   rL   rN   )Ú
__future__r   Útypingr   r   r   r   Úapify_client._modelsr   r	   r
   r   Úcollections.abcr   r   r   r   r   ÚDEFAULT_CHUNK_SIZEr   r,   r/   r4   r>   r"   r#   r   r   r   ú<module>rT      s‹  ðÝ "ç =Ó =ç VÓ VáßLÓLáˆCƒL€àÐ ðô	ˆx˜‰{ô 	ð ØØ!ñ*Ø(ð*ð ð*ð ð	*ð
 ð*ð ó*ð` ØØ!ñØ3ðð ðð ð	ð
 ðð óð@ 
ð ØØ!ñ%Ø'ð%ð ð%ð ð	%ð
 ð%ð  ò%ó 
ð%ð 
ð ØØ!ñØ+ðð ðð ð	ð
 ðð òó 
ðð ØØ!ñ1Ø8ð1ð ð1ð ð	1ð
 ð1ð 4ó1ðh 
ð ØØ!ñ*Ø2ð*ð ð*ð ð	*ð
 ð*ð %ò*ó 
ð*ð 
ð ØØ!ñ!Ø6ð!ð ð!ð ð	!ð
 ð!ð ò!ó 
ð!ð ØØ!ñØCðð ðð ð	ð
 ðð >óó:+ôSr   