o
    }©Zh{#  ã                   @   st   U d Z ddlmZ ddlmZ ddlmZ dZi Zee	e
f ed< efdd„Zdd
d„Zdd„ ZG dd„ deƒZd	S )z=Wrap long-running operations returned from Google Cloud APIs.é    )ÚDict)Úoperations_pb2)Újson_formatztype.googleapis.comÚ_TYPE_URL_MAPc                 C   s   | j j}d||f S )a   Compute a type URL for a klass.

    :type klass: type
    :param klass: class to be used as a factory for the given type

    :type prefix: str
    :param prefix: URL prefix for the type

    :rtype: str
    :returns: the URL, prefixed as appropriate
    z%s/%s)Z
DESCRIPTORZ	full_name)ÚklassÚprefixÚname© r	   úV/var/www/html/lang_env/lib/python3.10/site-packages/google/cloud/operation/__init__.pyÚ_compute_type_url   s   r   Nc                 C   sB   |du rt | ƒ}|tv rt| | urtdt| f ƒ‚| t|< dS )a  Register a klass as the factory for a given type URL.

    :type klass: :class:`type`
    :param klass: class to be used as a factory for the given type

    :type type_url: str
    :param type_url: (Optional) URL naming the type. If not provided,
                     infers the URL from the type descriptor.

    :raises ValueError: if a registration already exists for the URL.
    NzConflict: %s)r   r   Ú
ValueError)r   Útype_urlr	   r	   r
   Úregister_type,   s   r   c                 C   s   t | j }| | j¡S )a‹  Convert an ``Any`` protobuf into the actual class.

    Uses the type URL to do the conversion.

    .. note::

        This assumes that the type URL is already registered.

    :type any_pb: :class:`google.protobuf.any_pb2.Any`
    :param any_pb: An any object to be converted.

    :rtype: object
    :returns: The instance (of the correct type) stored in the any
              instance.
    )r   r   Z
FromStringÚvalue)Zany_pbr   r	   r	   r
   Ú	_from_anyA   s   
r   c                   @   s€   e Zd ZdZdZ	 dZ	 dZ	 dZ	 dZdd„ Z	e
dd„ ƒZe
dd	„ ƒZed
d„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú	Operationa€  Representation of a Google API Long-Running Operation.

    .. _protobuf: https://github.com/googleapis/googleapis/blob/                  050400df0fdb16f63b63e9dee53819044bffc857/                  google/longrunning/operations.proto#L80
    .. _service: https://github.com/googleapis/googleapis/blob/                 050400df0fdb16f63b63e9dee53819044bffc857/                 google/longrunning/operations.proto#L38
    .. _JSON: https://cloud.google.com/speech/reference/rest/              v1beta1/operations#Operation

    This wraps an operation `protobuf`_ object and attempts to
    interact with the long-running operations `service`_ (specific
    to a given API). (Some services also offer a `JSON`_
    API that maps the same underlying data type.)

    :type name: str
    :param name: The fully-qualified path naming the operation.

    :type client: :class:`~google.cloud.client.Client`
    :param client: The client used to poll for the status of the operation.
                   If the operation was created via JSON/HTTP, the client
                   must own a :class:`~google.cloud._http.Connection`
                   to send polling requests. If created via protobuf, the
                   client must have a gRPC stub in the ``_operations_stub``
                   attribute.

    :type caller_metadata: dict
    :param caller_metadata: caller-assigned metadata about the operation
    NTc                 K   s    || _ || _| ¡ | _d| _d S )NF)r   ÚclientÚcopyÚcaller_metadataÚ	_complete)Úselfr   r   r   r	   r	   r
   Ú__init__Ž   s   

zOperation.__init__c                 K   s(   | |j |fi |¤Ž}| |¡ d|_|S )aD  Factory:  construct an instance from a protobuf.

        :type operation_pb:
            :class:`~google.longrunning.operations_pb2.Operation`
        :param operation_pb: Protobuf to be parsed.

        :type client: object: must provide ``_operations_stub`` accessor.
        :param client: The client used to poll for the status of the operation.

        :type caller_metadata: dict
        :param caller_metadata: caller-assigned metadata about the operation

        :rtype: :class:`Operation`
        :returns: new instance, with attributes based on the protobuf.
        T)r   Ú_update_stateÚ
