o
    _ik                  	   @   sV  d dl Z d dlZd dlZd dlZd dlmZmZmZ d dlm	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 ddlmZ ddlmZ ddlmZ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#	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'	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Union)	urlencode   )_api_client)_api_module)_common)_extra_utils)_transformers)types)get_value_by_path)set_value_by_path)
AsyncPagerPagerzgoogle_genai.filesfrom_objectparent_objectreturnc                 C   .   i }t | dgd urt|dgt | dg |S )Nfilegetvsetvr   r   	to_object r   K/home/air/demo/back/venv/lib/python3.10/site-packages/google/genai/files.py_CreateFileParameters_to_mldev)      r   c                 C   .   i }t | dgd urt|dgt | dg |S NsdkHttpResponsesdk_http_responser   r   r   r   r   _CreateFileResponse_from_mldev4      r#   c                 C   6   i }t | dgd urt|ddgtt | dg |S Nname_urlr   r   r   tt_file_namer   r   r   r   _DeleteFileParameters_to_mldevA      r,   c                 C   r   r    r   r   r   r   r   _DeleteFileResponse_from_mldevN   r$   r.   c                 C   r%   r&   r)   r   r   r   r   _GetFileParameters_to_mldev[   r-   r/   c                 C   sX   i }t | dgd urt|ddgt | dg t | dgd ur*t|ddgt | dg |S )N	page_size_querypageSize
page_token	pageTokenr   r   r   r   r   _ListFilesConfig_to_mldevh   s   
r5   c                 C   s*   i }t | dgd urtt | dg| |S )Nconfig)r   r5   r   r   r   r   _ListFilesParameters_to_mldev}   s   r7   c                 C   s   i }t | dgd urt|dgt | dg t | dgd ur(t|dgt | dg t | dgd ur@t|dgdd t | dgD  |S )Nr!   r"   nextPageTokennext_page_tokenfilesc                 S      g | ]}|qS r   r   .0itemr   r   r   
<listcomp>       z1_ListFilesResponse_from_mldev.<locals>.<listcomp>r   r   r   r   r   _ListFilesResponse_from_mldev   s    rA   c                 C   r   )Nurisr   r   r   r   r   !_RegisterFilesParameters_to_mldev   r   rC   c                 C   s^   i }t | dgd urt|dgt | dg t | dgd ur-t|dgdd t | dgD  |S )Nr!   r"   r:   c                 S   r;   r   r   r<   r   r   r   r?      r@   z5_RegisterFilesResponse_from_mldev.<locals>.<listcomp>r   r   r   r   r   !_RegisterFilesResponse_from_mldev   s    rD   c                	   @   s  e Zd Zdddeej dejfddZdddejdeej	 dej
fdd	Zddd
edeej dejfddZddd
edeej dejfddZdddee deej dejfddZdddeeeje ejf deej dejfddZdddeeejejejf deej  de!fddZ"ddde#j$j%j&dee deej dejfddZ'dddeej de(ej fddZdS )FilesNr6   r6   r   c                C   s  t j|d}| jjrtdt|}|d}|rd|}nd}|d}|r1| dt| }|	dd  d }|j
d urH|j
jd urH|j
j}t|}t|}| jd|||}|js`i nt|j}	| jjsnt|	}	t jj|	| d	}
t j|jd
|
_| j|
 |
S NrF   =This method is only supported in the Gemini Developer client.r(   r:   r1   ?r6   getresponsekwargsheaders)r   _ListFilesParametersr   vertexai
ValueErrorr7   rJ   
format_mapr   popr6   http_optionsr	   convert_to_dictencode_unserializable_typesrequestbodyjsonloadsrA   ListFilesResponse_from_response
model_dumpHttpResponserO   r"   _verify_responseselfr6   parameter_modelrequest_dictrequest_url_dictpathquery_paramsrU   rL   response_dictreturn_valuer   r   r   _list   sD   




zFiles._listr   c                C   2  t j||d}| jjr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|||}	|d urtt|d	d rtt j|	d
}
| j|
 |
S |	jsyi nt|	j}| jjst|}t jj|| d}
| j|
 |
S Nr   r6   rH   r(   zupload/v1beta/filesr1   rI   r6   postshould_return_http_responser"   rK   )r   _CreateFileParametersr   rQ   rR   r   rJ   rS   r   rT   r6   rU   r	   rV   rW   rX   getattrCreateFileResponser`   rY   rZ   r[   r#   r]   r^   rb   r   r6   rc   rd   re   rf   rg   rU   rL   ri   rh   r   r   r   _create   P   





zFiles._creater'   c                C   s   t j||d}| jjr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 )ai  Retrieves the file information from the service.

    Args:
      name (str): The name identifier for the file to retrieve.
      config (GetFileConfig): Optional, configuration for the get method.

    Returns:
      File: The file information.

    Usage:

    .. code-block:: python

      file = client.files.get(name='files/...')
      print(file.uri)
    r'   r6   rH   r(   files/{file}r1   rI   r6   NrJ   rK   )r   _GetFileParametersr   rQ   rR   r/   rJ   rS   r   rT   r6   rU   r	   rV   rW   rX   rY   rZ   r[   Filer]   r^   r`   rb   r'   r6   rc   rd   re   rf   rg   rU   rL   rh   ri   r   r   r   rJ   +  s<   




z	Files.getc                C   s  t j||d}| jjr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}
| jjsot|
}
t jj|
| d
}t j|	jd|_| j| |S )a]  Deletes a remotely stored file.

    Args:
      name (str): The name identifier for the file to delete.
      config (DeleteFileConfig): Optional, configuration for the delete method.

    Returns:
      DeleteFileResponse: The response for the delete method

    Usage:

    .. code-block:: python

      client.files.delete(name='files/...')
    rw   rH   r(   rx   r1   rI   r6   NdeleterK   rN   )r   _DeleteFileParametersr   rQ   rR   r,   rJ   rS   r   rT   r6   rU   r	   rV   rW   rX   rY   rZ   r[   r.   DeleteFileResponser]   r^   r_   rO   r"   r`   r{   r   r   r   r|   l  sJ   




