
    jX                        U d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
mZmZmZ d dlmZ d dl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  ej<                  e      Z ejB                  Z"ee#d<    G d d      Z$y)    N)s3_httpx_client)IteratorListLiteralOptional)	TypeAlias)file_uploaderupload_options)client)
attachment)core)url_helpers   )rest_helpersRESTAttachmentDetailsc                       e Zd ZdZdej
                  dededej                  ddf
dZ	d	ed
ede
d   dee   fdZd	ede
d   d
edededee   fdZ	 	 dd	ede
d   d
ededee   dee   ddfdZy)AttachmentClienta  
    Client for interacting with attachment-related operations.

    This client provides methods to retrieve attachment lists, download attachments,
    and upload attachments for traces and spans.

    The AttachmentClient supports:
    - Listing attachments associated with traces or spans
    - Downloading attachment content as a byte stream
    - Uploading files as attachments to traces or spans

    All operations are performed within the context of a specific project and require
    the project name to be provided.
    rest_clienturl_overrideworkspace_namerest_httpx_clientreturnNc                 <    || _         || _        || _        || _        y)a  
        Initialize the AttachmentClient.
        It is typically created via ``Opik.get_attachment_client()`` rather
        than being instantiated directly.

        Parameters:
            rest_client: The REST API client instance for making backend requests.
            url_override: The base URL for the Opik server.
            workspace_name: The workspace name used for download operations.
            rest_httpx_client: The httpx client instance to use for making file uploads.

        Returns:
            None
        N)_rest_client_url_override_workspace_name_rest_httpx_client)selfr   r   r   r   s        {/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/api_objects/attachment/client.py__init__zAttachmentClient.__init__'   s$    * ()-"3    project_name	entity_identity_type)spantracec                 .   t        j                  | j                  |      }t        j                  | j
                  j                  d            j                  d      }| j                  j                  j                  ||||      }|j                  xs g S )a  
        Get a list of attachments for a specific entity (trace or span).

        Parameters:
            project_name: The name of the project containing the entity.
            entity_id: The ID of the trace or span to retrieve attachments for.
            entity_type: The type of entity ("trace" or "span").

        Returns:
            List[RESTAttachmentDetails]: List of attachment detail objects containing metadata about each attachment.
        utf-8)
project_idr$   r#   path)r   resolve_project_id_by_namer   base64	b64encoder   encodedecodeattachmentsattachment_listcontent)r   r"   r#   r$   r)   url_override_pathresponses          r   get_attachment_listz$AttachmentClient.get_attachment_listA   s    " "<<|

 #,,T-?-?-F-Fw-OPWW
 $$00@@!#"	 A 
 %2%r!   )r&   r%   	file_name	mime_typec              #     K   | j                  |||      }d}|D ]%  }|j                  |k(  s|j                  |k(  s#|} n |t        d|       |j                  t        d|       t        j                  |j                        rt        j                         n| j                  }	|	j                  d|j                        5 }
	 d|
j                  cxk  rdk  r&n n#|
j                         D ]  }|  	 ddd       y|
j                          |
j                         }t#        j$                  |
j                  t'        |
j(                        |      # t        j                  j                   $ r@ t#        j$                  |
j                  t'        |
j(                        |
j*                        w xY w# 1 sw Y   yxY ww)	a  
        Download an attachment as a stream of bytes.

        Parameters:
            project_name: The name of the project containing the entity.
            entity_type: The type of entity ("trace" or "span").
            entity_id: The ID of the trace or span containing the attachment.
            file_name: The name of the file to download.
            mime_type: The MIME type of the file.

        Returns:
            Iterator[bytes]: Iterator yielding bytes of the attachment content.
        )r"   r$   r#   NzAttachment not found: z*No download URL available for attachment: GET   i,  )status_codeheadersbody)r5   r6   r7   
ValueErrorlinkr   is_aws_presigned_urlr   
get_cachedr   streamr;   
iter_bytesreadjsondecoderJSONDecodeErrorrest_api_coreApiErrordictr<   text)r   r"   r$   r#   r6   r7   attachments_detailsattachment_to_downloadattachment_detailshttpx_client_uploadr4   chunkresponse_jsons                r   download_attachmentz$AttachmentClient.download_attachmentb   s    * #66%# 7 

 CG"5",,	9&00I=);& #6 ")5i[ABB!&&.I)UVV //0F0K0KL &&((( 	 !''/E/J/JKx(..44!)!4!4!6# "7 LK  (  (($00X--."  <<// #,, ( 4 4 !1!12!  LKsG   +GGB
GF:
1E<	G E%5F:AF77F::G?G	file_pathc                     t         j                  j                  |      st        d|       |t         j                  j	                  |      }|t        j                  |      \  }}t         j                  j                  |      }t        j                  | j                  j                  d            j                  d      }	t        j                  ||||||||	d	      }
t        j                   |
| j"                  | j$                         y)a  
        Upload an attachment for a specific entity (trace or span).

        Parameters:
            project_name: The name of the project containing the entity.
            entity_type: The type of entity ("trace" or "span").
            entity_id: The ID of the trace or span to attach the file to.
            file_path: The path to the file to upload on the local filesystem.
            file_name: The name to assign to the uploaded file. If not provided, uses the basename of file_path.
            mime_type: The MIME type of the file. If not provided, attempts to automatically detect based on the file extension.

        Returns:
            None
        zFile not found: Nr(   F)	rS   r6   	file_sizer7   r$   r#   r"   encoded_url_overridedelete_after_upload)r
   r   upload_httpx_client)osr*   existsFileNotFoundErrorbasename	mimetypes
guess_typegetsizer,   r-   r   r.   r/   r
   FileUploadOptionsr	   upload_attachmentr   r   )r   r"   r$   r#   rS   r6   r7   _rU   rV   upload_optss              r   ra   z"AttachmentClient.upload_attachment   s    . ww~~i(#&6yk$BCC((3I$//	:LIqGGOOI.	%//%%g. 

&/ 	 %66#%!5 %

 	''&)) $ 7 7	
r!   )NN)__name__
__module____qualname____doc__rest_api_clientOpikApistrhttpxClientr    r   r   r   r5   r   bytesrR   r   ra    r!   r   r   r      s+   4$,,4 4 	4
 !<<4 
44&& & _-	&
 
#	$&BBB _-B 	B
 B B 
%BT $(#'5
5
 _-5
 	5

 5
 C=5
 C=5
 
5
r!   r   )%r,   loggingrY   r]   rk   json.decoderrE   opikr   typingr   r   r   r   typing_extensionsr   opik.file_uploadr	   r
   opik.rest_apir   rh   opik.rest_api.typesr   rest_api_attachmentr   rH   r    r   	getLoggerrd   LOGGER
Attachmentr   __annotations__r   rn   r!   r   <module>r}      sd      	      4 4 ' : 3 A /  			8	$#6#A#A y AD
 D
r!   