o
    lqi\V                     @   sB  d 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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#Z$G dd deZ%dS )z.
This module provides a client class for BTS.
    N)bce_v1_signer)BceBaseClient)BceClientError)bce_http_client)handler)http_content_types)http_headers)http_methods)bts)INVALID_ARGS_ERROR)create_instance_args_2_dict)CreateInstanceArgs)Row)Cell)	QueryCell)BatchQueryRowArgs)QueryRowArgs)ScanArgs)create_table_args_2_dict)update_table_args_2_dict)_decode)_encodec                   @   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d)ddZ	d)ddZ
d)ddZd)ddZd)ddZd)ddZd)ddZd)ddZd)ddZd)dd Zd)d!d"Zd)d#d$Zd%d& Z			d+d'd(ZdS ),	BtsClientz
    BTS Client
    Nc                 C   s   t | | d S N)r   __init__)selfconfig r   g/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/baidubce/services/bts/bts_client.pyr   3   s   zBtsClient.__init__c                 C   sD   t jd | }|du rt }| jtj||tj|tdt	j
tjidS )ax  
        create instance

        :param instance_name: instance name
        :type instance_name: string
        :param create_instance_args: arguments for create instance
        :type create_instance_args: CreateInstanceArgs
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
           /Ndefaultpathr   bodyheaders)r
   
URL_PREFIXr   _send_requestr	   PUTjsondumpsr   r   CONTENT_TYPEr   JSON)r   instance_nameZcreate_instance_argsr   r#   r   r   r   create_instance7   s   
zBtsClient.create_instancec                 C       t jd | }| jtj||dS )z
        drop instance

        :param instance_name: instance name
        :type instance_name: string
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        r   r#   r   r
   r&   r'   r	   DELETEr   r-   r   r#   r   r   r   drop_instanceL      zBtsClient.drop_instancec                 C   s   d}| j tj||dS )z
        list instances

        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        s   /v1/instancesr0   )r'   r	   GET)r   r   r#   r   r   r   list_instances[   s   
zBtsClient.list_instancesc                 C   r/   )z
        show instance

        :param instance_name: instance name
        :type instance_name: string
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        r   r0   r
   r&   r'   r	   r6   r3   r   r   r   show_instanceh   r5   zBtsClient.show_instancec                 C   >   t jd | d | }| jtj||tj|tdtj	t
jidS )a  
        create table

        :param instance_name: instance name
        :type instance_name: string
        :param table_name: table name
        :type table_name: string
        :param create_table_args: arguments for create table
        :type create_table_args: CreateTableArgs
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        r      /table/r    r"   )r
   r&   r'   r	   r(   r)   r*   r   r   r+   r   r,   )r   r-   
table_nameZcreate_table_argsr   r#   r   r   r   create_tablex   
   
zBtsClient.create_tablec                 C   r:   )a  
        update table

        :param instance_name: instance name
        :type instance_name: string
        :param table_name: table name
        :type table_name: string
        :param update_table_args: arguments for update table
        :type update_table_args: UpdateTableArgs
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        r   r;   r    r"   )r
   r&   r'   r	   r(   r)   r*   r   r   r+   r   r,   )r   r-   r<   Zupdate_table_argsr   r#   r   r   r   update_table   r>   zBtsClient.update_tablec                 C   (   t jd | d | }| jtj||dS )a@  
        drop table

        :param instance_name: instance name
        :type instance_name: string
        :param table_name: table name
        :type table_name: string
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        r   r;   r0   r1   r   r-   r<   r   r#   r   r   r   
drop_table      zBtsClient.drop_tablec                 C   r@   )a@  
        show table

        :param instance_name: instance name
        :type instance_name: string
        :param table_name: table name
        :type table_name: string
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        r   r;   r0   r8   rA   r   r   r   
show_table   rC   zBtsClient.show_tablec                 C   s$   t jd | d }| jtj||dS )z
        list tables

        :param instance_name: instance name
        :type instance_name: string
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        r   s   /tablesr0   r8   r3   r   r   r   list_tables   s   zBtsClient.list_tablesc              
   C   s
  |du s	|j dkrtt}t| |t|j g d}z9|jD ]3}t|tr;|	 }t|d |d< |d 
