
    rh3                         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
ej                        ZddZd Zd ZddZd ZddZddZy)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                        e Zd ZdZdej
                  f fd	Zed        Zed        Z	e
d        Zd ZddZdd	Zd
 Zd Z xZS )	OperationaH  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.
        polling (google.api_core.retry.Retry): The configuration used for polling.
            This parameter controls how often :meth:`done` is polled. If the
            ``timeout`` argument is specified in the :meth:`result` method, it will
            override the ``polling.timeout`` property.
        retry (google.api_core.retry.Retry): DEPRECATED: use ``polling`` instead.
            If specified it will override ``polling`` parameter to maintain
            backward compatibility.
    Nc                     t        t        | 
  dd|i| || _        || _        || _        || _        || _        t        j                         | _
        | j                          y )Nr    )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   kwargs	__class__s	           l/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/google/api_core/operation.pyr   zOperation.__init__I   s\     	i'BB6B#'+ ) 0'')    c                     | j                   S )zAgoogle.longrunning.Operation: The current long-running operation.)r   r   s    r    r   zOperation.operation]   s     r!   c                     | j                   j                  d      syt        j                  | j                  | j                   j
                        S )z8google.protobuf.Message: the current operation metadata.metadataN)r   HasFieldr   from_any_pbr   r%   r#   s    r    r%   zOperation.metadatab   sA     ''
3++!9!9
 	
r!   c                 @    t         j                  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   payloads     r    deserializezOperation.deserializel   s     ''227;;r!   c                    | j                   5  | j                  j                  r| j                  r
	 ddd       y| j                  j	                  d      rFt        j                  | j                  | j                  j                        }| j                  |       n| j                  j	                  d      rt        j                  | j                  j                  j                  | j                  j                  j                  | j                  j                  f| j                        }| j                  |       n&t        j                   d      }| j                  |       ddd       y# 1 sw Y   yxY w)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.   coder0   set_exceptionGoogleAPICallError)r   r-   	exceptions      r    r   z$Operation._set_result_from_operationx   s!   
 "" 	.
 ??''4+;+;	. 	. ''
3+77%%t'?'? )))'2&77 $ 5 5 : : OO1199 OO113!__		 ""9-&99.	 ""9-5	. 	. 	.s   $E+D(E++E4c                     | j                   j                  s:|r| j                  |      n| j                         | _         | j                          yy)zRefresh the operation and update the result if needed.

        Args:
            retry (google.api_core.retry.Retry): (Optional) How to retry the RPC.
        )retryN)r   r2   r   r   r   r;   s     r    _refresh_and_updatezOperation._refresh_and_update   s>     ##<Admm%m8t}}DO++- $r!   c                 P    | j                  |       | 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.
        )r=   r   r2   r<   s     r    r2   zOperation.done   s"     	  '###r!   c                 F    | j                         ry| j                          y)zAttempt to cancel the operation.

        Returns:
            bool: True if the cancel RPC was made, False if the operation is
                already complete.
        FT)r2   r   r#   s    r    r   zOperation.cancel   s     99;r!   c                     | j                          | j                  j                  d      xr1 | j                  j                  j                  t
        j                  k(  S )z$True if the operation was cancelled.r.   )r=   r   r&   r.   r6   r   	CANCELLEDr#   s    r    	cancelledzOperation.cancelled   sI      "OO$$W- A%%**h.@.@@	
r!   N)__name__
__module____qualname____doc__r   DEFAULT_POLLINGr   propertyr   r%   classmethodr+   r   r=   r2   r   rB   __classcell__)r   s   @r    r
   r
   2   sq    8 ''*(   
 
 	< 	<.B
.
$
r!   r
   c                     dj                  |      }| ||       }  | d|      }t        j                  |t        j                               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/{}GETmethodpath)formatr   	ParseDictr   r
   )api_requestoperation_namer;   rP   api_responses        r    _refresh_httprV      sL     !!.1DK(e$7L  ~/G/G/IJJr!   c                 :    dj                  |      } | d|       y)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POSTrN   N)rQ   )rS   rT   rP   s      r    _cancel_httprY      s     "((8DvD)r!   c                    t        j                  | t        j                               }t	        j
                  t        ||j                        }t	        j
                  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   rR   r   r
   	functoolspartialrV   namerY   )r   rS   r   r   operation_protor   r   s          r    from_http_jsonr_      si    * "++I~7O7O7QRO{O<P<PQG|[/:N:NOF_gv{MfMMr!   c                 j    t        j                  |      }| j                  }| ||      } ||      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.
    r]   )r   GetOperationRequestGetOperation)operations_stubrT   r;   
request_pbrpcs        r    _refresh_grpcrg     s8      33HJ

&
&CCjz?r!   c                 R    t        j                  |      }| j                  |       y)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.
    ra   N)r   CancelOperationRequestCancelOperation)rd   rT   re   s      r    _cancel_grpcrk     s"      66NKJ##J/r!   c                     t        j                  t        || j                  |      }t        j                  t        || j                  |      }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%   )r[   r\   rg   r]   rk   r
   )r   rd   r   grpc_metadatar   r   r   s          r    	from_grpcro   &  s^    . 	G 	F YGGGr!   c                     t        j                  |j                  | j                  |      }t        j                  |j                  | j                  |      }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.
    rm   )r[   r\   get_operationr]   cancel_operationr
   )r   operations_clientr   rn   r   r   r   s          r    
from_gapicrt   L  sd    . ''G
 **F
 YGGGr!   rC   )rG   r[   r   google.api_corer   r   google.api_core.futurer   google.longrunningr   google.protobufr   
google.rpcr   PollingFuturer
   rV   rY   r_   rg   rk   ro   rt   r   r!   r    <module>r{      sa   0   & , * - ' R
%% R
jK,
*N6*	0#HL!Hr!   