
    h~                         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mZ d dlm	Z	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mZmZmZmZmZ d dlmZ dZd	Zd
Z G d de      Zy)    N)ABCabstractmethod)AnyBinaryIO	CoroutineList)generate_key)make_presigned_url)DeleteFileResponseListFileResponseUploadResultUsageInfoResponseUTApiOptionsUTtoken)snakifyz7.4.4z
server-sdkzhttps://api.uploadthing.comc                      e Zd ZdZddedz  fdZdefdZe	 dde	de	d	edz  dee
eeef   z  fd
       Ze	 	 ddeee   z  de	de	dz  dee   e
eeee   f   z  fd       Ze	 dde	ee	   z  de	dz  dee
eeef   z  fd       Ze	 ddedz  dedz  dee
eeef   z  fd       Zedee
eeef   z  fd       Zdede	de	dz  defdZ	 dde	de	d	edz  dee	eedz  f   fdZdej8                  ddfdZy)	BaseUTApizBase class for UploadThing API client.

    This abstract class defines the interface for both synchronous and
    asynchronous clients.
    Noptionsc                    |xs
 t               | _        |r|j                  nt        j                  d      }|st        d      t        t        j                  t        j                  |      j                  d                  }t        j                  |      | _        |r|j                  r|j                  n/t        j                  d      xs | j                  j                  d   | _        t         | _        y )NUPLOADTHING_TOKENzUPLOADTHING_TOKEN is requiredzutf-8UPLOADTHING_REGIONr   )r   r   tokenosgetenv
ValueErrorr   jsonloadsbase64	b64decodedecoder   model_validateregionregionsAPI_URLapi_url)selfr   	b64_tokendecoded_tokens       T/var/www/Befach/backend/env/lib/python3.12/site-packages/upyloadthing/base_client.py__init__zBaseUTApi.__init__#   s    0,.$GMM"))4G*H 	 <==JJv''	299'BC
 ++M:
 7>> NN/0IDJJ4F4Fq4I 	
     returnc                 F    t         t        | j                  j                  dS )zCreate headers required for UploadThing API requests.

        Returns:
            dict: Headers containing SDK version, adapter type, and API key
        )zx-uploadthing-versionzx-uploadthing-be-adapterzx-uploadthing-api-key)SDK_VERSION
BE_ADAPTERr   api_keyr&   s    r)   _make_headerszBaseUTApi._make_headers5   s!     &1(2%)ZZ%7%7
 	
r+   methodpathdatac                      y)a   Make an HTTP request to the UploadThing API.

        Args:
            method: HTTP method to use
            path: API endpoint path
            data: Request data/parameters

        Returns:
            Response data as dictionary or coroutine
        N )r&   r3   r4   r5   s       r)   _requestzBaseUTApi._requestA       " 	r+   filescontent_dispositionaclc                      y)a>  Upload one or more files to UploadThing.

        Args:
            files: Single file or list of files to upload
            content_disposition: Content disposition header value
            acl: Access control list setting for uploaded files

        Returns:
            List of upload results or coroutine
        Nr7   )r&   r:   r;   r<   s       r)   upload_fileszBaseUTApi.upload_filesT   r9   r+   keyskey_typec                      y)a  Delete one or more files from UploadThing.

        Args:
            keys: Single key or list of keys identifying files to delete
            key_type: Type of key provided ('file_key' or 'custom_id')

        Returns:
            Delete operation response or coroutine
        Nr7   )r&   r?   r@   s      r)   delete_fileszBaseUTApi.delete_filesg        	r+   limitoffsetc                      y)zList files stored in UploadThing.

        Args:
            limit: Maximum number of files to return
            offset: Number of files to skip

        Returns:
            File listing response or coroutine
        Nr7   )r&   rD   rE   s      r)   
