a
    ž˜ëjÏ  ã                   @  sX  d dl mZ 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mZ d dlmZ d dlmZmZmZmZmZmZmZmZ d d	lmZ d d
lmZm Z m!Z!m"Z"m#Z# e $e¡Z%dddœdd„Z&ddddœdd„Z'ddddddœdd„Z(ddddddœdd „Z)d!d!d"œd#d$„Z*d%dd&œd'd(„Z+d)dd*œd+d,„Z,dS )-é    )ÚannotationsN)ÚTupleÚAnyÚBinaryIO)Úurlparse)ÚURL)Ú	DataFieldÚ	FileField)ÚUNSTRUCTURED_CLIENT_LOGGER_NAME)ÚPARTITION_FORM_FILES_KEYÚ!PARTITION_FORM_SPLIT_PDF_PAGE_KEYÚ)PARTITION_FORM_SPLIT_PDF_ALLOW_FAILED_KEYÚ'PARTITION_FORM_SPLIT_CACHE_TMP_DATA_KEYÚ+PARTITION_FORM_SPLIT_CACHE_TMP_DATA_DIR_KEYÚPARTITION_FORM_PAGE_RANGE_KEYÚ'PARTITION_FORM_STARTING_PAGE_NUMBER_KEYÚFormData)Úshared)ÚBackoffStrategyÚRetriesÚRetryConfigÚretry_asyncÚserialize_request_bodyzhttpx.Requestzdict[str, Any])ÚrequestÚreturnc                 C  sä   | j  dd¡}d|vri S | jdu s0t| jdƒs4i S | jj}i }|D ]š}t|tƒr˜d|jv rŠ|j dd¡}||vrzg ||< ||  	|j
¡ |j
||j< qDt|tƒrD|jdu s¶|j ¡ s¾tdƒ‚|j|j  dd¡|jdœ||j< qD|S )	zËExtracts the multipart fields from the request.

    Args:
        request: The request object.

    Returns:
        The multipart fields.

    Raises:
        Exception: If the filename is not set
    úContent-TypeÚ Ú	multipartNÚfieldsú[]z"Filename can't be an empty string.)ÚfilenameÚcontent_typeÚfile)ÚheadersÚgetÚstreamÚhasattrr   Ú
isinstancer   ÚnameÚreplaceÚappendÚvaluer	   r    ÚstripÚ
ValueErrorr"   )r   r!   r   Zmapped_fieldsÚfieldr(   © r/   ún/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured_client/_hooks/custom/request_utils.pyÚget_multipart_stream_fields%   s.    


ýr1   r   Úint)Ú	form_dataÚpage_numberr   c                   sH   t tttt dd¡tttg‰ ‡ ‡fdd„ˆD ƒ}d|t < t|ƒ|t< |S )zÉCreates the request body for the partition API."

    Args:
        form_data: The form data.
        page_number: The page number.

    Returns:
        The updated request payload for the chunk.
    r   r   c                   s   i | ]}|ˆ vr|ˆ| “qS r/   r/   )Ú.0Úkey©Zfields_to_dropr3   r/   r0   Ú
<dictcomp>b   s   z3create_pdf_chunk_request_params.<locals>.<dictcomp>Úfalse)	r   r   r   r   r)   r   r   r   Ústr)r3   r4   Zchunk_payloadr/   r7   r0   Úcreate_pdf_chunk_request_paramsL   s    
ø
ÿr;   zTuple[BinaryIO, int]r:   )r3   Ú	pdf_chunkÚoriginal_requestr    r   c                 C  sœ   |\}}t | |ƒ}t|jƒ}t|tjƒr0| ¡ n|}tjf dtj	||dd�i|¤Ž}	t
|	dddtjƒ}
|
du rvtdƒ‚tjd|jp„d	i |¥|
j|
j|
jd
�S )a“  Creates a new request object with the updated payload for the partition API.

    Args:
        form_data: The form data.
        pdf_chunk: Tuple of pdf chunk contents (can be both io.BytesIO or
            a file object created with e.g. open()) and the page number.
        original_request: The original request.
        filename: The filename.

    Returns:
        The updated request object.
    Úfileszapplication/pdf)ÚcontentÚ	file_namer!   Fr   Nz%Failed to serialize the request body.ÚPOSTr   )ÚmethodÚurlr#   r?   Údatar>   )r;   Úprepare_request_headersr#   r'   ÚioÚBytesIOÚgetvaluer   ZPartitionParametersZFilesr   r-   ÚhttpxÚRequestrC   r?   rD   r>   )r3   r<   r=   r    Úpdf_chunk_filer4   rD   Zoriginal_headersZpdf_chunk_contentZpdf_chunk_partition_paramsZserialized_bodyr/   r/   r0   Úcreate_pdf_chunk_requestj   sB    


