o
    /in                     @   sp   d Z ddlZddl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 G dd	 d	ejZdd
dZdS )aB  AsyncIO futures for long-running operations returned from Google Cloud APIs.

These futures can be used to await for the result of a long-running operation
using :meth:`AsyncOperation.result`:


.. code-block:: python

    operation = my_api_client.long_running_method()
    result = await operation.result()

Or asynchronously using callbacks and :meth:`Operation.add_done_callback`:

.. code-block:: python

    operation = my_api_client.long_running_method()

    def my_callback(future):
        result = await future.result()

    operation.add_done_callback(my_callback)

    N)
exceptions)protobuf_helpers)async_future)operations_pb2)code_pb2c                       s   e Zd ZdZdejf fdd	Zedd Zedd Z	e
d	d
 Zdd ZejfddZejfddZdd Zdd Z  ZS )AsyncOperationa  A Future for interacting with a Google API Long-Running Operation.

    Args:
        operation (google.longrunning.operations_pb2.Operation): The
            initial operation.
        refresh (Callable[[], ~.api_core.operation.Operation]): A callable that
            returns the latest state of the operation.
        cancel (Callable[[], None]): A callable that tries to cancel
            the operation.
        result_type (func:`type`): The protobuf type for the operation's
            result.
        metadata_type (func:`type`): The protobuf type for the operation's
            metadata.
        retry (google.api_core.retry.Retry): The retry configuration used
            when polling. This can be used to control how often :meth:`done`
            is polled. Regardless of the retry's ``deadline``, it will be
            overridden by the ``timeout`` argument to :meth:`result`.
    Nc                    sB   t  j|d || _|| _|| _|| _|| _t | _	| 
  d S )Nretry)super__init__
_operation_refresh_cancel_result_type_metadata_type	threadingLock_completion_lock_set_result_from_operation)self	operationrefreshcancelresult_typemetadata_typer	   	__class__ S/var/www/passon-env/lib/python3.10/site-packages/google/api_core/operation_async.pyr   E   s   	
zAsyncOperation.__init__c                 C   s   | j S )zAgoogle.longrunning.Operation: The current long-running operation.)r   r   r   r   r   r   X   s   zAsyncOperation.operationc                 C   s"   | j dsdS t| j| j jS )z8google.protobuf.Message: the current operation metadata.metadataN)r   HasFieldr   from_any_pbr   r    r   r   r   r   r    ]   s
   
zAsyncOperation.metadatac                 C   s   t j|S )zDeserialize a ``google.longrunning.Operation`` protocol buffer.

        Args:
            payload (bytes): A serialized operation protocol buffer.

        Returns:
            ~.operations_pb2.Operation: An Operation protobuf object.
        )r   	Operation
FromString)clspayloadr   r   r   deserializeg   s   
zAsyncOperation.deserializec                 C   s   | j e | jjr| j r	 W d   dS | jdr+t| j| jj}| 	| n-| jdrFt
j| jjj| jjf| jd}| | nt
d}| | W d   dS W d   dS W d   dS 1 skw   Y  dS )zASet the result or exception from the operation if it is complete.Nresponseerror)errorsr(   zLUnexpected state: Long-running operation had neither response nor error set.)r   r   done_futurer!   r   r"   r   r(   
set_resultr   GoogleAPICallErrorr)   messageset_exception)r   r(   	exceptionr   r   r   r   s   s2   

"z)AsyncOperation._set_result_from_operationc                    s.   | j js| j|dI dH | _ |   dS dS )zRefresh the operation and update the result if needed.

        Args:
            retry (google.api_core.retry.Retry): (Optional) How to retry the RPC.
        r   N)r   r+   r   r   r   r	   r   r   r   _refresh_and_update   s
   z"AsyncOperation._refresh_and_updatec                    s   |  |I dH  | jjS )zChecks to see if the operation is complete.

        Args:
            retry (google.api_core.retry.Retry): (Optional) How to retry the RPC.

        Returns:
            bool: True if the operation is complete, False otherwise.
        N)r3   r   r+   r2   r   r   r   r+      s   	zAsyncOperation.donec                    s*   |   I dH }|rdS |  I dH  dS )zAttempt to cancel the operation.

        Returns:
            bool: True if the cancel RPC was made, False if the operation is
                already complete.
        NFT)r+   r   )r   resultr   r   r   r      s   zAsyncOperation.cancelc                    s,   |   I dH  | jdo| jjjtjkS )z$True if the operation was cancelled.Nr)   )r3   r   r!   r)   coder   	CANCELLEDr   r   r   r   	cancelled   s
   zAsyncOperation.cancelled)__name__
__module____qualname____doc__r   DEFAULT_RETRYr   propertyr   r    classmethodr'   r   r3   r+   r   r7   __classcell__r   r   r   r   r   1   s     

	
r   c                 K   s>   t j|j| j|d}t j|j| j|d}t| |||fi |S )a  Create an operation future from a gapic client.

    This interacts with the long-running operations `service`_ (specific
    to a given API) via a gapic client.

    .. _service: https://github.com/googleapis/googleapis/blob/                 050400df0fdb16f63b63e9dee53819044bffc857/                 google/longrunning/operations.proto#L38

    Args:
        operation (google.longrunning.operations_pb2.Operation): The operation.
        operations_client (google.api_core.operations_v1.OperationsClient):
            The operations client.
        result_type (:func:`type`): The protobuf result type.
        grpc_metadata (Optional[List[Tuple[str, str]]]): Additional metadata to pass
            to the rpc.
        kwargs: Keyword args passed into the :class:`Operation` constructor.

    Returns:
        ~.api_core.operation.Operation: The operation future to track the given
            operation.
    )r    )	functoolspartialget_operationnamecancel_operationr   )r   operations_clientr   grpc_metadatakwargsr   r   r   r   r   
from_gapic   s   rH   )N)r;   r@   r   google.api_corer   r   google.api_core.futurer   google.longrunningr   
google.rpcr   AsyncFuturer   rH   r   r   r   r   <module>   s    