o
    YiE                  	   @   s  d dl Z d dlZd dlmZmZmZ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 dd	lmZ ed
Z	ddeeeef ef deeeef  deeef fddZ	ddeeeef ef deeeef  deeef fddZ	ddeeeef ef deeeef  deeef fddZ	ddeeeef ef deeeef  deeef fddZG dd de
jZG dd de
jZdS )    N)AnyOptionalTypeVarUnion)	urlencode   )_api_module)_common)types)get_value_by_path)set_value_by_pathzgoogle_genai.operationsfrom_objectparent_objectreturnc                 C   sV   i }t | dgd urt|dgt | dg t | dgd ur)t|ddgt | dg |S )Noperation_nameoperationNameresource_name_urlZresourceNamegetvsetvr   r   Z	to_object r   Q/home/kim/smarthome/.venv/lib/python3.10/site-packages/google/genai/operations.py*_FetchPredictOperationParameters_to_vertex!   s   
r   c                 C   0   i }t | dgd urt|ddgt | dg |S Nr   r   r   r   r   r   r   r    _GetOperationParameters_to_mldev3      
r   c                 C   r   r   r   r   r   r   r   !_GetOperationParameters_to_vertexB   r   r   c                 C   s0   i }t | dgd urt|ddgt | dg |S )Noperation_idr   r   r   r   r   r   (_GetProjectOperationParameters_to_vertexQ   s   r!   c                	   @      e Zd Zdddedeej defddZdddededeej	 defd	d
Z
dddedeej dejfddZedejdZdddedeej defddZdS )
OperationsNconfigr   r%   r   c                C   s   t j||d}| jjrt|}|d}|rd|}nd}nt|}|d}|r0d|}nd}|d}|rB| dt| }|	dd  d }|j
d urY|j
jd urY|j
j}t|}t|}| jd|||}	|	jssi }
|
S t|	j}
|
S Nr   r%   r   z{operationName}_query?r%   get)r
   _GetOperationParameters_api_clientvertexair   r*   