_from_grpc)ÚclsÚoperation_pbr   r   Úresultr	   r	   r
   Úfrom_pb”   s   
zOperation.from_pbc                 K   s8   t  |t ¡ ¡}| |j|fi |¤Ž}| |¡ d|_|S )aø  Factory: construct an instance from a dictionary.

        :type operation: dict
        :param operation: Operation as a JSON object.

        :type client: :class:`~google.cloud.client.Client`
        :param client: The client used to poll for the status of the operation.

        :type caller_metadata: dict
        :param caller_metadata: caller-assigned metadata about the operation

        :rtype: :class:`Operation`
        :returns: new instance, with attributes based on the protobuf.
        F)r   Ú	ParseDictr   r   r   r   r   )r   Z	operationr   r   r   r   r	   r	   r
   Ú	from_dictª   s
   
zOperation.from_dictc                 C   s   | j S )z|Has the operation already completed?

        :rtype: bool
        :returns: True if already completed, else false.
        )r   ©r   r	   r	   r
   ÚcompleteÀ   s   zOperation.completec                 C   s   t j| jd}| jj |¡S )zÝPolls the status of the current operation.

        Uses gRPC request to check.

        :rtype: :class:`~google.longrunning.operations_pb2.Operation`
        :returns: The latest status of the current operation.
        )r   )r   ZGetOperationRequestr   r   Z_operations_stubZGetOperation)r   Z
request_pbr	   r	   r
   Ú_get_operation_rpcÉ   s   zOperation._get_operation_rpcc                 C   s.   d| j f }| jjjd|d}t |t ¡ ¡S )zÞChecks the status of the current operation.

        Uses HTTP request to check.

        :rtype: :class:`~google.longrunning.operations_pb2.Operation`
        :returns: The latest status of the current operation.
        zoperations/%sÚGET)ÚmethodÚpath)r   r   Ú_connectionZapi_requestr   r   r   r   )r   r%   Zapi_responser	   r	   r
   Ú_get_operation_httpÔ   s   zOperation._get_operation_httpc                 C   s   | j r|  ¡ S |  ¡ S )z¹Checks the status of the current operation.

        :rtype: :class:`~google.longrunning.operations_pb2.Operation`
        :returns: The latest status of the current operation.
        )r   r"   r'   r    r	   r	   r
   Ú_get_operationà   s   zOperation._get_operationc                 C   s\   |j rd| _| d¡rt|jƒ| _| d¡}|dkr |j| _dS |dkr,t|jƒ| _dS dS )zÖUpdate the state of the current object based on operation.

        :type operation_pb:
            :class:`~google.longrunning.operations_pb2.Operation`
        :param operation_pb: Protobuf to be parsed.
        TÚmetadatar   ÚerrorÚresponseN)Údoner   ZHasFieldr   r)   Z
WhichOneofr*   r+   )r   r   Zresult_typer	   r	   r
   r   ë   s   

ÿzOperation._update_statec                 C   s&   | j rtdƒ‚|  ¡ }|  |¡ | j S )zçCheck if the operation has finished.

        :rtype: bool
        :returns: A boolean indicating if the current operation has completed.
        :raises ValueError: if the operation
                 has already completed.
        zThe operation has completed.)r!   r   r(   r   )r   r   r	   r	   r
   Úpollþ   s
   
zOperation.poll)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Útargetr+   r*   r)   r   r   Úclassmethodr   r   Úpropertyr!   r"   r'   r(   r   r-   r	   r	   r	   r
   r   U   s.    


r   )N)r1   Útypingr   Zgoogle.longrunningr   Zgoogle.protobufr   Z_GOOGLE_APIS_PREFIXr   ÚstrÚtypeÚ__annotations__r   r   r   Úobjectr   r	   r	   r	   r
   Ú<module>   s   