list_fileszBaseUTApi.list_filesv   rC   r+   c                      y)zGet usage information for the UploadThing account.

        Returns:
            Usage information response or coroutine
        Nr7   r1   s    r)   get_usage_infozBaseUTApi.get_usage_info   s     	r+   filec                    t        j                         j                  }t        || j                  j
                        }t        |ddt        j                                }|j                  dd      }|j                  d       t        j                  |      d   xs d}t        | j                  || j                  j                  | j                  j
                  ||||||
      }	|||||||	dS )a6  Prepare file metadata and presigned URL for upload.

        Args:
            file: File-like object to upload
            content_disposition: Content disposition header value
            acl: Access control list setting

        Returns:
            dict: Prepared file data including presigned URL
        nameupload_r      zapplication/octet-stream)file_keyrL   sizetype	custom_idrJ   
ingest_url)uuiduuid4hexr	   r   app_idgetattrseek	mimetypes
guess_typer
   r"   r0   )
r&   rJ   r;   r<   	file_seedrO   	file_name	file_size	file_typerS   s
             r)   _prepare_file_datazBaseUTApi._prepare_file_data   s     JJL$$		4::+<+<=D&GDJJL>*BC	IIaO			!  +A.L2L 	 (KKJJJJ

 !"$
 	
r+   c                     |j                  d      r| j                   | }n|}| j                         }d t        |t              r2t        fd|j                         D              }|r||d|ifS ||d|ifS ||dfS )zPrepare common request parameters.

        Args:
            method: HTTP method to use
            path: API endpoint path
            data: Request data/parameters

        Returns:
            tuple: (url, headers, prepared_data)
        /c                 H    t        | d      xr t        | j                        S )Nread)hasattrcallablerd   )objs    r)   is_file_likez0BaseUTApi._prepare_request.<locals>.is_file_like   s    3'>HSXX,>>r+   c              3   x   K   | ]1  }t        |t              xr t        |      d k\  xr  |d          3 yw)rN      N)
isinstancetuplelen).0vrh   s     r)   	<genexpr>z-BaseUTApi._prepare_request.<locals>.<genexpr>   s>       1e$KQ1Kad9KKs   7:r:   r   N)
startswithr%   r2   rk   dictanyvalues)r&   r3   r4   r5   urlheaders	has_filesrh   s          @r)   _prepare_requestzBaseUTApi._prepare_request   s     ??3\\N4&)CC$$&	? dD!  I Ggt_44&$//GT!!r+   ec                     d|j                   j                   }	 |j                   j                         }d|v r|d|d    z  }t	        j
                  ||j                  |j                         |# t        $ r Y 8w xY w)zHandle HTTP error responses consistently.

        Args:
            e: The HTTP error exception

        Raises:
            httpx.HTTPStatusError: Enhanced error with more details
        zUploadThing API error: errorz - )requestresponse)r}   status_coder   	ExceptionhttpxHTTPStatusErrorr|   )r&   ry   	error_msg
error_datas       r)   _handle_error_responsez BaseUTApi._handle_error_response   s     .ajj.D.D-EF		*J*$s:g#6"788	 ##qyy1::
	  		s   )A1 1	A=<A=)N)inlinezpublic-read)rO   )NN)__name__
__module____qualname____doc__r   r*   rr   r2   r   strr   r   r8   r   r   r   r>   r   rB   intr   rG   r   rI   r`   rl   rx   r   r   r   r7   r+   r)   r   r      s'   t 3 $

t 

 
 !	  Tk	
 
	#sD.)	) $  $,'	$x.( ! 4Z	
 
l	iS$|2D(DE	E $ <F$s)O/2Tz	iS2D(DE	E  =A4Z03d
	Ic30@&@A	A  	YsC1B'BC	C +
+
36+
=@4Z+
	+
\ ;? " "!$ ",04K "	sD$+%	& "D(=(= $ r+   r   )r   r   rZ   r   rT   abcr   r   typingr   r   r   r   r   upyloadthing.file_keyr	   upyloadthing.presignr
   upyloadthing.schemasr   r   r   r   r   r   upyloadthing.utilsr   r.   r/   r$   r   r7   r+   r)   <module>r      sR       	  # 1 1  . 3  '

'U Ur+   