| q t|trPt|d |d< |d 
| q ttW n tyc } z|d}~ww t|}	tjd | d | d }
| jtj|
||	tjtjid	S )
a  
        put row

        :param instance_name: instance name
        :type instance_name: string
        :param table_name: table name
        :type table_name: string
        :param put_row_args: arguments for put row
        :type put_row_args: Row
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        N rowkeycellsvaluerI   r   r;      /rowr"   )rH   r   r   _loggerdebugr   rI   
isinstancer   to_dictappenddict	Exceptionr)   r*   r
   r&   r'   r	   r(   r   r+   r   r,   )r   r-   r<   Zput_row_argsr   exrow_datacellZ	cell_dictr$   r#   r   r   r   put_row   s8   






zBtsClient.put_rowc                 C   s,  |du rt t}t| |g }|jD ]^}zNt|tr+| }|d dkr*t tnt|trAt	
|}|ddkr@t tnt tt|d |d< |dg D ]
}	t|	d |	d< qS|| W q tyr } z|d}~ww td|i}
tjd | d | d	 }| jtj|||
tjtjid
S )a  
        batch put row

        :param instance_name: instance name
        :type instance_name: string
        :param table_name: table name
        :type table_name: string
        :param batch_put_row_args: arguments for batch put row
        :type batch_put_row_args: BatchPutRowArgs
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        NrH   rF   rI   rJ   rowsr   r;      /rowsr"   )r   r   rL   rM   rW   rN   r   rO   rQ   copydeepcopygetr   rP   rR   r)   r*   r
   r&   r'   r	   r(   r   r+   r   r,   )r   r-   r<   Zbatch_put_row_argsr   rS   	rows_datarowrT   rU   r$   r#   r   r   r   batch_put_row   s@   





zBtsClient.batch_put_rowc           
   
   C   s   |du st |dr|jdkrtt}t| |t|jg d}z/t |drO|jrO|jD ]!}t|t	r>|d 
|  q-t|trK|d 
| q-ttW n ty^ } z|d}~ww t|}tjd | d | d }	| jtj|	||tjtjid	S )
a  
        delete row

        :param instance_name: instance name
        :type instance_name: string
        :param table_name: table name
        :type table_name: string
        :param delete_row_args: arguments for delete row
        :type delete_row_args: QueryRowArgs
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        NrH   rF   rG   rI   r   r;   rK   r"   )hasattrrH   r   r   rL   rM   r   rI   rN   r   rP   rO   rQ   rR   r)   r*   r
   r&   r'   r	   r2   r   r+   r   r,   )
r   r-   r<   Zdelete_row_argsr   rS   Zdelete_row_datarU   r$   r#   r   r   r   
delete_row/  s4   





zBtsClient.delete_rowc              
   C   s   |du s	t |tsttg }z8|jD ]2}t |tr| }nt |tr'|}nttd|v r<|d r<t|d |d< ntt|	| qW n t
yU } z|d}~ww d|i}	t|	}
tjd | d | d }| jtj|||
tjtjidS )a  
        batch delete row

        :param instance_name: instance name
        :type instance_name: string
        :param table_name: table name
        :type table_name: string
        :param batch_delete_row_args: arguments for batch delete row
        :type batch_delete_row_args: BatchQueryRowArgs
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        NrH   rW   r   r;   rX   r"   )rN   r   r   r   rW   r   rO   rQ   r   rP   rR   r)   r*   r
   r&   r'   r	   r2   r   r+   r   r,   )r   r-   r<   Zbatch_delete_row_argsr   r\   r]   rT   rS   Zbatch_delete_row_datar$   r#   r   r   r   batch_delete_rowY  s4   





zBtsClient.batch_delete_rowc              
   C   s  t |tr|d}nt |tr|j}ntt|sttt|g d}t|drR|j	rR|j	D ]!}t |t
rA|d |  q0t |trN|d | q0ttt|}tjd | d | d }	| jtj|	||tjtjtjtjid}
z:|
jdurtt|
jd	 j|
jd	 _tt|
jd	 j	D ]}tt|
jd	 j	| j|
jd	 j	| _qW |
S W |
S  t y } z|d}~ww )
a  
        get row

        :param instance_name: instance name
        :type instance_name: string
        :param table_name: table name
        :type table_name: string
        :param get_row_args: arguments for get row
        :type get_row_args: QueryRowArg
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        rH   rG   rI   r   r;   rK   r"   Nr   )!rN   rQ   r[   r   rH   r   r   r   r_   rI   r   rP   rO   r)   r*   r
   r&   r'   r	   POSTr   r+   r   r,   BTS_METHOD_HEADERr6   resultr   strrangelenrJ   rR   )r   r-   r<   Zget_row_argsr   rH   Zget_row_datarU   r$   r#   responseirS   r   r   r   get_row  sL   






