o
    ×èFh¼  ã                   @  st   d Z ddlmZ ddl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mZmZmZ G dd	„ d	e
ƒZd
S )aU  Classes for representing aggregation queries for the Google Cloud Firestore API.

A :class:`~google.cloud.firestore_v1.aggregation.AggregationQuery` can be created directly from
a :class:`~google.cloud.firestore_v1.collection.Collection` and that can be
a more common way to create an aggregation query than direct usage of the constructor.
é    )Úannotations)Ú
exceptions)Úgapic_v1)Úretry)ÚAggregationResultÚBaseAggregationQueryÚ_query_response_to_result)Ú	GeneratorÚUnionÚListÚAnyc                      s^   e Zd ZdZd‡ fdd„Zdejjdfddd„Zdd„ Z	dd„ Z
dejjdfddd„Z‡  ZS )ÚAggregationQueryz5Represents an aggregation query to the Firestore API.ÚreturnÚNonec                   s   t t| ƒ |¡ d S )N)Úsuperr   Ú__init__)ÚselfÚnested_query©Ú	__class__© ú^/var/www/html/loop/nvenv/lib/python3.10/site-packages/google/cloud/firestore_v1/aggregation.pyr   (   s   zAggregationQuery.__init__Nr   ú:Union[retries.Retry, None, gapic_v1.method._MethodDefault]Útimeoutúfloat | NoneúList[AggregationResult]c                 C  s   | j |||d�}t|ƒS )a�  Runs the aggregation query.

        This sends a ``RunAggregationQuery`` RPC and returns a list of aggregation results in the stream of ``RunAggregationQueryResponse`` messages.

        Args:
            transaction
                (Optional[:class:`~google.cloud.firestore_v1.transaction.Transaction`]):
                An existing transaction that this query will run in.
                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).
            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:
            list: The aggregation query results

        )Útransactionr   r   )ÚstreamÚlist)r   r   r   r   Úresultr   r   r   Úget.   s   zAggregationQuery.getc                 C  s0   |   |||¡\}}| jjjd|| jjdœ|¤ŽS )ú!Helper method for :meth:`stream`.)ÚrequestÚmetadataNr   )Ú_prep_streamÚ_clientÚ_firestore_apiÚrun_aggregation_queryÚ_rpc_metadata)r   r   r   r   r"   Úkwargsr   r   r   Ú_get_stream_iteratorM   s   ý
þýz%AggregationQuery._get_stream_iteratorc                 C  s8   |du r|t jju r| jjj}|j}|j}| |¡S dS )r!   NF)	r   ÚmethodÚDEFAULTr%   r&   Ú
_transportr'   Ú_retryÚ
_predicate)r   Úexcr   r   Ú	transportÚgapic_callabler   r   r   Ú_retry_query_after_exception[   s   

z-AggregationQuery._retry_query_after_exceptionú4Union[Generator[List[AggregationResult], Any, None]]c              
   c  s‚   � |   |||¡}	 zt|dƒ}W n" tjy2 } z|  |||¡r-|   |||¡}W Y d}~q‚ d}~ww |du r9dS t|ƒ}|V  q	)aù  Runs the aggregation query.

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

        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 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.

        Yields:
            :class:`~google.cloud.firestore_v1.base_aggregation.AggregationResult`:
            The result of aggregations of this query
        TN)r*   Únextr   ÚGoogleAPICallErrorr3   r   )r   r   r   r   Úresponse_iteratorÚresponser0   r   r   r   r   r   f   s2   € ýý€÷ïzAggregationQuery.stream)r   r   )r   r   r   r   r   r   )r   r   r   r   r   r4   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r+   r,   r    r*   r3   r   Ú__classcell__r   r   r   r   r   %   s    úúr   N)r<   Ú
__future__r   Úgoogle.api_corer   r   r   ÚretriesÚ*google.cloud.firestore_v1.base_aggregationr   r   r   Útypingr	   r
   r   r   r   r   r   r   r   Ú<module>   s   