o
    qmi                     @   s"  d dl Z d dlmZ ddlmZ ddlmZ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 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  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( e )e j*dZ+G dd dZ,G dd dZ-dS )    N)JSONDecodeError   )ApiError)AsyncClientWrapperSyncClientWrapper)AsyncHttpResponseHttpResponse)jsonable_encoder)parse_obj_as)RequestOptions)'convert_and_respect_annotation_metadata)BadRequestError)ForbiddenError)InternalServerError)ServiceUnavailableError)TooManyRequestsError)"DocDigitizationJobParametersParams)$DocDigitizationWebhookCallbackParams) DocDigitizationCreateJobResponse)$DocDigitizationDownloadFilesResponse) DocDigitizationJobStatusResponse)"DocDigitizationUploadFilesResponse.c                
   @      e Zd ZdefddZeedddeje deje	 deje
 d	ee fd
dZdddedeje deje
 d	ee fddZdddedeje
 d	ee fddZdddedeje
 d	ee fddZdddedeje
 d	ee fddZdS )RawDocumentIntelligenceClientclient_wrapperc                C   
   || _ d S N_client_wrapperselfr    r!   ]/home/ubuntu/.local/lib/python3.10/site-packages/sarvamai/document_intelligence/raw_client.py__init__      
z&RawDocumentIntelligenceClient.__init__Njob_parameterscallbackrequest_optionsr&   r'   r(   returnc             
   C   s  | j jjd| j  jdt|tddt|tdddddi|td}zd	|j	  kr-d
k rBn nt
ttt| d}t||dW S |j	dkrbtt|jt
t
jt
j tt
jt
j | dd|j	dkrtt|jt
t
jt
j tt
jt
j | dd|j	dkrtt|jt
t
jt
j tt
jt
j | dd|j	dkrtt|jt
t
jt
j tt
jt
j | dd|j	dkrtt|jt
t
jt
j tt
jt
j | dd| }W n ty   t|j	t|j|jdw t|j	t|j|d)a@  
        Creates a new Document Intelligence job.

        **Supported Languages (BCP-47 format):**
        - `hi-IN`: Hindi (default)
        - `en-IN`: English
        - `bn-IN`: Bengali
        - `gu-IN`: Gujarati
        - `kn-IN`: Kannada
        - `ml-IN`: Malayalam
        - `mr-IN`: Marathi
        - `or-IN`: Odia
        - `pa-IN`: Punjabi
        - `ta-IN`: Tamil
        - `te-IN`: Telugu
        - `ur-IN`: Urdu
        - `as-IN`: Assamese
        - `bodo-IN`: Bodo
        - `doi-IN`: Dogri
        - `ks-IN`: Kashmiri
        - `kok-IN`: Konkani
        - `mai-IN`: Maithili
        - `mni-IN`: Manipuri
        - `ne-IN`: Nepali
        - `sa-IN`: Sanskrit
        - `sat-IN`: Santali
        - `sd-IN`: Sindhi

        **Output Formats (delivered as ZIP file):**
        - `html`: Structured HTML files with layout preservation
        - `md`: Markdown files (default)
        - `json`: Structured JSON files for programmatic processing

        Parameters
        ----------
        job_parameters : typing.Optional[DocDigitizationJobParametersParams]
            Configuration parameters for the Document Intelligence job including language and output format. Defaults to Hindi (hi-IN) and Markdown output if omitted.

        callback : typing.Optional[DocDigitizationWebhookCallbackParams]
            Optional webhook for completion notification

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[DocDigitizationCreateJobResponse]
            Successful Response
        doc-digitization/job/v1POSTwriteobject_
annotation	directionr&   r'   content-typeapplication/jsonbase_urlmethodjsonheadersr(   omit   ,  type_r.   responsedata  r8   body        status_coder8   rC   )r   httpx_clientrequestget_environmentbaser   r   r   OMITrI   typingcastr   r
   r7   r   r   dictr8   OptionalAnyr   r   r   r   r   r   textr    r&   r'   r(   	_response_data_response_jsonr!   r!   r"   
initialise!   s   8
	



















