a
    !f                     @  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                      sr   e Zd ZdZdd fddZdejjdfddd	d
ddZdd Z	dd Z
dejjdfdddd
ddZ  ZS )AggregationQueryz5Represents an aggregation query to the Firestore API.None)returnc                   s   t t| | d S )N)superr   __init__)selfZnested_query	__class__ f/var/www/html/python-backend/venv/lib/python3.9/site-packages/google/cloud/firestore_v1/aggregation.pyr   (   s    zAggregationQuery.__init__Nz:Union[retries.Retry, None, gapic_v1.method._MethodDefault]zfloat | NonezList[AggregationResult])r   timeoutr   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f || jjd|S )!Helper method for :meth:`stream`.)requestmetadata)Z_prep_stream_client_firestore_apirun_aggregation_queryZ_rpc_metadata)r   r   r   r   r   kwargsr   r   r   _get_stream_iteratorM   s    
z%AggregationQuery._get_stream_iteratorc                 C  s8   |du r4|t jju r*| jjj}|j}|j}||S dS )r   NF)	r   methodDEFAULTr    r!   
_transportr"   Z_retryZ
_predicate)r   excr   r   	transportZgapic_callabler   r   r   _retry_query_after_exception[   s    

z-AggregationQuery._retry_query_after_exceptionz4Union[Generator[List[AggregationResult], Any, None]]c              
   c  s   |  |||}zt|d}W nR tjyn } z8| |||rX|  |||}W Y d}~qn W Y d}~n
d}~0 0 |du rzqt|}|V  qdS )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
        N)r$   nextr   ZGoogleAPICallErrorr*   r   )r   r   r   r   Zresponse_iteratorresponser(   r   r   r   r   r   f   s(     zAggregationQuery.stream)__name__
__module____qualname____doc__r   r   r%   r&   r   r$   r*   r   __classcell__r   r   r   r   r   %   s   r   N)r0   
__future__r   Zgoogle.api_corer   r   r   retriesZ*google.cloud.firestore_v1.base_aggregationr   r   r   typingr	   r
   r   r   r   r   r   r   r   <module>   s   