
    j              
           d dl Z d dlmZmZ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Ze j*                   G d d             Z G d d      Z	 ddedeej2                     dedee   fdZy)    N)ListOptionalIterable   )client)types)	api_error)retry_decorator   )file_upload_monitor)upload_optionsBEMinIOc                   4    e Zd ZU eed<   ee   ed<   defdZy)MultipartUploadMetadata	upload_idurlsreturnc                 H    | j                   duxr | j                   t        k7  S )zQAllows to check if upload should go directly to S3 or use local backend endpoint.N)r   LOCAL_UPLOAD_MAGIC_ID)selfs    w/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/file_upload/upload_client.pyshould_use_s3_uploaderz.MultipartUploadMetadata.should_use_s3_uploader   s     ~~T)Udnn@U.UU    N)__name__
__module____qualname__str__annotations__r   boolr    r   r   r   r      s    N
s)OV Vr   r   c                      e Zd ZdZdej
                  dej                  ddfdZde	j                  ded	edefd
Zde	j                  dedeej"                     ddfdZej(                  	 ddededeej.                     deddf
d       Zy)RestFileUploadClienta#  Defines the file upload REST API client wrapper that is used for communication with backend in order
    to start and complete S3 file upload operation as well as multipart upload against local backend.
    Args:
        rest_client: The REST API client to communicate with the backend.
    rest_clienthttpx_clientr   Nc                      || _         || _        y )N)r#   r$   )r   r#   r$   s      r   __init__zRestFileUploadClient.__init__!   s     '(r   r   num_of_file_partsbase_url_pathc           	          | j                   j                  j                  |j                  ||j                  |j
                  ||j                  |j                        }t        |j                  |j                        S )a  Starts upload by sending request to the backend and receiving upload metadata. The upload metadata
        will include the list of pre-signed URLs for direct S3 upload and upload ID assigned to this file
        upload operation. If backend decides to force upload to then local endpoint then list of URLs will
        include only one URL and upload ID will have magic value 'BEMinIO'.)	file_namer'   entity_type	entity_idpath	mime_typeproject_name)r   r   )r#   attachmentsstart_multi_part_uploadr*   r+   r,   r.   r/   r   r   pre_sign_urls)r   r   r'   r(   responses        r   start_uploadz!RestFileUploadClient.start_upload'   sy     ##//GG$../&22$..$..'44 H 
 '((x/E/E
 	
r   upload_metadata
file_partsc           
          | j                   j                  j                  |j                  |j                  |j
                  |j                  |j                  ||j                  |j                         y)zInvoked to finalize direct S3 file upload operation on the backend. It is invoked after all file parts
        was successfully uploaded to S3.)r*   r+   r,   	file_sizer   uploaded_file_partsr/   r.   N)
r#   r0   complete_multi_part_uploadr*   r+   r,   r8   r   r/   r.   )r   r   r5   r6   s       r   s3_upload_completedz(RestFileUploadClient.s3_upload_completed>   sf     	$$??$..&22$..$..%// *'44$.. 	@ 		
r   
upload_url	file_pathmonitor
chunk_sizec                     | j                   j                  |t        |||            }|j                  dk\  r=||j	                          t        j                  |j                  |j                        y)zInvoked to upload a file to the local backend using httpx client configured with necessary authorization
        headers. Raises the `HTTPStatusError` if one occurred.)r?   r>   )urlcontenti  N)status_codebody)r$   put_data_generatorrC   resetrest_api_errorApiErrorrB   )r   r<   r=   r>   r?   r3   s         r   upload_file_localz&RestFileUploadClient.upload_file_localQ   sv     $$((#I*gV ) 
 3&" ))$00x7G7G  'r   )r   r   r   __doc__rest_api_clientOpikApihttpxClientr&   file_upload_optionsFileUploadOptionsintr   r   r4   r   rest_api_typesMultipartUploadPartr;   r
   opik_rest_retryr   r   FileUploadMonitorrJ   r    r   r   r"   r"      s    )*22)BG,,)	)
+==
 
 	

 
!
.
+==
 1
 ;;<	

 

& $$   -??@	
  
 %r   r"   r=   r>   r?   r   c              #      K   t        | d      5 }|j                  |      x}r4| ||j                  t        |             |j                  |      x}r4d d d        y # 1 sw Y   y xY ww)Nrb)openreadupdatelen)r=   r>   r?   filedatas        r   rF   rF   i   sd     
 
i	$ii
++d+J"s4y) ii
++d+ 
		s   A,AA 	A, A)%A,rK   )dataclassestypingr   r   r   rP   rest_apir   rN   r   rU   rest_api.corer	   rH   rest_client_configuratorr
    r   r   rR   r   	dataclassr   r"   r   rX   rT   bytesrF   r    r   r   <module>ri      s     + +  0 . 7 6 ! 3!  V V VL Ld 	*	*);;<	* 	* e_		*r   