ÿ
ýýÿúûúrL   zhttpx.AsyncClientr   zasyncio.Semaphorezhttpx.Response)ÚclientÚpdf_chunk_requestrK   Úlimiterr   c                 ƒ  sH  d}d}t dt|d |d |d dd�d	d
�}dg}‡ ‡fdd„}|4 I d H šÞ z¤zLt|t||ƒƒI d H }	|	W W t|tjƒsŠ|jsŠ| ¡  W  d   ƒI d H  S  t	yÖ }
 z t
jd|
|
d� |
‚W Y d }
~
n
d }
~
0 0 W t|tjƒsô|jsô| ¡  n t|tjƒ�s|j�s| ¡  0 W d   ƒI d H  �qD1 I d H �s:0    Y  d S )Niè  i`ê  Úbackoffé   é   é   g®Gázþ?)Zinitial_intervalZmax_intervalZmax_elapsed_timeÚexponentT)Zretry_connection_errorsZ5xxc                   “  s   ˆ   ˆ¡I d H S )N)Úsendr/   ©rM   rN   r/   r0   Ú
do_request·   s    z"call_api_async.<locals>.do_requestzRequest failed with error: %s)Úexc_info)r   r   r   r   r'   rF   rG   ÚclosedÚcloseÚ	ExceptionÚloggerÚerror)rM   rN   rK   rO   Z
one_secondZ
one_minuteZretry_configZretryable_codesrW   ÚresponseÚer/   rV   r0   Úcall_api_async¡   s<    üø
ÿ
ú
ÿr`   zhttpx.Headers)r#   r   c                 C  s$   |   ¡ }| dd¡ | dd¡ |S )zËPrepare the request headers by removing the 'Content-Type' and 'Content-Length' headers.

    Args:
        headers: The original request headers.

    Returns:
        The modified request headers.
    r   NúContent-Length)ÚcopyÚpop)r#   Únew_headersr/   r/   r0   rE   È   s    rE   Úlist)Úelementsr   c                 C  sL   t jdddid�}t | ¡ ¡ }tt|ƒƒ}|j d|i¡ t	|d|ƒ |S )zñ
    Creates a modified response object with updated content.

    Args:
        elements: The list of elements to be serialized and added to
        the response.

    Returns:
        The modified response object with updated content.
    éÈ   r   zapplication/json)Ústatus_coder#   ra   Ú_content)
rI   ÚResponseÚjsonÚdumpsÚencoder:   Úlenr#   ÚupdateÚsetattr)rf   r^   r?   Úcontent_lengthr/   r/   r0   Úcreate_responseÙ   s    ÿrr   z	str | URL)rC   r   c                 C  s   t t| ƒƒ}|j› d|j› �S )zsExtracts the base URL from the given URL.

    Args:
        url: The URL.

    Returns:
        The base URL.
    z://)r   r:   ÚschemeÚnetloc)rC   Ú
parsed_urlr/   r/   r0   Úget_base_urlî   s    	rv   )-Ú
__future__r   ZasynciorF   rk   ÚloggingÚtypingr   r   r   Úurllib.parser   rI   r   Zhttpx._multipartr   r	   Z(unstructured_client._hooks.custom.commonr
   Z,unstructured_client._hooks.custom.form_utilsr   r   r   r   r   r   r   r   Zunstructured_client.modelsr   Zunstructured_client.utilsr   r   r   r   r   Ú	getLoggerr\   r1   r;   rL   r`   rE   rr   rv   r/   r/   r/   r0   Ú<module>   s*   (

'7'