zFiles.deleterB   c                C   rk   NrB   r6   rH   r(   zfiles:registerr1   rI   r6   rn   ro   rp   rK   )r   _RegisterFilesParametersr   rQ   rR   rC   rJ   rS   r   rT   r6   rU   r	   rV   rW   rX   rr   RegisterFilesResponser`   rY   rZ   r[   rD   r]   r^   rb   rB   r6   rc   rd   re   rf   rg   rU   rL   ri   rh   r   r   r   _register_files  rv   zFiles._register_filesc                C   s^  | j jrtdt }|r*t|trtjdi |}n|}tj|j|j	|j
d}nt }|j	dur@|j	ds@d|j	 |_	tj||j|jd\}}}||_||_| j|tj|ddd}|jdu sp|jjdu spd	|jjvrttd
|jjd	 }	t|tjr| j j||	|j|d}
nt|}| j j||	|j|d}
tjj|
jd |r| dS i dS )a  Calls the API to upload a file using a supported file service.

    Args:
      file: A path to the file or an `IOBase` object to be uploaded. If it's an
        IOBase object, it must be opened in blocking (the default) mode and
        binary mode. In other words, do not use non-blocking mode or text mode.
        The given stream must be seekable, that is, it must be able to call
        `seek()` on 'path'.
      config: Optional parameters to set `diplay_name`, `mime_type`, and `name`.
    rH   	mime_typer'   display_nameNfiles/user_http_optionsuser_mime_typeTrU   ro   rm   x-goog-upload-urlPFailed to create file. Upload URL did not returned from the create file request.rU   r   rK   r   )r   rQ   rR   r   UploadFileConfig
isinstancedictrz   r   r'   r   
startswithr
   prepare_resumable_uploadrU   
size_bytesru   CreateFileConfigr"   rO   KeyErrorioIOBaseupload_fileosfspathr]   rZ   r^   rb   r   r6   config_modelfile_objrU   r   r   rL   
upload_urlreturn_filefs_pathr   r   r   upload  sf   





zFiles.uploadc          	      C   s   | j jrtdd}|rt|trtjdi |}n|}t|tjr+|jdu r+tdt	
|}d| d}ddi}| dt| }d}t|d	gdurSt|d	g}| j j||d
}t|tjrf||_|S t|tjru|jduru||j_|S )ar  Downloads a file's data from storage.

    Files created by `upload` can't be downloaded. You can tell which files are
    downloadable by checking the `source` or `download_uri` property.

    Note: This method returns the data as bytes. For `Video` and
    `GeneratedVideo` objects there is an additional side effect, that it also
    sets the `video_bytes` property on the `Video` object.

    Args:
      file (str): A file name, uri, or file object. Identifying which file to
        download.
      config (DownloadFileConfigOrDict): Optional, configuration for the get
        method.

    Returns:
      File: The file data as bytes.

    Usage:

    .. code-block:: python

      for file client.files.list():
        if file.download_uri is not None:
          break
      else:
        raise ValueError('No files found with a `download_uri`.')
      data = client.files.download(file=file)
      # data = client.files.download(file=file.name)
      # data = client.files.download(file=file.download_uri)

      video = types.Video(uri=file.uri)
      video_bytes = client.files.download(file=video)
      video.video_bytes
    rH   NzOnly generated files can be downloaded, uploaded files can't be downloaded. You can tell which files are downloadable by checking the `source` or `download_uri` property.r   	:downloadaltmediarI   rU   r   r   )r   rQ   rR   r   r   r   DownloadFileConfigrz   download_urir*   r+   r   r   download_fileVideovideo_bytesGeneratedVideovideo)	rb   r   r6   r   r'   rf   rg   rU   datar   r   r   download>  s<   )

zFiles.downloadauthc                C   s   t |tjjjstd|du rt }ntj|}|j	dd}|j
p't }|jp,i }dd | D }t| j|}d| |d< |jrL|j|d	< ||_||_
| j||d
S )*Registers gcs files with the file service.:auth must be a google.auth.credentials.Credentials object.NTdeepc                 S      i | ]	\}}|  |qS r   lowerr=   kvr   r   r   
<dictcomp>      z(Files.register_files.<locals>.<dictcomp>Bearer authorizationx-goog-user-projectr   )r   googler   credentialsCredentialsrR   r   RegisterFilesConfigmodel_validate
model_copyrU   HttpOptionsrO   itemsr   get_token_from_credentialsquota_project_idr   rb   r   rB   r6   rU   rO   tokenr   r   r   register_files  s$   


zFiles.register_filesc                C   s   | j }td|| j |d|S )a  Lists all files from the service.

    Args:
      config (ListFilesConfig): Optional, configuration for the list method.

    Returns:
      A Pager object that contains one page of files. When iterating over
      the pager, it automatically fetches the next page if there are more.

    Usage:

    .. code-block:: python

      for file in client.files.list(config={'page_size': 10}):
        print(file.name)
    r:   rF   )rj   r   rb   r6   list_requestr   r   r   list  s   
z
Files.list))__name__
__module____qualname__r   r   ListFilesConfigOrDictr\   rj   
FileOrDictCreateFileConfigOrDictrs   ru   strGetFileConfigOrDictrz   rJ   DeleteFileConfigOrDictr~   r|   r   RegisterFilesConfigOrDictr   r   r   r   PathLiker   r   UploadFileConfigOrDictr   r   r   DownloadFileConfigOrDictbytesr   r   r   r   r   r   r   r   r   r   r   rE      s    
8
@
B
K
C
P
V
"rE   c                	   @   sz  e Zd Zdddeej dejfddZdddejdeej	 dej
fdd	Zddd
edeej dejfddZddd
edeej dejfddZdddee deej dejfddZdddeeeje ejf deej dejfddZdddeeejf deej defddZ ddde!j"j#j$dee deej dejfddZ%dddeej de&ej fddZdS )
AsyncFilesNrF   r6   r   c                   s  t j|d}| jjr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|||I d H }|jsdi nt|j}	| jjsrt|	}	t jj|	| d	}
t j|jd
|
_| j|
 |
S rG   )r   rP   r   rQ   rR   r7   rJ   rS   r   rT   r6   rU   r	   rV   rW   async_requestrY   rZ   r[   rA   r\   r]   r^   r_   rO   r"   r`   ra   r   r   r   rj     sJ   