z(RawDocumentIntelligenceClient.initialiser(   job_idfilesc             	   C   s  | j jjd| j  jd||dddi|td}zd|j  kr#dk r8n ntt	t
t	| d	}t||d
W S |jdkrXtt|jttjtj t
tjtj | d	d|jdkrxtt|jttjtj t
tjtj | d	d|jdkrtt|jttjtj t
tjtj | d	d|jdkrtt|jttjtj t
tjtj | d	d|jdkrtt|jttjtj t
tjtj | d	d| }W n ty   t|jt|j|jdw t|jt|j|d)a  
        Returns presigned URLs for uploading input files.

        **File Constraints:**
        - Exactly one file required (PDF or ZIP)
        - PDF files: `.pdf` extension
        - ZIP files: `.zip` extension

        Parameters
        ----------
        job_id : str
            Job identifier returned from Create Job

        files : typing.Sequence[str]
            List of filenames to upload (exactly 1 file: PDF or ZIP)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[DocDigitizationUploadFilesResponse]
            Successful Response
        $doc-digitization/job/v1/upload-filesr+   r[   r\   r2   r3   r4   r:   r;   r<   r>   rA   rB   rD   rE   rF   rG   rH   )r   rJ   rK   rL   rM   rN   rI   rO   rP   r   r
   r7   r   r   rQ   r8   rR   rS   r   r   r   r   r   r   rT   r    r[   r\   r(   rV   rW   rX   r!   r!   r"   get_upload_links   s   




















z.RawDocumentIntelligenceClient.get_upload_linksc             	   C     | j jjdt| d| j  jd|d}zd|j  kr!dk r6n ntt	t
t	| d}t||dW S |jd	krVtt|jttjtj t
tjtj | dd
|jdkrvtt|jttjtj t
tjtj | dd
|jdkrtt|jttjtj t
tjtj | dd
|jdkrtt|jttjtj t
tjtj | dd
|jdkrtt|jttjtj t
tjtj | dd
| }W n ty   t|jt|j|jdw t|jt|j|d)a  
        Validates the uploaded file and starts processing.

        **Validation Checks:**
        - File must be uploaded before starting
        - File size must not exceed 200 MB
        - PDF must be parseable by the PDF parser
        - ZIP must contain only JPEG/PNG images
        - ZIP must be flat (no nested folders beyond one level)
        - ZIP must contain at least one valid image
        - Page/image count must not exceed 500
        - User must have sufficient credits

        **Processing:**
        Job runs asynchronously. Poll the status endpoint or use webhook callback for completion notification.

        Parameters
        ----------
        job_id : str
            The unique identifier of the job

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[DocDigitizationJobStatusResponse]
            Successful Response
        doc-digitization/job/v1//startr+   r5   r6   r(   r:   r;   r<   r>   rA   rB   rD   rE   rF   rG   rH   r   rJ   rK   r	   rL   rM   rI   rO   rP   r   r
   r7   r   r   rQ   r8   rR   rS   r   r   r   r   r   r   rT   r    r[   r(   rV   rW   rX   r!   r!   r"   start   s    




















z#RawDocumentIntelligenceClient.startc             	   C   ra   )a  
        Returns the current status of a job with page-level metrics.

        **Job States:**
        - `Accepted`: Job created, awaiting file upload
        - `Pending`: File uploaded, waiting to start
        - `Running`: Processing in progress
        - `Completed`: All pages processed successfully
        - `PartiallyCompleted`: Some pages succeeded, some failed
        - `Failed`: All pages failed or job-level error

        **Page Metrics:**
        Response includes detailed progress: total pages, pages processed, succeeded, failed, and per-page errors.

        Parameters
        ----------
        job_id : str
            The unique identifier of the job

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[DocDigitizationJobStatusResponse]
            Successful Response
        rb   /statusGETrd   r:   r;   r<   r>   rA   rB   rD   rE   rF   rG   rH   re   rf   r!   r!   r"   
get_status  s   




















z(RawDocumentIntelligenceClient.get_statusc             	   C   ra   )a   
        Returns presigned URLs for downloading output files.

        **Prerequisites:**
        - Job must be in `Completed` or `PartiallyCompleted` state
        - Failed jobs have no output available

        Parameters
        ----------
        job_id : str
            The unique identifier of the job

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[DocDigitizationDownloadFilesResponse]
            Successful Response
        rb   /download-filesr+   rd   r:   r;   r<   r>   rA   rB   rD   rE   rF   rG   rH   )r   rJ   rK   r	   rL   rM   rI   rO   rP   r   r
   r7   r   r   rQ   r8   rR   rS   r   r   r   r   r   r   rT   rf   r!   r!   r"   get_download_links  s   




















z0RawDocumentIntelligenceClient.get_download_links)__name__
__module____qualname__r   r#   rN   rO   rR   r   r   r   r   r   rY   strSequencer   r`   r   rg   rj   r   rl   r!   r!   r!   r"   r      b    
 
