
    h"                     j    d dl Z d dlmZm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  G d de      Zy)    N)BinaryIOList)	BaseUTApi)DeleteFileResponseListFileResponseUploadResultUsageInfoResponse)snakifyc                       e Zd ZdZ	 	 ddedededz  dedef
dZd	edefd
Z		 	 dde
ee
   z  dededz  dee   fdZ	 ddeee   z  dedz  defdZ	 ddedz  dedz  defdZdefdZy)
AsyncUTApizAsynchronous UploadThing API client.

    This class provides asynchronous methods for interacting with the
    UploadThing API. Use this client for async/await operations.
    Nmethodpathdatatimeoutreturnc                 ~  K   | j                  |||      \  }}}|xs i }|j                  d|i       	 t        j                         4 d{   } |j                  d|||d| d{   }	|	j                          t        |	j                               }
t        |
t              r|
cddd      d{    S t        d      7 y7 ]7 # 1 d{  7  sw Y   yxY w# t        j                  $ r t        j                  d| d| d      dt        j                  $ r}| j                  |        d}~ww xY ww)	a  Make an async HTTP request to the UploadThing API.

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

        Returns:
            dict: Parsed JSON response

        Raises:
            httpx.TimeoutException: If the request times out
            httpx.HTTPStatusError: If the server returns an error status
        r   N)r   urlheaderszExpected dict responsezRequest to z timed out after z seconds )_prepare_requestupdatehttpxAsyncClientrequestraise_for_statusr
   json
isinstancedict	TypeErrorTimeoutExceptionHTTPStatusError_handle_error_response)selfr   r   r   r   r   r   request_kwargsclientresponseresultes               U/var/www/Befach/backend/env/lib/python3.12/site-packages/upyloadthing/async_client.py_requestzAsyncUTApi._request   sU    , (,'<'<D$(
$Wn (-2y'23	((* : :f!/ "!sG"?M"  ))+ 1fd+!: : :   899:: : : : %% 	((cU"3G9HE $$ 	''*	s   1D=C! CC! C*C+>C)C! 5C
6C! :D=;CC! C
C! CCCC! D=C! !AD:#D55D::D=	file_datac           	         K   | j                  d|d   d|d   |d   |d   fi       d{   }t        d|d   |d   |d	   |d   d
|S 7  w)zUpload a single file to UploadThing.

        Args:
            file_data: Dictionary containing file metadata and content

        Returns:
            UploadResult: Result of the upload operation
        PUT
ingest_urlfilenametype)r   Nfile_keysize)r2   r0   r3   r1   r   )r*   r   )r#   r+   r'   s      r)   _upload_single_filezAsyncUTApi._upload_single_fileE   s      }}l#f%f%f% % 

 

  
z*6"6"6"	

 
 	


s   )AA!Afilescontent_dispositionaclc                    K   t        |t              s|g}|D cg c]  }| j                  |||       }}t        j                  |D cg c]  }| j                  |       c}  d{   }|S c c}w c c}w 7 w)a  Upload one or more files to UploadThing asynchronously.

        Args:
            files: Single file or list of files to upload (file-like objects)
            content_disposition: Content disposition header value ('inline' or 'attachment')
            acl: Access control list setting for uploaded files

        Returns:
            List[UploadResult]: List of upload results containing file information
        N)r   list_prepare_file_dataasynciogatherr4   )r#   r5   r6   r7   r/   
files_datar+   resultss           r)   upload_fileszAsyncUTApi.upload_filesb   s       %&GE 
 ##D*=sC

 
  CMNid&&y1N
 
 
 O
s'   A;A/A;	A4!A;(A9)A;keyskey_typec                    K   t        |t              r|gn|}|dk(  rdnd|i}| j                  dd|       d{   }t        di |S 7 w)a8  Delete one or more files from UploadThing asynchronously.

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

        Returns:
            DeleteFileResponse: Response containing deletion results
        r2   fileKeys	customIdsPOSTz/v6/deleteFilesNr   )r   strr*   r   )r#   r@   rA   	keys_listr   r'   s         r)   delete_fileszAsyncUTApi.delete_files   s[      )s3TF	"j0Jk9
 }}V->EE!+F++ Fs   7AA	Alimitoffsetc                 z   K   i }|r||d<   |r||d<   | j                  dd|       d{   }t        di |S 7 w)a
  List files stored in UploadThing asynchronously.

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

        Returns:
            ListFileResponse: Response containing list of files
        rI   rJ   rE   z/v6/listFilesNr   )r*   r   )r#   rI   rJ   paramsr&   s        r)   
list_fileszAsyncUTApi.list_files   sO      #F7O%F8vGG+(++ Hs   ';9;c                 X   K   | j                  dd       d{   }t        di |S 7 w)zGet usage information for the UploadThing account asynchronously.

        Returns:
            UsageInfoResponse: Response containing usage statistics
        rE   z/v6/getUsageInfoNr   )r*   r	   )r#   r'   s     r)   get_usage_infozAsyncUTApi.get_usage_info   s0      }}V-?@@ *6** As   *(*)Ng      >@)inlinezpublic-read)r2   )NN)__name__
__module____qualname____doc__rF   r   floatr*   r   r4   r   r   r?   r   rH   intr   rM   r	   rO   r       r)   r   r      s
    !,, , Tk	,
 , 
,\
4 
L 
@ $,'	$x.( ! 4Z	
 
l	@ =G,$s)O,/2Tz,	,( >B,4Z,03d
,	,*+&7 +rW   r   )r;   typingr   r   r   upyloadthing.base_clientr   upyloadthing.schemasr   r   r   r	   upyloadthing.utilsr
   r   r   rW   r)   <module>r\      s.     !  .  '`+ `+rW   