*zBtsClient.get_rowc              
   C   s  i }t |trDg }|jD ]2}t |tr| }nt |tr |}nttd|v r5|d r5t|d |d< ntt|	| q||d< nt |trL|}nttt
|}	tjd | d | d }
| jtj|
||	tjtjtjtjid}zD|jdurtt|jD ]4}tt|j| j|j| _tt|j| jD ]}tt|j| j| j|j| j| _qqW |S W |S  ty } z|d}~ww )a  
        batch get row

        :param instance_name: instance name
        :type instance_name: string
        :param table_name: table name
        :type table_name: string
        :param batch_get_row_args: arguments for batch get row
        :type batch_get_row_args: BatchQueryRowArgs
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        rH   rW   r   r;   rX   r"   N) rN   r   rW   r   rO   rQ   r   r   r   rP   r)   r*   r
   r&   r'   r	   rb   r   r+   r   r,   rc   r6   rd   rf   rg   r   re   rH   rI   rJ   rR   )r   r-   r<   Zbatch_get_row_argsr   Zbatch_get_row_datar\   Zrow_argrT   r$   r#   rh   ri   jrS   r   r   r   batch_get_row  sP   








*zBtsClient.batch_get_rowc              
   C   sT  t |tr
| }nt |tr|}nttd|v r&|d r&t|d |d< d|v r6|d r6t|d |d< t|}t	j
d | d | d }| jtj|||tjtjid}zD|jdurtt|jD ]4}	tt|j|	 j|j|	 _tt|j|	 jD ]}
tt|j|	 j|
 j|j|	 j|
 _q~qdW |S W |S  ty } z|d}~ww )a  
        scan

        :param instance_name: instance name
        :type instance_name: string
        :param table_name: table name
        :type table_name: string
        :param scan_args: arguments for scan
        :type scan_args: ScanArgs
        :param config: None
        :type config: BceClientConfiguration

        :return:
        :rtype baidubce.bce_response.BceResponse
        ZstartRowkeyZ
stopRowkeyr   r;   rX   r"   N)rN   r   rO   rQ   r   r   r   r)   r*   r
   r&   r'   r	   r6   r   r+   r   r,   rd   rf   rg   r   re   rH   rI   rJ   rR   )r   r-   r<   Z	scan_argsr   Zscan_args_dictr$   r#   rh   ri   rk   rS   r   r   r   scan  s<   





*zBtsClient.scanc                 C   s(   |d u r| j S t| j }|| |S r   )r   rY   Zmerge_non_none_values)r   r   Z
new_configr   r   r   _merge_config#  s
   
zBtsClient._merge_configc              
   C   sV   |  |}|d u rtj}|jd ur|pi }|j|tj< t|tj	tj
|g|||||S r   )rn   r   Z
parse_jsonZsecurity_tokenr   ZSTS_SECURITY_TOKENr   send_requestr   signZparse_error)r   Zhttp_methodr#   r$   r%   paramsr   Zbody_parserr   r   r   r'   ,  s   


zBtsClient._send_requestr   )NN)NNNNN)__name__
__module____qualname____doc__r   r.   r4   r7   r9   r=   r?   rB   rD   rE   rV   r^   r`   ra   rj   rl   rm   rn   r'   r   r   r   r   r   /   s0    











,
/
*
+
7
;-r   )&ru   rY   r)   loggingZbaidubce.authr   Zbaidubce.bce_base_clientr   Zbaidubce.exceptionr   Zbaidubce.httpr   r   r   r   r	   Zbaidubce.servicesr
   Zbaidubce.services.btsr   Zbaidubce.services.bts.modelr   r   r   r   r   r   r   r   r   r   Zbaidubce.services.bts.utilr   r   	getLoggerrr   rL   r   r   r   r   r   <module>   s8   
