o
    ˷eU2                     @   s   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 ddlmZ G d	d
 d
ejZdddZdd Zdd ZdddZdd ZdddZdddZdS )a6  Futures for long-running operations returned from Google Cloud APIs.

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


.. code-block:: python

    operation = my_api_client.long_running_method()
    result = 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 = future.result()

    operation.add_done_callback(my_callback)

    N)
exceptions)protobuf_helpers)polling)operations_pb2)json_format)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 )	Operationa  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                    sF   t t| j|d || _|| _|| _|| _|| _t	 | _
|   d S )Nretry)superr   __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__ P/var/www/ideatree/venv/lib/python3.10/site-packages/google/api_core/operation.pyr   F   s   	
zOperation.__init__c                 C   s   | j S )zAgoogle.longrunning.Operation: The current long-running operation.)r   r   r   r   r   r   Y   s   zOperation.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Operation.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   r   
FromString)r   payloadr   r   r   deserializeh   s   
zOperation.deserializec                 C   s   | j g | jjr| jr	 W d   dS | jdr)t| j| jj}| 	| n1| jdrHt
j| jj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 smw   Y  dS )zASet the result or exception from the operation if it is complete.Nresponseerror)status_codemessageerrorsr'   zLUnexpected state: Long-running operation had neither response nor error set.)r   r   done_result_setr"   r   r#   r   r'   
set_resultr   from_grpc_statusr(   coder*   set_exceptionGoogleAPICallError)r   r'   	exceptionr   r   r   r   t   s4   
"z$Operation._set_result_from_operationc                 C   s&   | j js| j|d| _ |   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Operation._refresh_and_updatec                 C   s   |  | | 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.
        )r5   r   r,   r4   r   r   r   r,      s   
	zOperation.donec                 C   s   |   rdS |   dS )zAttempt to cancel the operation.

        Returns:
            bool: True if the cancel RPC was made, False if the operation is
                already complete.
        FT)r,   r   r    r   r   r   r      s   zOperation.cancelc                 C   s$   |    | jdo| jjjtjkS )z$True if the operation was cancelled.r(   )r5   r   r"   r(   r0   r   	CANCELLEDr    r   r   r   	cancelled   s   zOperation.cancelled)__name__
__module____qualname____doc__r   DEFAULT_RETRYr   propertyr   r!   classmethodr&   r   r5   r,   r   r7   __classcell__r   r   r   r   r   2   s     

	
!r   c                 C   s6   d |}|dur|| } | d|d}t|t S )a  Refresh an operation using a JSON/HTTP client.

    Args:
        api_request (Callable): A callable used to make an API request. This
            should generally be
            :meth:`google.cloud._http.Connection.api_request`.
        operation_name (str): The name of the operation.
        retry (google.api_core.retry.Retry): (Optional) retry policy

    Returns:
        google.longrunning.operations_pb2.Operation: The operation.
    zoperations/{}NGETmethodpath)formatr   	ParseDictr   r   )api_requestoperation_namer
   rC   api_responser   r   r   _refresh_http   s
   
rI   c                 C   s   d |}| d|d dS )a"  Cancel an operation using a JSON/HTTP client.

    Args:
        api_request (Callable): A callable used to make an API request. This
            should generally be
            :meth:`google.cloud._http.Connection.api_request`.
        operation_name (str): The name of the operation.
    zoperations/{}:cancelPOSTrA   N)rD   )rF   rG   rC   r   r   r   _cancel_http   s   
	rK   c                 K   sF   t | t }tt||j}tt||j}t||||fi |S )a  Create an operation future using a HTTP/JSON client.

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

    .. _HTTP/JSON: https://cloud.google.com/speech/reference/rest/            v1beta1/operations#Operation

    Args:
        operation (dict): Operation as a dictionary.
        api_request (Callable): A callable used to make an API request. This
            should generally be
            :meth:`google.cloud._http.Connection.api_request`.
        result_type (:func:`type`): The protobuf result type.
        kwargs: Keyword args passed into the :class:`Operation` constructor.

    Returns:
        ~.api_core.operation.Operation: The operation future to track the given
            operation.
    )	r   rE   r   r   	functoolspartialrI   namerK   )r   rF   r   kwargsoperation_protor   r   r   r   r   from_http_json   s   rQ   c                 C   s*   t j|d}| j}|dur||}||S )a{  Refresh an operation using a gRPC client.

    Args:
        operations_stub (google.longrunning.operations_pb2.OperationsStub):
            The gRPC operations stub.
        operation_name (str): The name of the operation.
        retry (google.api_core.retry.Retry): (Optional) retry policy

    Returns:
        google.longrunning.operations_pb2.Operation: The operation.
    rN   N)r   GetOperationRequestGetOperation)operations_stubrG   r
   
request_pbrpcr   r   r   _refresh_grpc  s
   rX   c                 C   s   t j|d}| | dS )zCancel an operation using a gRPC client.

    Args:
        operations_stub (google.longrunning.operations_pb2.OperationsStub):
            The gRPC operations stub.
        operation_name (str): The name of the operation.
    rR   N)r   CancelOperationRequestCancelOperation)rU   rG   rV   r   r   r   _cancel_grpc  s   r[   c                 K   s>   t jt|| j|d}t jt|| j|d}t| |||fi |S )a  Create an operation future using a gRPC client.

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

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

    Args:
        operation (google.longrunning.operations_pb2.Operation): The operation.
        operations_stub (google.longrunning.operations_pb2.OperationsStub):
            The operations stub.
        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!   )rL   rM   rX   rN   r[   r   )r   rU   r   grpc_metadatarO   r   r   r   r   r   	from_grpc"     

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\   )rL   rM   get_operationrN   cancel_operationr   )r   operations_clientr   r]   rO   r   r   r   r   r   
from_gapicB  r_   rc   )N)r;   rL   r   google.api_corer   r   google.api_core.futurer   google.longrunningr   google.protobufr   
google.rpcr   PollingFuturer   rI   rK   rQ   rX   r[   r^   rc   r   r   r   r   <module>   s$    


 