o
    Yi                     @   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
mZ ddlmZ ddl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Union   )_common)typesT)Z
batch_jobsmodelsZtuning_jobsfilesZcached_contentsZfile_search_storesZ	documentsc                
   @   s   e Zd ZdZdededef dededdf
d	d
Zdeded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jdf fddZe	dej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configreturnNc                 C   s   || _ || _t|| j pg | _d| _t|dd | _|si }nt|tr)t	|}nt|}t|d|d< || _
|dt| j| _d S )Nr   sdk_http_responseZnext_page_token
page_token	page_size)_name_requestgetattr_page_idx_sdk_http_response
isinstancedictcopydeepcopy_configgetlen
_page_size)selfr   r   r   r   Zrequest_config r)   M/home/kim/smarthome/.venv/lib/python3.10/site-packages/google/genai/pagers.py
_init_page)   s   
z_BasePager._init_pagec                 C   s   |  |||| d S N)r+   r(   r   r   r   r   r)   r)   r*   __init__C   s   z_BasePager.__init__c                 C      | j S )a_  Returns a subset of the entire list of items.

    For the number of items returned, see `pageSize()`.

    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*   pageL   s   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   r0   r)   r)   r*   r   ]      z_BasePager.namec                 C   r/   )a  Returns the maximum number of items fetched by the pager at one time.

    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'   r0   r)   r)   r*   r   l   r2   z_BasePager.page_sizec                 C   r/   )z.Returns the http response of the API response.)r   r0   r)   r)   r*   r   {   s   z_BasePager.sdk_http_responsec                 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$   r0   r)   r)   r*   r      s   z_BasePager.configc                 C   s
   t | jS )z6Returns the total number of items in the current page.)r&   r1   r0   r)   r)   r*   __len__      
z_BasePager.__len__indexc                 C   s
   | j | S )z$Returns the item at the given index.)r1   )r(   r5   r)   r)   r*   __getitem__   r4   z_BasePager.__getitem__c                 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+   r.   propertylistr   r1   r   intr   r
   r   ZHttpResponser   r   Z
StringDictr   r3   r6   r8   r)   r)   r)   r*   r   &   sF    



	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   r&   	next_page
IndexErrorStopIterationr1   r(   itemr)   r)   r*   __next__   s   zPager.__next__c                 C   
   d| _ | S )z#Returns an iterator over the items.r   r   r0   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%   rC   r   r8   r1   r7   r)   r)   r*   rB      s
   
zPager.next_pageN)
r9   r:   r;   r<   r   rG   r   rJ   r?   rB   r)   r)   r)   r*   rA      s
    rA   c                       sp   e Zd ZdZdededee f dedef fddZd	e	e
 fd
dZd	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 r,   )superr.   r-   	__class__r)   r*   r.      s   zAsyncPager.__init__r   c                 C   rH   )z)Returns an async iterator over the items.r   rI   r0   r)   r)   r*   	__aiter__   rK   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&   rB   rC   StopAsyncIterationr1   rE   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   rL   rM   NrN   r7   r)   r)   r*   rB      s   
zAsyncPager.next_page)r9   r:   r;   r<   r=   r   r   r   r.   r   r   rS   rU   r?   rB   __classcell__r)   r)   rQ   r*   rO      s    	rO   )r<   r"   typingr   r   r   r   r   r   r   r	   r
    r   r   r   r=   r   rA   rO   r)   r)   r)   r*   <module>   s   , +