p
m
kr   c                
   @   r   )"AsyncRawDocumentIntelligenceClientr   c                C   r   r   r   r   r!   r!   r"   r#   [  r$   z+AsyncRawDocumentIntelligenceClient.__init__Nr%   r&   r'   r(   r)   c             
      s  | j jjd| j  jdt|tddt|tdddddi|tdI d	H }zd
|j	  kr1dk rFn nt
ttt| d}t||dW S |j	dkrftt|jt
t
jt
j tt
jt
j | dd|j	dkrtt|jt
t
jt
j tt
jt
j | dd|j	dkrtt|jt
t
jt
j tt
jt
j | dd|j	dkrtt|jt
t
jt
j tt
jt
j | dd|j	dkrtt|jt
t
jt
j tt
jt
j | dd| }W n ty   t|j	t|j|jdw t|j	t|j|d)aE  
        Creates a new Document Intelligence job.

        **Supported Languages (BCP-47 format):**
        - `hi-IN`: Hindi (default)
        - `en-IN`: English
        - `bn-IN`: Bengali
        - `gu-IN`: Gujarati
        - `kn-IN`: Kannada
        - `ml-IN`: Malayalam
        - `mr-IN`: Marathi
        - `or-IN`: Odia
        - `pa-IN`: Punjabi
        - `ta-IN`: Tamil
        - `te-IN`: Telugu
        - `ur-IN`: Urdu
        - `as-IN`: Assamese
        - `bodo-IN`: Bodo
        - `doi-IN`: Dogri
        - `ks-IN`: Kashmiri
        - `kok-IN`: Konkani
        - `mai-IN`: Maithili
        - `mni-IN`: Manipuri
        - `ne-IN`: Nepali
        - `sa-IN`: Sanskrit
        - `sat-IN`: Santali
        - `sd-IN`: Sindhi

        **Output Formats (delivered as ZIP file):**
        - `html`: Structured HTML files with layout preservation
        - `md`: Markdown files (default)
        - `json`: Structured JSON files for programmatic processing

        Parameters
        ----------
        job_parameters : typing.Optional[DocDigitizationJobParametersParams]
            Configuration parameters for the Document Intelligence job including language and output format. Defaults to Hindi (hi-IN) and Markdown output if omitted.

        callback : typing.Optional[DocDigitizationWebhookCallbackParams]
            Optional webhook for completion notification

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[DocDigitizationCreateJobResponse]
            Successful Response
        r*   r+   r,   r-   r1   r2   r3   r4   Nr:   r;   r<   r>   rA   rB   rD   rE   rF   rG   rH   )r   rJ   rK   rL   rM   r   r   r   rN   rI   rO   rP   r   r
   r7   r   r   rQ   r8   rR   rS   r   r   r   r   r   r   rT   rU   r!   r!   r"   rY   ^  s   8
	



















z-AsyncRawDocumentIntelligenceClient.initialiserZ   r[   r\   c             	      s   | j jjd| j  jd||dddi|tdI dH }zd|j  kr'd	k r<n ntt	t
t	| d
}t||dW S |jdkr\tt|jttjtj t
tjtj | d
d|jdkr|tt|jttjtj t
tjtj | d
d|jdkrtt|jttjtj t
tjtj | d
d|jdkrtt|jttjtj t
tjtj | d
d|jdkrtt|jttjtj t
tjtj | d
d| }W n ty   t|jt|j|jdw t|jt|j|d)a  
        Returns presigned URLs for uploading input files.

        **File Constraints:**
        - Exactly one file required (PDF or ZIP)
        - PDF files: `.pdf` extension
        - ZIP files: `.zip` extension

        Parameters
        ----------
        job_id : str
            Job identifier returned from Create Job

        files : typing.Sequence[str]
            List of filenames to upload (exactly 1 file: PDF or ZIP)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[DocDigitizationUploadFilesResponse]
            Successful Response
        r]   r+   r^   r2   r3   r4   Nr:   r;   r<   r>   rA   rB   rD   rE   rF   rG   rH   )r   rJ   rK   rL   rM   rN   rI   rO   rP   r   r
   r7   r   r   rQ   r8   rR   rS   r   r   r   r   r   r   rT   r_   r!   r!   r"   r`     s   




















