o
    R+ i"                     @   s  d Z ddlZddlZddl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 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ZddlmZ ddlmZ ddlmZ ddlZeeZdd Z G dd de
Z!dS )z3
This module provides a client class for infinite.
    N)utils)compat)bce_v1_signer)BceBaseClient)bce_http_client)handler)http_headers)http_content_types)http_methods)required)infinite)BceClientError)BceServerError)BceClientConfigurationc                 C   s   | j d tjjd krtd| j d tjjd kr.|  }|r(|jd|i | 	  dS t
| j|jjd}| j |_| 	  |)Nd   z#Can not handle 1xx http status codeZBodyT)Z
request_id)statushttpclientCONTINUEr   OKread__dict__updatecloser   reasonmetadataZbce_request_idstatus_code)http_responseresponsebodyZbse r    v/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/baidubce/services/infinite/infinite_client.py_parse_http_response,   s   r"   c                   @   s~   e Zd ZdZdddZ		dddZ			dd	d
ZdddZdddZe	dddZ
dd ZdddZ				dddZdS )InfiniteClientz
    Infinite sdk client
    Nc                 C   s   t | | d S N)r   __init__selfconfigr    r    r!   r%   ?   s   zInfiniteClient.__init__application/jsonpredictc           
      C   f   i }|dur
||d< d|d< ||d< t j}|d | }tj|tjt i}	| jtj	|d ||	||dS )	aY  
        predict

        :param endpoint_name: endpoint name
        :type endpoint_name: string

        :param body: request data
        :type body: binary string or dict

        :param variant_name: variant name or None
        :type variant_name: string

        :param content_type: content type,supports application/json,x-image,and x-recordio-protobuf
        :type content_type: string

        :param config: None
        :type config: BceClientConfiguration

        :param interface: interface_name,
            several of predict/predict_proba/predict_log_proba/fit_predict/staged_predict/staged_predict are supported
            depend on frameworks and algorithm used

        :return: response as following format
            {
                Body: 'predict result'
            }
        :rtype: baidubce.bce_response.BceResponse
        Nvariantr*   action	interface
; charset=/invocationshttp_methodfunction_namer   headersparamsr(   
baidubceZDEFAULT_ENCODINGr   CONTENT_TYPEBCE_REQUEST_IDuuiduuid4_send_requestr
   POST
r'   endpoint_namer   Zvariant_namecontent_typer(   r.   r5   Zdefault_encodingr4   r    r    r!   r*   B   s$   
zInfiniteClient.predictc           
      C   r+   )	ai  
        debug

        :param endpoint_name: endpoint name
        :type endpoint_name: string

        :param body: request data
        :type body: binary or dict

        :param variant_name: variant name or None
        :type variant_name: string

        :param content_type: content type,supports application/json,x-image,and x-recordio-protobuf
        :type content_type: string

        :param config: None
        :type config: BceClientConfiguration

        :param interface: interface_name,
            several of predict/predict_proba/predict_log_proba/fit_predict/staged_predict/staged_predict are supported
            depend on frameworks and algorithm used
        :type config: string

        :return: response as following format
            {
                Body: 'debug info'
            }
        :rtype: baidubce.bce_response.BceResponse
        Nr,   debugr-   r.   r/   r0   r1   r6   r>   r    r    r!   rA   v   s$    
zInfiniteClient.debugc                 C   s*   t jtjt jt i}| jtj	d||dS )a*  
        get all endpoint

        :param config: None
        :type config: BceClientConfiguration

        :return: response as following format
            {
                Body: '{"endpointList":["ep1_name","ep2_name"]}'
            }
        :rtype: baidubce.bce_response.BceResponse
        listr2   r3   r4   r(   
r   r8   r	   JSONr9   r:   r;   r<   r
   GET)r'   r(   r4   r    r    r!   get_endpoint_list   s   
z InfiniteClient.get_endpoint_listc                 C   s.   t jtjt jt i}| jtj	|d ||dS )a  
        get endpoint info

        :param endpoint_name: endpoint name
        :type endpoint_name: string

        :param config: None
        :type config: BceClientConfiguration

        :return: response as following format
            {
                Body: '{
                    "endpoint_uuid":"ep1",
                    "variant_configs":[
                        {
                            "variant_uuid":"v1",
                            "variant_name":"v1_name",
                            "...":"..."
                        }
                    ]
                }'
            }
        :rtype: baidubce.bce_response.BceResponse
        z/inforC   rD   )r'   r?   r(   r4   r    r    r!   get_endpoint_info   s   
z InfiniteClient.get_endpoint_infoc                 C   s   t tjt|S r$   )r   Z
append_urir   Z
URL_PREFIXr   convert_to_bytes)r(   r3   r    r    r!   	_get_path   s   zInfiniteClient._get_pathc                 C   s8   |d u r
|  | jS t| j}|| |  |}|S r$   )_convert_configr(   copyZmerge_non_none_values)r'   r(   Z
new_configr    r    r!   _merge_config   s   

zInfiniteClient._merge_configc                 C   sR   |d ur'|j d urt|j |_ |jd ur't|jj|j_t|jj|j_|S r$   )Zendpointr   rI   credentialsZaccess_key_idZsecret_access_keyr&   r    r    r!   rK      s   

zInfiniteClient._convert_configc           	   
   C   s>   |  |}t||}|d u rt}t|tj|g|||||S r$   )rM   r#   rJ   r"   r   send_requestr   sign)	r'   r2   r3   r   r4   r5   r(   Zbody_parserpathr    r    r!   r<      s   


zInfiniteClient._send_requestr$   )Nr)   Nr*   )NNNNNN)__name__
__module____qualname____doc__r%   r*   rA   rG   rH   staticmethodrJ   rM   rK   r<   r    r    r    r!   r#   ;   s*    

4

5
#
	r#   )"rU   rL   loggingwarningsr7   r   r   Zbaidubce.authr   Zbaidubce.bce_base_clientr   Zbaidubce.httpr   r   r   r	   r
   Zbaidubce.utilsr   Zbaidubce.servicesr   http.clientr   Zbaidubce.exceptionr   r   Z!baidubce.bce_client_configurationr   r:   	getLoggerrR   Z_loggerr"   r#   r    r    r    r!   <module>   s0   
