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mZ ddlm	Z	 ddl
mZmZmZ ddlmZ ejd dkr<eefZneefZdZd	Zd
ZG dd dejZdS )z.
This module provides a client class for BCM.
    N)bce_base_clientcompat)bce_v1_signer)handlerbce_http_clienthttp_methods)bls_handler   i  d   Zdescc                   @   s   e Zd ZdZdZdZdZdZd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	S )	BlsClientz
    BLS base sdk client
    s	   /logstore   /v1s   /v2s   /v3s   content-types   application/json;charset=UTF-8s   x-bce-request-idNc                 C   s   t j| | d S N)r   BceBaseClient__init__)selfconfig r   l/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/baidubce/services/bls/bls_client.pyr   2   s   zBlsClient.__init__c                 C   s(   |d u r| j S t| j }|| |S r   )r   copyZmerge_non_none_values)r   r   Z
new_configr   r   r   _merge_config5   s
   
zBlsClient._merge_configc	           	   
   C   s|   |  |}|d u rtj}|d u ri }| j|vr| j|| j< | j|vr)t || j< t	|t
jtj|g||tj | |||S r   )r   r   Z
parse_jsoncontent_type_header_keycontent_type_header_valuerequest_id_header_keyuuiduuid4r   send_requestr   signr   Zparse_errorr   
log_prefix)	r   Zhttp_methodpathversionbodyheadersparamsr   Zbody_parserr   r   r   _send_request=   s   


zBlsClient._send_requestc	                 C   s~   t |}d| }	|||d}
|dur||
d< |du rt|
d< |dur-|tkr)t}||
d< |dur5||
d< | jtj|	|
|dS )a  
        Pull log records from specified log store.

        :param config:
        :param marker:
        :param limit:
        :param project:
        :param log_stream_name:
        :param log_store_name: The name of log store which will be pulled.
        :type log_store_name: string
        :param start_time: Start time of pulling log records.
        :type start_time: string
        :param end_time: End time of pulling log records.
        :type end_time: string
        :return: A list of log records.
        :rtype: list
           /%s/logrecords   startDateTimes   endDateTimes   logStreamNameN   project   limit   markerr"   r   )r   convert_to_bytesDEFAULT_BATCH_RECORD_NUMBERMAX_BATCH_RECORD_NUMBERr#   r   GET)r   log_store_name
start_timeend_timelog_stream_nameprojectlimitmarkerr   r   r"   r   r   r   pull_log_recordsM   s   
zBlsClient.pull_log_recordsc                 C   s|   t |}d| }i }	dd |D }
|dur|nd}dd |D }|dur)||	d< ||
||d}| jtj||	t||d	S )
aP  
        Push log records into the specified log stream in the given log store.

        :param log_store_name: The name of the log store which will receive the log records.
        :type log_store_name: str

        :param log_stream_name: The name of the log stream to write to.
        :type log_stream_name: str

        :param log_records: A list of log records to push.
                            Each record should be an instance of `LogRecordModel`.
                            Example:
                            [
                                LogRecordModel(message="test log", timestamp=1715231012000),
                                LogRecordModel(message="another log", timestamp=1715231044000)
                            ]
        :type log_records: List[LogRecordModel]

        :param project: (Optional) The project name to which the log store belongs.
        :type project: str or None

        :param type: (Optional) Type of logs, defaults to 'TEXT' if not specified.
        :type type: str or None

        :param tags: (Optional) A list of tags associated with the push.
                     Each tag should be an instance of `TagModel`.
                     Example:
                     [
                         TagModel("env", "prod"),
                         TagModel("service", "auth")
                     ]
        :type tags: List[TagModel] or None

        :param config: (Optional) Custom request config.
        :type config: baidubce.BceClientConfiguration or None

        :return: A dictionary containing the result of the push operation.
        :rtype: dict
        r$   c                 S      g | ]}t |qS r   dict).0tagr   r   r   
<listcomp>       z.BlsClient.push_log_records.<locals>.<listcomp>NZTEXTc                 S   r6   r   r7   )r9   recordr   r   r   r;      r<   r2   )ZlogStreamNametagstypeZ
logRecords)r"   r    r   )r   r*   r#   r   POSTjsondumps)r   r.   r1   Zlog_recordsr2   r?   r>   r   r   r"   Z	tags_listZrecords_payloadr    r   r   r   push_log_recordsq   s   
)zBlsClient.push_log_recordsc                 C   s   t |}d| }|||d}|dur||d< |du rt|d< |dur-|tkr)t}||d< |	dur5|	|d< |dur=||d< |durN|dkrJ||d	< nt|d	< | jtj|||
d
S )a  
        query log records from specified log store.

        :param sort: desc or asc
        :type sort: string
        :param query: query statement, eg: match *
        :type query: string
        :param config:
        :param marker:
        :param limit:
        :param project:
        :param log_stream_name:
        :param log_store_name: The name of log store which will be pulled.
        :type log_store_name: string
        :param start_time: Start time of pulling log records.
        :type start_time: string
        :param end_time: End time of pulling log records.
        :type end_time: string
        :return: A list of log records.
        :rtype: list
        r$   r%   Nr&   r'   r(      queryasc   sortr)   )r   r*   r+   r,   DEFAULT_SORTr#   r   r-   )r   r.   r/   r0   r1   r2   querysortr3   r4   r   r   r"   r   r   r   query_log_records   s(   

zBlsClient.query_log_recordsc                 C   s   t |}d| }|||d}|dur||d< |du rt|d< |dur-|tkr)t}||d< |dur5||d< |	durF|	dkrB|	|d< nt|d< |durN||d	< | jtj|| j||
d
S )a  
        Pull log records v3 from specified log store.

        :param query: query statement, eg: match *
        :type query: string
        :param sort: desc or asc
        :type: sort: string
        :param config:
        :param marker:
        :param limit:
        :param project:
        :param log_stream_name:
        :param log_store_name: The name of log store which will be pulled.
        :type log_store_name: string
        :param start_time: Start time of pulling log records.
        :type start_time: string
        :param end_time: End time of pulling log records.
        :type end_time: string
        :return: A list of log records.
        :rtype: list
        s   /%s/logrecord/pullr%   Nr&   r'   r(   rE   rF   rD   )r   r"   r   )	r   r*   r+   r,   rG   r#   r   r-   
version_v3)r   r.   r/   r0   r1   r2   rH   r3   r4   rI   r   r   r"   r   r   r   pull_log_records_v3   s(   

zBlsClient.pull_log_records_v3r   )r   NNNNN)NNNN)NNNNNN)__name__
__module____qualname____doc__r   r   Z
version_v2rK   r   r   r   r   r   r#   r5   rC   rJ   rL   r   r   r   r   r   $   s4    



$
:
1r   )rP   r   rA   sysr   Zbaidubcer   r   Zbaidubce.authr   Zbaidubce.httpr   r   r   Zbaidubce.services.blsr   version_infostrunicodeZ
value_typebytesr,   r+   rG   r   r   r   r   r   r   <module>   s    