format_mapr   r   popr%   http_optionsr	   convert_to_dictencode_unserializable_typesrequestbodyjsonloadsselfr   r%   parameter_modelrequest_dictrequest_url_dictpathquery_paramsr0   responseresponse_dictr   r   r   _get_videos_operation`   s<   






z Operations._get_videos_operationr   c                C   s   t j|||d}| jjstdt|}|d}|r!d|}nd}|d}|r3| dt| }|	dd  d }	|j
d urJ|j
jd urJ|j
j}	t|}t|}| jd|||	}
|
jsdi }|S t|
j}|S 	Nr   r   r%   6This method is only supported in the Vertex AI client.r   z${resourceName}:fetchPredictOperationr(   r)   r%   post)r
    _FetchPredictOperationParametersr,   r-   
ValueErrorr   r*   r.   r   r/   r%   r0   r	   r1   r2   r3   r4   r5   r6   r8   r   r   r%   r9   r:   r;   r<   r=   r0   r>   r?   r   r   r   _fetch_predict_videos_operation   s>   





z*Operations._fetch_predict_videos_operationr    c                C   s   t j||d}| jjstdt|}|d}|r d|}nd}|d}|r2| dt| }|	dd  d }|j
d urI|j
jd urI|j
j}t|}t|}| jd|||}	|	jsai nt|	j}
t jj|
| d	}| j| |S 
N)r    r%   rC   r   zoperations/{operation_id}r(   r)   r%   r*   )r>   kwargs)r
   _GetProjectOperationParametersr,   r-   rF   r!   r*   r.   r   r/   r%   r0   r	   r1   r2   r3   r4   r5   r6   ProjectOperation_from_response
model_dump_verify_responser8   r    r%   r9   r:   r;   r<   r=   r0   r>   r?   return_valuer   r   r   _get   s8   




zOperations._getTbound	operationc          
      C   s   |j }|s	td| jjre|dd }t }t|tr2|	dd}|dur1tjdi t|}nt|tj
rH|durH|jdurD|jnt }tj|d}| j|||d}|j|dd	}	| j|	 |	S | j||d
}|j|dd	}	| j|	 |	S )a  Gets the status of a long-running operation.

    Args:
      operation (Operation): The operation instance to get the status for.
      config (GetOperationConfig): Configuration for getting the operation.

    Returns:
      Operation: The updated operation instance with the latest status or
      result.

    Usage:

    .. code-block:: python

      import time

      operation = client.models.generate_videos(
          model="veo-2.0-generate-001",
          source=types.GenerateVideosSource(
              prompt="A neon hologram of a cat driving at top speed",
          ),
      )
      while not operation.done:
          time.sleep(10)
          operation = client.operations.get(operation)

      print(operation.result)
    Operation name is empty./operations/r   r0   Nr0   rB   TZis_vertex_air'   Fr   )namerF   r,   r-   
rpartitionr
   HttpOptions
isinstancedictr*   GetOperationConfigr0   FetchPredictOperationConfigrH   from_api_responserO   r@   
r8   rV   r%   r   r   r0   Zdict_optionsZfetch_operation_configr?   Zresponse_operationr   r   r   r*      sL   $

zOperations.get__name__
__module____qualname__strr   r
   ZGetOperationConfigOrDictr   r@   Z!FetchPredictOperationConfigOrDictrH   rL   rR   r   Z	OperationrS   r*   r   r   r   r   r#   ^   sH    
6
6
1r#   c                	   @   r"   )AsyncOperationsNr$   r   r%   r   c                   s   t j||d}| jjr t|}|d}|rd|}nd}nt|}|d}|r1d|}nd}|d}|rC| dt| }|	dd  d }|j
d urZ|j
jd urZ|j
j}t|}t|}| jd|||I d H }	|	jswi }
|
S t|	j}
|
S r&   )r
   r+   r,   r-   r   r*   r.   r   r   r/   r%   r0   r	   r1   r2   async_requestr4   r5   r6   r7   r   r   r   r@   J  sB   







z%AsyncOperations._get_videos_operationr   c                   s   t j|||d}| jjstdt|}|d}|r"d|}nd}|d}|r4| dt| }|	dd  d }	|j
d urK|j
jd urK|j
j}	t|}t|}| jd|||	I d H }
|
jshi }|S t|
j}|S rA   )r
   rE   r,   r-   rF   r   r*   r.   r   r/   r%   r0   r	   r1   r2   rj   r4   r5   r6   rG   r   r   r   rH   }  s@   






z/AsyncOperations._fetch_predict_videos_operationr    c                   s   t j||d}| jjstdt|}|d}|r!d|}nd}|d}|r3| dt| }|	dd  d }|j
d urJ|j
jd urJ|j
j}t|}t|}| jd|||I d H }	|	jsei nt|	j}
t jj|
| d	}| j| |S rI   )r
   rK   r,   r-   rF   r!   r*   r.   r   r/   r%   r0   r	   r1   r2   rj   r4   r5   r6   rL   rM   rN   rO   rP   r   r   r   rR     s>   





zAsyncOperations._getrS   rT   rV   c          
         s   |j }|s
td| jjrc|dd }t }t|tr3|	dd}|dur2tjdi t|}nt|tj
rI|durI|jdurE|jnt }tj|d}| j|||dI dH }|j|dd	}	|	S | j||d
I dH }|j|dd	}	|	S )a  Gets the status of a long-running operation.

    Args:
      operation (Operation): The operation instance to get the status for.
      config (GetOperationConfig): Configuration for getting the operation.

    Returns:
      Operation: The updated operation instance with the latest status or
      result.

    Usage:

    .. code-block:: python

      import asyncio

      operation = await client.aio.models.generate_videos(
          model="veo-2.0-generate-001",
          source=types.GenerateVideosSource(
              prompt="A neon hologram of a cat driving at top speed",
          ),
      )
      while not operation.done:
          await asyncio.sleep(10)
          operation = await client.aio.operations.get(operation)

      print(operation.result)
    rW   rX   r   r0   NrY   rB   TrZ   r'   Fr   )r[   rF   r,   r-   r\   r
   r]   r^   r_   r*   r`   r0   ra   rH   rb   r@   rc   r   r   r   r*     sJ   #

zAsyncOperations.getrd   r   r   r   r   ri   H  sH    
8
6
3ri   )N)r5   loggingtypingr   r   r   r   urllib.parser    r   r	   r
   r   r   r   r   	getLoggerloggerr_   rh   objectr   r   r   r!   Z
BaseModuler#   ri   r   r   r   r   <module>   sZ   








 k