z3AsyncRawDocumentIntelligenceClient.get_upload_linksc             	        | j jjdt| d| j  jd|dI dH }zd|j  kr%dk r:n ntt	t
t	| d}t||d	W S |jd
krZtt|jttjtj t
tjtj | dd|jdkrztt|jttjtj t
tjtj | dd|jdkrtt|jttjtj t
tjtj | dd|jdkrtt|jttjtj t
tjtj | dd|jdkrtt|jttjtj t
tjtj | dd| }W n ty   t|jt|j|jdw t|jt|j|d)a  
        Validates the uploaded file and starts processing.

        **Validation Checks:**
        - File must be uploaded before starting
        - File size must not exceed 200 MB
        - PDF must be parseable by the PDF parser
        - ZIP must contain only JPEG/PNG images
        - ZIP must be flat (no nested folders beyond one level)
        - ZIP must contain at least one valid image
        - Page/image count must not exceed 500
        - User must have sufficient credits

        **Processing:**
        Job runs asynchronously. Poll the status endpoint or use webhook callback for completion notification.

        Parameters
        ----------
        job_id : str
            The unique identifier of the job

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[DocDigitizationJobStatusResponse]
            Successful Response
        rb   rc   r+   rd   Nr:   r;   r<   r>   rA   rB   rD   rE   rF   rG   rH   r   rJ   rK   r	   rL   rM   rI   rO   rP   r   r
   r7   r   r   rQ   r8   rR   rS   r   r   r   r   r   r   rT   rf   r!   r!   r"   rg   ]  s    




















z(AsyncRawDocumentIntelligenceClient.startc             	      rt   )a  
        Returns the current status of a job with page-level metrics.

        **Job States:**
        - `Accepted`: Job created, awaiting file upload
        - `Pending`: File uploaded, waiting to start
        - `Running`: Processing in progress
        - `Completed`: All pages processed successfully
        - `PartiallyCompleted`: Some pages succeeded, some failed
        - `Failed`: All pages failed or job-level error

        **Page Metrics:**
        Response includes detailed progress: total pages, pages processed, succeeded, failed, and per-page errors.

        Parameters
        ----------
        job_id : str
            The unique identifier of the job

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[DocDigitizationJobStatusResponse]
            Successful Response
        rb   rh   ri   rd   Nr:   r;   r<   r>   rA   rB   rD   rE   rF   rG   rH   ru   rf   r!   r!   r"   rj     s   




















z-AsyncRawDocumentIntelligenceClient.get_statusc             	      rt   )a%  
        Returns presigned URLs for downloading output files.

        **Prerequisites:**
        - Job must be in `Completed` or `PartiallyCompleted` state
        - Failed jobs have no output available

        Parameters
        ----------
        job_id : str
            The unique identifier of the job

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[DocDigitizationDownloadFilesResponse]
            Successful Response
        rb   rk   r+   rd   Nr:   r;   r<   r>   rA   rB   rD   rE   rF   rG   rH   )r   rJ   rK   r	   rL   rM   rI   rO   rP   r   r
   r7   r   r   rQ   r8   rR   rS   r   r   r   r   r   r   rT   rf   r!   r!   r"   rl   3  s   




















z5AsyncRawDocumentIntelligenceClient.get_download_links)rm   rn   ro   r   r#   rN   rO   rR   r   r   r   r   r   rY   rp   rq   r   r`   r   rg   rj   r   rl   r!   r!   r!   r"   rs   Z  rr   rs   ).rO   json.decoderr   core.api_errorr   core.client_wrapperr   r   core.http_responser   r   core.jsonable_encoderr	   core.pydantic_utilitiesr
   core.request_optionsr   core.serializationr   errors.bad_request_errorr   errors.forbidden_errorr   errors.internal_server_errorr    errors.service_unavailable_errorr   errors.too_many_requests_errorr   (requests.doc_digitization_job_parametersr   *requests.doc_digitization_webhook_callbackr   *types.doc_digitization_create_job_responser   .types.doc_digitization_download_files_responser   *types.doc_digitization_job_status_responser   ,types.doc_digitization_upload_files_responser   rP   rS   rN   r   rs   r!   r!   r!   r"   <module>   s6       A