o
    ˷e%                     @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZmZ dd	lmZ G d
d deZdS )zHClasses for representing collections for the Google Cloud Firestore API.    )gapic_v1)retry)BaseCollectionReference_item_to_document_ref)async_queryasync_document)DocumentReference)AsyncIterator)AnyAsyncGeneratorTuple)Transactionc                       s  e Zd ZdZd fddZdejfddZdefd	d
Z	de
jjdfdededejdedeeef f
ddZ	ddedejf fddZde
jjdfdedejdedeedf fddZde
jjdfdedejdedefddZde
jjdfdedejdedeej fddZ   Z!S )AsyncCollectionReferencea  A reference to a collection in a Firestore database.

    The collection may already exist or this class can facilitate creation
    of documents within the collection.

    Args:
        path (Tuple[str, ...]): The components in the collection path.
            This is a series of strings representing each collection and
            sub-collection ID, as well as the document IDs for any documents
            that contain a sub-collection.
        kwargs (dict): The keyword arguments for the constructor. The only
            supported keyword is ``client`` and it must be a
            :class:`~google.cloud.firestore_v1.client.Client` if provided. It
            represents the client that created this collection reference.

    Raises:
        ValueError: if

            * the ``path`` is empty
            * there are an even number of elements
            * a collection ID in ``path`` is not a string
            * a document ID in ``path`` is not a string
        TypeError: If a keyword other than ``client`` is used.
    returnNc                    s   t t| j|i | d S N)superr   __init__)selfpathkwargs	__class__ a/var/www/ideatree/venv/lib/python3.10/site-packages/google/cloud/firestore_v1/async_collection.pyr   @   s   z!AsyncCollectionReference.__init__c                 C   s
   t | S )zeQuery factory.

        Returns:
            :class:`~google.cloud.firestore_v1.query.Query`
        )r   
AsyncQuery)r   r   r   r   _queryC   s   
zAsyncCollectionReference._query
chunk_sizec                 C  s*   |   |2 z	3 d H W }|V  q6 d S r   )r   	_chunkify)r   r   pager   r   r   r   K   s   z"AsyncCollectionReference._chunkifydocument_datadocument_idr   timeoutc                    s8   |  ||||\}}|j|fi |I dH }|j|fS )a   Create a document in the Firestore database with the provided data.

        Args:
            document_data (dict): Property names and values to use for
                creating the document.
            document_id (Optional[str]): The document identifier within the
                current collection. If not provided, an ID will be
                automatically assigned by the server (the assigned ID will be
                a random 20 character string composed of digits,
                uppercase and lowercase letters).
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.  Defaults to a system-specified policy.
            timeout (float): The timeout for this request.  Defaults to a
                system-specified value.

        Returns:
            Tuple[:class:`google.protobuf.timestamp_pb2.Timestamp`,                 :class:`~google.cloud.firestore_v1.async_document.AsyncDocumentReference`]:
                Pair of

                * The ``update_time`` when the document was created/overwritten.
                * A document reference for the created document.

        Raises:
            :class:`google.cloud.exceptions.Conflict`:
                If ``document_id`` is provided and the document already exists.
        N)	_prep_addcreateupdate_time)r   r   r    r   r!   document_refr   write_resultr   r   r   addO   s   "
zAsyncCollectionReference.addc                    s   t t| |S )a  Create a sub-document underneath the current collection.

        Args:
            document_id (Optional[str]): The document identifier
                within the current collection. If not provided, will default
                to a random 20 character string composed of digits,
                uppercase and lowercase and letters.

        Returns:
            :class:`~google.cloud.firestore_v1.document.async_document.AsyncDocumentReference`:
            The child document.
        )r   r   document)r   r    r   r   r   r(   z   s   z!AsyncCollectionReference.document	page_sizec                 C  s\   |  |||\}}| jjjd|| jjd|I dH }|2 z3 dH W }t| |V  q6 dS )a`  List all subdocuments of the current collection.

        Args:
            page_size (Optional[int]]): The maximum number of documents
                in each page of results from this request. Non-positive values
                are ignored. Defaults to a sensible value set by the API.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.  Defaults to a system-specified policy.
            timeout (float): The timeout for this request.  Defaults to a
                system-specified value.

        Returns:
            Sequence[:class:`~google.cloud.firestore_v1.collection.DocumentReference`]:
                iterator of subdocuments of the current collection. If the
                collection does not exist at the time of `snapshot`, the
                iterator will be empty
        )requestmetadataNr   )_prep_list_documents_client_firestore_apilist_documents_rpc_metadatar   )r   r)   r   r!   r*   r   iteratorir   r   r   r/      s   
z'AsyncCollectionReference.list_documentstransactionc                    s,   |  ||\}}|jdd|i|I dH S )a  Read the documents in this collection.

        This sends a ``RunQuery`` RPC and returns a list of documents
        returned in the stream of ``RunQueryResponse`` messages.

        Args:
            transaction
                (Optional[:class:`~google.cloud.firestore_v1.transaction.Transaction`]):
                An existing transaction that this query will run in.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.  Defaults to a system-specified policy.
            timeout (float): The timeout for this request.  Defaults to a
                system-specified value.

        If a ``transaction`` is used and it already has write operations
        added, this method cannot be used (i.e. read-after-write is not
        allowed).

        Returns:
            list: The documents in this collection that match the query.
        r3   Nr   )_prep_get_or_streamget)r   r3   r   r!   queryr   r   r   r   r5      s   zAsyncCollectionReference.getc                 C  s@   |  ||\}}|jdd|i|2 z	3 dH W }|V  q6 dS )a  Read the documents in this collection.

        This sends a ``RunQuery`` RPC and then returns an iterator which
        consumes each document returned in the stream of ``RunQueryResponse``
        messages.

        .. note::

           The underlying stream of responses will time out after
           the ``max_rpc_timeout_millis`` value set in the GAPIC
           client configuration for the ``RunQuery`` API.  Snapshots
           not consumed from the iterator before that point will be lost.

        If a ``transaction`` is used and it already has write operations
        added, this method cannot be used (i.e. read-after-write is not
        allowed).

        Args:
            transaction (Optional[:class:`~google.cloud.firestore_v1.transaction.                Transaction`]):
                An existing transaction that the query will run in.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.  Defaults to a system-specified policy.
            timeout (float): The timeout for this request.  Defaults to a
                system-specified value.

        Yields:
            :class:`~google.cloud.firestore_v1.document.DocumentSnapshot`:
            The next document that fulfills the query.
        r3   Nr   )r4   stream)r   r3   r   r!   r6   r   dr   r   r   r7      s
   $ zAsyncCollectionReference.stream)r   Nr   )"__name__
__module____qualname____doc__r   r   r   r   intr   r   methodDEFAULTdictstrretriesRetryfloatr   r
   r'   r   AsyncDocumentReferencer(   r   r   r/   r   listr5   r	   DocumentSnapshotr7   __classcell__r   r   r   r   r   &   sz    

,

#
!r   N)r<   google.api_corer   r   rB   )google.cloud.firestore_v1.base_collectionr   r   google.cloud.firestore_v1r   r   "google.cloud.firestore_v1.documentr   typingr	   r
   r   r   %google.cloud.firestore_v1.transactionr   r   r   r   r   r   <module>   s   