o
    0Xxi                     @   s   d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
 e
dZe	d ZG dd dee ZG dd	 d	ee ZG d
d dee ZdS )zPagers for the GenAI List APIs.    N)AnyAsyncIterator	AwaitableCallableGenericIteratorLiteralTypeVarT)
batch_jobsmodelstuning_jobsfilescached_contentsc                   @   s   e Zd ZdZdedeeef dedefddZede	e
 fd	d
ZedefddZedefddZedeeef fddZdefddZdede
fddZdeddfddZdS )
_BasePagerz9Base pager class for iterating through paginated results.namerequestresponseconfigc                 C   sx   || _ || _t|| j pg | _d| _|si }nt|tr"t|}nt|}t|d|d< || _	|
dt| j| _d S )Nr   next_page_token
page_token	page_size)_name_requestgetattr_page_idx
isinstancedictcopydeepcopy_configgetlen
_page_size)selfr   r   r   r   request_config r'   R/home/air/biblejyuku/back/venv/lib/python3.10/site-packages/google/genai/pagers.py__init__!   s   
z_BasePager.__init__returnc                 C      | j S )al  Returns the current page, which is a list of items.

    The returned list of items is a subset of the entire list.

    Usage:

    .. code-block:: python

      batch_jobs_pager = client.batches.list(config={'page_size': 5})
      print(f"first page: {batch_jobs_pager.page}")
      # first page: [BatchJob(name='projects/./locations/./batchPredictionJobs/1
    )r   r%   r'   r'   r(   page9      z_BasePager.pagec                 C   r+   )zReturns the type of paged item (for example, ``batch_jobs``).

    Usage:

    .. code-block:: python

      batch_jobs_pager = client.batches.list(config={'page_size': 5})
      print(f"name: {batch_jobs_pager.name}")
      # name: batch_jobs
    )r   r,   r'   r'   r(   r   J   s   z_BasePager.namec                 C   r+   )aO  Returns the length of the page fetched each time by this pager.

    The number of items in the page is less than or equal to the page length.

    Usage:

    .. code-block:: python

      batch_jobs_pager = client.batches.list(config={'page_size': 5})
      print(f"page_size: {batch_jobs_pager.page_size}")
      # page_size: 5
    )r$   r,   r'   r'   r(   r   Y   r.   z_BasePager.page_sizec                 C   r+   )a  Returns the configuration when making the API request for the next page.

    A configuration is a set of optional parameters and arguments that can be
    used to customize the API request. For example, the ``page_token`` parameter
    contains the token to request the next page.

    Usage:

    .. code-block:: python

      batch_jobs_pager = client.batches.list(config={'page_size': 5})
      print(f"config: {batch_jobs_pager.config}")
      # config: {'page_size': 5, 'page_token': 'AMEw9yO5jnsGnZJLHSKDFHJJu'}
    )r!   r,   r'   r'   r(   r   j   s   z_BasePager.configc                 C   s
   t | jS )z6Returns the total number of items in the current page.)r#   r-   r,   r'   r'   r(   __len__}      
z_BasePager.__len__indexc                 C   s
   | j | S )z$Returns the item at the given index.)r-   )r%   r1   r'   r'   r(   __getitem__   r0   z_BasePager.__getitem__Nc                 C   s   |  | j| j|| j dS )zInitializes the next page from the response.

    This is an internal method that should be called by subclasses after
    fetching the next page.

    Args:
      response: The response object from the API request.
    N)r)   r   r   r   r%   r   r'   r'   r(   _init_next_page   s   	z_BasePager._init_next_page)__name__
__module____qualname____doc__	PagedItemr   r   r)   propertylistr
   r-   strr   intr   r   r   r/   r2   r4   r'   r'   r'   r(   r      s,    

r   c                   @   sB   e Zd ZdZdefddZdee fddZdee fddZ	d	S )
Pagerz4Pager class for iterating through paginated results.r*   c                 C   sL   | j t| krz|   W n	 ty   tw | j| j  }|  j d7  _ |S )zReturns the next item.   )r   r#   	next_page
IndexErrorStopIterationr-   r%   itemr'   r'   r(   __next__   s   zPager.__next__c                 C   
   d| _ | S )z#Returns an iterator over the items.r   r   r,   r'   r'   r(   __iter__      zPager.__iter__c                 C   s2   | j ds
td| j| j d}| | | jS )a  Fetches the next page of items. This makes a new API request.

    Usage:

    .. code-block:: python

      batch_jobs_pager = client.batches.list(config={'page_size': 5})
      print(f"current page: {batch_jobs_pager.page}")
      batch_jobs_pager.next_page()
      print(f"next page: {batch_jobs_pager.page}")
      # current page: [BatchJob(name='projects/.../batchPredictionJobs/1
      # next page: [BatchJob(name='projects/.../batchPredictionJobs/6
    r   No more pages to fetch.r   r   r"   rA   r   r4   r-   r3   r'   r'   r(   r@      s
   
zPager.next_pageN)
r5   r6   r7   r8   r
   rE   r   rH   r;   r@   r'   r'   r'   r(   r>      s
    r>   c                       st   e Zd ZdZdedeeee f dedef fddZde	e
 fd	d
Zdee
 fddZdee
 fddZ  ZS )
AsyncPagerz9AsyncPager class for iterating through paginated results.r   r   r   r   c                    s   t  |||| d S )N)superr)   )r%   r   r   r   r   	__class__r'   r(   r)      s   zAsyncPager.__init__r*   c                 C   rF   )z)Returns an async iterator over the items.r   rG   r,   r'   r'   r(   	__aiter__   rI   zAsyncPager.__aiter__c                    sT   | j t| krz	|  I dH  W n	 ty   tw | j| j  }|  j d7  _ |S )z%Returns the next item asynchronously.Nr?   )r   r#   r@   rA   StopAsyncIterationr-   rC   r'   r'   r(   	__anext__   s   zAsyncPager.__anext__c                    s:   | j dstd| j| j dI dH }| | | jS )aS  Fetches the next page of items asynchronously.

    This makes a new API request.

    Returns:
      The next page of items.

    Raises:
      IndexError: No more pages to fetch.

    Usage:

    .. code-block:: python

      batch_jobs_pager = await client.aio.batches.list(config={'page_size': 5})
      print(f"current page: {batch_jobs_pager.page}")
      await batch_jobs_pager.next_page()
      print(f"next page: {batch_jobs_pager.page}")
      # current page: [BatchJob(name='projects/.../batchPredictionJobs/1
      # next page: [BatchJob(name='projects/.../batchPredictionJobs/6
    r   rJ   rK   NrL   r3   r'   r'   r(   r@      s   
zAsyncPager.next_page)r5   r6   r7   r8   r9   r   r   r   r)   r   r
   rQ   rS   r;   r@   __classcell__r'   r'   rO   r(   rM      s    	rM   )r8   r   typingr   r   r   r   r   r   r   r	   r
   r9   r   r>   rM   r'   r'   r'   r(   <module>   s   (s+