o
    /i!                     @   s:   d Z ddlZddlmZ ddlmZ G dd dejZdS )at  Futures for extended long-running operations returned from Google Cloud APIs.

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

.. code-block:: python

    extended_operation = my_api_client.long_running_method()

    extended_operation.result()

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

.. code-block:: python

    extended_operation = my_api_client.long_running_method()

    def my_callback(ex_op):
        print(f"Operation {ex_op.name} completed")

    extended_operation.add_done_callback(my_callback)

    N)
exceptions)pollingc                       s   e Zd ZdZejf fdd	Zedd Zedd Z	edd	 Z
ed
d Zdd ZdddZdd Zdd ZdddZdd Zedd Z  ZS )ExtendedOperationaA  An ExtendedOperation future for interacting with a Google API Long-Running Operation.

    Args:
        extended_operation (proto.Message): The initial operation.
        refresh (Callable[[], type(extended_operation)]): A callable that returns
            the latest state of the operation.
        cancel (Callable[[], None]): A callable that tries to cancel the operation.
        polling Optional(google.api_core.retry.Retry): The configuration used
            for polling. This can be used to control how often :meth:`done`
            is polled. If the ``timeout`` argument to :meth:`result` is
            specified it will override the ``polling.timeout`` property.
        retry Optional(google.api_core.retry.Retry): DEPRECATED use ``polling``
            instead. If specified it will override ``polling`` parameter to
            maintain backward compatibility.

    Note: Most long-running API methods use google.api_core.operation.Operation
    This class is a wrapper for a subset of methods that use alternative
    Long-Running Operation (LRO) semantics.

    Note: there is not a concrete type the extended operation must be.
    It MUST have fields that correspond to the following, POSSIBLY WITH DIFFERENT NAMES:
    * name: str
    * status: Union[str, bool, enum.Enum]
    * error_code: int
    * error_message: str
    c                    sD   t  jdd|i| || _|| _|| _d| _t | _| 	  d S )Nr   F )
super__init___extended_operation_refresh_cancel
_cancelled	threadingLock_completion_lock_handle_refreshed_operation)selfextended_operationrefreshcancelr   kwargs	__class__r   V/var/www/passon-env/lib/python3.10/site-packages/google/api_core/extended_operation.pyr   I   s   
zExtendedOperation.__init__c                 C      | j jS N)r   namer   r   r   r   r   j      zExtendedOperation.namec                 C   r   r   )r   statusr   r   r   r   r   n   r   zExtendedOperation.statusc                 C   r   r   )r   
error_coder   r   r   r   r   r   r   zExtendedOperation.error_codec                 C   r   r   )r   error_messager   r   r   r   r   v   r   zExtendedOperation.error_messagec                 C   s   t | j|S r   )getattrr   )r   r   r   r   r   __getattr__z   s   zExtendedOperation.__getattr__Nc                 C   s   |  | | jjS r   )_refresh_and_updater   doner   retryr   r   r   r#   }   s   
zExtendedOperation.donec                 C   s   |   rdS |   d| _dS )NFT)r#   r
   r   r   r   r   r   r      s
   zExtendedOperation.cancelc                 C   s   | j sdS |   | jjS )NF)r   r"   r   r#   r   r   r   r   	cancelled   s   zExtendedOperation.cancelledc                 C   s2   | j js|r| j|dn|  | _ |   d S d S )N)r%   )r   r#   r	   r   r$   r   r   r   r"      s
   z%ExtendedOperation._refresh_and_updatec                 C   s   | j m | jjs	 W d    d S | jr:| jr:g }t| dr(t| jdr(| jj}tj	| j| j| j|d}| 
| n&| js@| jrStd| j d| j }| 
| n| d  W d    d S W d    d S W d    d S 1 ssw   Y  d S )Nerrorerrors)status_codemessageresponser(   zUnexpected error z: )r   r   r#   r   r   hasattrr'   r(   r   from_http_statusset_exceptionGoogleAPICallError
set_result)r   r(   	exceptionr   r   r   r      s4   "z-ExtendedOperation._handle_refreshed_operationc                 K   s   | |||fi |S )aA  
        Return an instantiated ExtendedOperation (or child) that wraps
        * a refresh callable
        * a cancel callable (can be a no-op)
        * an initial result

        .. note::
            It is the caller's responsibility to set up refresh and cancel
            with their correct request argument.
            The reason for this is that the services that use Extended Operations
            have rpcs that look something like the following:

            // service.proto
            service MyLongService {
                rpc StartLongTask(StartLongTaskRequest) returns (ExtendedOperation) {
                    option (google.cloud.operation_service) = "CustomOperationService";
                }
            }

            service CustomOperationService {
                rpc Get(GetOperationRequest) returns (ExtendedOperation) {
                    option (google.cloud.operation_polling_method) = true;
                }
            }

            Any info needed for the poll, e.g. a name, path params, etc.
            is held in the request, which the initial client method is in a much
            better position to make made because the caller made the initial request.

            TL;DR: the caller sets up closures for refresh and cancel that carry
            the properly configured requests.

        Args:
            refresh (Callable[Optional[Retry]][type(extended_operation)]): A callable that
                returns the latest state of the operation.
            cancel (Callable[][Any]): A callable that tries to cancel the operation
                on a best effort basis.
            extended_operation (Any): The initial response of the long running method.
                See the docstring for ExtendedOperation.__init__ for requirements on
                the type and fields of extended_operation
        r   )clsr   r   r   r   r   r   r   make   s   +zExtendedOperation.maker   )__name__
__module____qualname____doc__r   DEFAULT_POLLINGr   propertyr   r   r   r   r!   r#   r   r&   r"   r   classmethodr3   __classcell__r   r   r   r   r   -   s(     !





r   )r7   r   google.api_corer   google.api_core.futurer   PollingFuturer   r   r   r   r   <module>   s
   