
    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mZ 	 	 	 	 	 	 	 ddee         dee         deee                  dee         dededee         dee         fdZ	 	 	 	 	 	 	 ddeee                  deee                  deee                  deee                  dededeee                  deee                  fdZdS )    N)IOListOptional)UnstructuredClient)shared)Element)logger)exactly_one)dict_to_elementselements_from_json.https://api.unstructured.io/general/v0/general filenamecontent_typefilefile_filenameapi_urlapi_keymetadata_filenamereturnc                    t          | |           |r|rt          d          ||}t          j        d           d|v r
|dd         n|}t	          ||          }	| Qt          | d          5 }
t          j        |
                                | 	          }ddd           n# 1 swxY w Y   n)|'|t          d
          t          j        ||	          }t          j	        dd|i|}|	j
                            |          }|j        dk    rt          |j        j                  S t          d|j         d          )a  Partitions a document using the Unstructured REST API. This is equivalent to
    running the document through partition.

    See https://api.unstructured.io/general/docs for the hosted API documentation or
    https://github.com/Unstructured-IO/unstructured-api for instructions on how to run
    the API locally as a container.

    Parameters
    ----------
    filename
        A string defining the target filename path.
    content_type
        A string defining the file content in MIME type
    file
        A file-like object using "rb" mode --> open(filename, "rb").
    metadata_filename
        When file is not None, the filename (string) to store in element metadata. E.g. "foo.txt"
    api_url
        The URL for the Unstructured API. Defaults to the hosted Unstructured API.
    api_key
        The API key to pass to the Unstructured API.
    request_kwargs
        Additional parameters to pass to the data field of the request to the Unstructured API.
        For example the `strategy` parameter.
    )r   r   zOnly one of metadata_filename and file_filename is specified. metadata_filename is preferred. file_filename is marked for deprecation.NzuThe file_filename kwarg will be deprecated in a future version of unstructured. Please use metadata_filename instead.z/general/v0/generali)api_key_auth
server_urlrb)content	file_namezWIf file is specified in partition_via_api, metadata_filename must be specified as well.files   )textReceive unexpected status code  from the API. )r
   
ValueErrorr	   warnr   openr   FilesreadPartitionParametersgeneral	partitionstatus_coder   raw_responser   )r   r   r   r   r   r   r   request_kwargsbase_urlsdkfr   reqresponses                 d/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/unstructured/partition/api.pypartition_via_apir4      s   F ---- 
] 
W
 
 	

  )4	
 	
 	
 !6 @ @wtt}}gH
'h
G
G
GC(D!! 	QL"  E	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
	$?   '
 
 

 
$  
 C {$$S))Hs""!x'<'ABBBBRh.BRRR
 
 	
s   0)B%%B),B)	filenamescontent_typesr   file_filenamesmetadata_filenamesc                    d|d}|r|rt          d          ||}t          j        d           | |r/t          |          t          |           k    rt          d          t	          j                    5 fd| D             }g }	t          |          D ]3\  }
}| |
         }|||
         nd}|	                    d|||ff           4t          j	        ||||		          }ddd           n# 1 swxY w Y   n||r/t          |          t          |          k    rt          d
          |st          d          t          |          t          |          k    rt          d          g }	t          |          D ]3\  }
}|||
         nd}||
         }|	                    d|||ff           4t          j	        ||||		          }|j
        dk    r]g }|                                }t          |d         t                    s|g}|D ]$}|                    t          |                     %|S t          d|j
         d          )a  Partitions multiple documents using the Unstructured REST API by batching
    the documents into a single HTTP request.

    See https://api.unstructured.io/general/docs for the hosted API documentation or
    https://github.com/Unstructured-IO/unstructured-api for instructions on how to run
    the API locally as a container.

    Parameters
    ----------
    filenames
        A list of strings defining the target filename paths.
    content_types
        A list of strings defining the file contents in MIME types.
    files
        A list of file-like object using "rb" mode --> open(filename, "rb").
    metadata_filename
        When file is not None, the filename (string) to store in element metadata. E.g. "foo.txt"
    api_url
        The URL for the Unstructured API. Defaults to the hosted Unstructured API.
    api_key
        The API key to pass to the Unstructured API.
    request_kwargs
        Additional parameters to pass to the data field of the request to the Unstructured API.
        For example the `strategy` parameter.
    zapplication/json)ACCEPTzUNSTRUCTURED-API-KEYzOnly one of metadata_filenames and file_filenames is specified. metadata_filenames is preferred. file_filenames is marked for deprecation.NzwThe file_filenames kwarg will be deprecated in a future version of unstructured. Please use metadata_filenames instead.z6content_types and filenames must have the same length.c                 V    g | ]%}                     t          |d                     &S )r   )enter_contextr%   ).0r0   stacks     r3   
<listcomp>z.partition_multiple_via_api.<locals>.<listcomp>   s/    KKKAU((a77KKK    r   )headersdatar   z2content_types and files must have the same length.z8metadata_filenames must be specified if files are passedz7metadata_filenames and files must have the same length.r   r   r    r!   )r#   r	   r$   len
contextlib	ExitStack	enumerateappendrequestspostr+   json
isinstancelistr   )r5   r6   r   r7   r   r   r8   r-   rA   _filesir   r   r   r2   _file	documentsresponse_listdocumentr>   s                      @r3   partition_multiple_via_apirS   i   s4   H % ' G
  
n 
Y
 
 	

 !+5	
 	
 	

  	WS//3y>>AAUVVV!## 	uKKKKKKKEF$U++ I I4$Q<3@3L}Q//RVw4(FGHHHH}#	  H	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  
	 	SS//3u::==QRRR! 	XWXXX#$$E

22VWWW!%(( 	F 	FHAu/</H=++dL)!,HMM7Xul$CDEEEE=	
 
 
 s""	  -*D11 	,*OM% 	9 	9H-h778888Rh.BRRR
 
 	
s   8A,C00C47C4)NNNNr   r   N)rD   typingr   r   r   rH   unstructured_clientr   unstructured_client.modelsr   unstructured.documents.elementsr   unstructured.loggerr	   unstructured.partition.commonr
   unstructured.staging.baser   r   strbytesr4   rS   r"   r@   r3   <module>r]      s2                 2 2 2 2 2 2 - - - - - - 3 3 3 3 3 3 & & & & & & 5 5 5 5 5 5 J J J J J J J J #"& $#'C'+T
 T
smT
3-T
 2e9
T
 C=	T

 T
 T
  }T
 
']T
 T
 T
 T
p &*)-!%*.C.2m
 m
S	"m
DI&m
 DIm
 T#Y'	m

 m
 m
 !c+m
 
$w-m
 m
 m
 m
 m
 m
r@   