zAsyncFiles._listr   c                   :  t j||d}| jjr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 }	|d urxt|d	d rxt j|	d
}
| j|
 |
S |	js}i nt|	j}| jjst|}t jj|| d}
| j|
 |
S rl   )r   rq   r   rQ   rR   r   rJ   rS   r   rT   r6   rU   r	   rV   rW   r   rr   rs   r`   rY   rZ   r[   r#   r]   r^   rt   r   r   r   ru     R   






zAsyncFiles._creater'   c                   s   t j||d}| jjr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 )as  Retrieves the file information from the service.

    Args:
      name (str): The name identifier for the file to retrieve.
      config (GetFileConfig): Optional, configuration for the get method.

    Returns:
      File: The file information.

    Usage:

    .. code-block:: python

      file = await client.aio.files.get(name='files/...')
      print(file.uri)
    rw   rH   r(   rx   r1   rI   r6   NrJ   rK   )r   ry   r   rQ   rR   r/   rJ   rS   r   rT   r6   rU   r	   rV   rW   r   rY   rZ   r[   rz   r]   r^   r`   r{   r   r   r   rJ   D  sB   





zAsyncFiles.getc                   s  t j||d}| jjr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}
| jjsst|
}
t jj|
| d
}t j|	jd|_| j| |S )ag  Deletes a remotely stored file.

    Args:
      name (str): The name identifier for the file to delete.
      config (DeleteFileConfig): Optional, configuration for the delete method.

    Returns:
      DeleteFileResponse: The response for the delete method

    Usage:

    .. code-block:: python

      await client.aio.files.delete(name='files/...')
    rw   rH   r(   rx   r1   rI   r6   Nr|   rK   rN   )r   r}   r   rQ   rR   r,   rJ   rS   r   rT   r6   rU   r	   rV   rW   r   rY   rZ   r[   r.   r~   r]   r^   r_   rO   r"   r`   r{   r   r   r   r|     sL   





zAsyncFiles.deleterB   c                   r   r   )r   r   r   rQ   rR   rC   rJ   rS   r   rT   r6   rU   r	   rV   rW   r   rr   r   r`   rY   rZ   r[   rD   r]   r^   r   r   r   r   r     r   zAsyncFiles._register_filesc                   s  | j jr	tdt }|r+t|trtjdi |}n|}tj|j|j	|j
d}nt }|j	durA|j	dsAd|j	 |_	tj||j|jd\}}}||_||_| j|tj|dddI dH }|jdu sz|jjdu szd	|jjvr~d
|jjvr~tdd	|jjv r|jjd	 }	n|jjd
 }	t|tjr| j j||	|j|dI dH }
nt|}| j j||	|j|dI dH }
tjj|
jd |r| dS i dS )a  Calls the API to upload a file asynchronously using a supported file service.

    Args:
      file: A path to the file or an `IOBase` object to be uploaded. If it's an
        IOBase object, it must be opened in blocking (the default) mode and
        binary mode. In other words, do not use non-blocking mode or text mode.
        The given stream must be seekable, that is, it must be able to call
        `seek()` on 'path'.
      config: Optional parameters to set `diplay_name`, `mime_type`, and `name`.
    rH   r   Nr   r   Tr   rm   r   zX-Goog-Upload-URLr   r   r   rK   r   )r   rQ   rR   r   r   r   r   rz   r   r'   r   r   r
   r   rU   r   ru   r   r"   rO   r   r   r   async_upload_filer   r   r]   rZ   r^   r   r   r   r   r     sn   





zAsyncFiles.uploadc          	         s   | j jr	tdd}|rt|trtjd
i |}n|}t|}d| d}d}t	|dgdur8t	|dg}ddi}|rG| dt
| }| j j||d	I dH }|S )at  Downloads a file's data from the file service.

    The Vertex-AI implementation of the API foes not include the file service.

    Files created by `upload` can't be downloaded. You can tell which files are
    downloadable by checking the `download_uri` property.

    Args:
      File (str): A file name, uri, or file object. Identifying which file to
        download.
      config (DownloadFileConfigOrDict): Optional, configuration for the get
        method.

    Returns:
      File: The file data as bytes.

    Usage:

    .. code-block:: python

      for file client.files.list():
        if file.download_uri is not None:
          break
      else:
        raise ValueError('No files found with a `download_uri`.')
      data = client.files.download(file=file)
      # data = client.files.download(file=file.name)
      # data = client.files.download(file=file.uri)
    rH   Nr   r   rU   r   r   rI   r   r   )r   rQ   rR   r   r   r   r   r*   r+   r   r   async_download_file)	rb   r   r6   r   r'   rf   rU   rg   r   r   r   r   r   ^  s.   #

zAsyncFiles.downloadr   c                   s   t |tjjjstd|du rt }ntj|}|j	dd}|j
p(t }|jp-i }dd | D }t| j|I dH }d| |d< |jrP|j|d	< ||_||_
| j||d
I dH S )r   r   NTr   c                 S   r   r   r   r   r   r   r   r     r   z-AsyncFiles.register_files.<locals>.<dictcomp>r   r   r   r   )r   r   r   r   r   rR   r   r   r   r   rU   r   rO   r   r    async_get_token_from_credentialsr   r   r   r   r   r   r     s*   



zAsyncFiles.register_filesc                   s$   | j }td|| j |dI dH |S )a  Lists all files from the service asynchronously.

    Args:
      config (ListFilesConfig): Optional, configuration for the list method.

    Returns:
      A Pager object that contains one page of files. When iterating over
      the pager, it automatically fetches the next page if there are more.

    Usage:

    .. code-block:: python

      async for file in await client.aio.files.list(config={'page_size': 10}):
        print(file.name)
    r:   rF   N)rj   r   r   r   r   r   r     s   zAsyncFiles.list)'r   r   r   r   r   r   r\   rj   r   r   rs   ru   r   r   rz   rJ   r   r~   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    
:
@
D
K
C
U
G
$r   )N)-r   rZ   loggingr   typingr   r   r   urllib.parser   google.authr    r   r   r	   r
   r   r*   r   r   r   r   r   pagersr   r   	getLoggerloggerr   r   objectr   r#   r,   r.   r/   r5   r7   rA   rC   rD   
BaseModulerE   r   r   r   r   r   <module>   s   




















    