o
    lqi{                     @   s   d Z ddlZddlZddlZddlZddlZddlm  m	  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dd Z G dd deZ!dS )z.
This module provides a client class for SMS.
    N)utils)bce_v1_signer)BceBaseClient)BceClientConfiguration)BceClientError)BceServerError)bce_http_client)http_headers)http_methods)sms)requiredc                 C   s   | j d tjjd krtdd }|  }|rpt|}d|v rQd|v rQd|v rQ|d }|dkr>t|d |d |d d}n?|j	
tj|tjdj	 |   d	S | j d tjjd kro|j	
tj|tjdj	 |   d	S n| j d tjjd kr}d	S |d u rt| j|jjd
}| j |_|)Nd   z#Can not handle 1xx http status codemessagecodeZ	requestIdZ1000)r   
request_id)object_hookT)r   )statushttpclientCONTINUEr   readjsonloadsr   __dict__updater   Zdict_to_python_objectcloseOKreasonmetadataZbce_request_idstatus_code)http_responseresponseZbsebodydZr_code r$   g/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/baidubce/services/sms/sms_client.py_parse_result,   s>   
r&   c                   @   s  e Zd ZdZdAddZeeddd Zeeeeee	d		dBd	d
Z
eeeed		dCddZeeeeed		dBddZeeddAddZeeddAddZeeeeeeddAddZeeeeeeddDddZeeddAddZeeddAd d!ZdAd"d#Zeeeeeed$	dAd%d&ZedDd'd(ZedDd)d*Ze	,	dEd-d.Zd/d0 Z		1dFd2d3Zeeed4dGd5d6Z		dHd7d8Zeed9dAd:d;Zeeed<	>	dId?d@ZdS )J	SmsClientz
    Sms sdk client
    Nc                 C   s"   |d ur	|  | t| | d S N)_check_config_typer   __init__selfconfigr$   r$   r%   r*   S   s   
zSmsClient.__init__)r-   c                 C   s   dS )NTr$   r+   r$   r$   r%   r)   X   s   zSmsClient._check_config_type)signature_idtemplate_idtypemobilecontent_var_dictc	           
      C   s0   |||||||d}	| j tjdt|	|ddS )u  
        Send message

        :param signature_id: The unique code identifying message signature, can be obtained from cloud.baidu.com
        :type  signature_id: string or unicode

        :param template_id: The unique code identifying message content template, can be obtained from cloud.baidu.com
        :type  template_id: string or unicode

        :param mobile: The target mobile, use "," as separators if you have multiple targets.
        :type  mobile: string or unicode

        :param content_var_dict: A map like "{"template param name": "template param content"}
        :type  content_var_dict: dict

        :param config: None
        :type  config: BceClientConfiguration

        :param custom: The user-defined param
        :type  custom: string or unicode

        :param user_ext_id: The user-defined channel code
        :type  user_ext_id: string or unicode

        :param merchant_url_id: The id of callback url specified by user
        :type  merchant_url_id: string or unicode

        :return: Object
            {
              "request_id": "5e6dacd5-8815-4183-8255-4ff079bf24e6",
              "code": "1000",
              "message": "成功",
              "data": [
                {
                  "code": "1000",
                  "message": "成功",
                  "mobile": "13800138000",
                  "message_id": "e325ea68-02c1-47ad-8844-c7b93cafaeba_13800138000"
                }
              ]
            }
        )signatureIdtemplater1   Z
contentVarcustomZ	userExtIdZmerchantUrlIdZsendSms   )r"   r-   api_version_send_requestr
   POSTr   dumps)
r,   r.   r/   r1   r2   r-   r5   Zuser_ext_idZmerchant_url_iddatar$   r$   r%   send_message\   s   .
zSmsClient.send_message)contentcontent_typecountry_typeDOMESTICc           	   
   C   s\   |||d}|r||d< |r||d< |r||d< t | jtjddt it||ddS )	a$  
        Create signature
        :param content: Signature content, only Chinese and English characters and numbers are allowed.
        :type  content: string or unicode

        :param content_type: Signature type, one of "Enterprise, MobileApp, Web, WeChatPublic, Brand, Else"
        :type  content_type: string or unicode

        :param description: Description of the signature
        :type  description: string or unicode

        :param country_type: The country or region in which the template can be used. Default value is "DOMESTIC".
                             The value of countryType could be DOMESTIC or INTERNATIONAL or GLOBAL.
                             DOMESTIC: the template can only be used in Mainland China.
                             INTERNATIONAL: the template can only be used out of Mainland China.
                             GLOBAL: the template can only be used all over the world.
        :type  country_type: string or unicode

        :param signature_file_base_64: The base64 encoding string of the signature certificate picture
        :type  signature_file_base_64: string or unicode

        :param signature_file_format: The format of the signature certificate picture, only one of JPG, PNG,
                                      JPEG allowed.
        :type  signature_file_format: string or unicode

        :param config: None
        :type  config: BceClientConfiguration
        :return: Object
            {
              "signature_id": "sms-sign-WWejWQ54455",
              "status": "SUBMITTED"
            }

        r>   ZcontentTypecountryTypedescriptionsignatureFileBase64signatureFileFormatsignatureApplyclientToken   paramsr"   r-   r7   )	modelZCreateSignatureResponser9   r
   r:   uuiduuid4r   r;   )	r,   r>   r?   rD   r@   signature_file_base_64signature_file_formatr-   r<   r$   r$   r%   create_signature   s   &
zSmsClient.create_signature)r>   r?   r@   r.   c	           
      C   sN   |||d}	|r||	d< |r||	d< |r||	d< | j tjd|t|	|ddS )a  
        Update signature
        :param content: Signature content
        :type  content: string or unicode

        :param content_type: Signature type, one of "Enterprise, MobileApp, Web, WeChatPublic, Brand, Else"
        :type  content_type: string or unicode

        :param signature_id: The unique code identifying the signature
        :type  signature_id: string or unicode

        :param description: Description of the signature
        :type  description: string or unicode

        :param country_type: The country or region in which the template can be used.
                             The value of countryType could be DOMESTIC or INTERNATIONAL or GLOBAL.
                             DOMESTIC: the template can only be used in Mainland China.
                             INTERNATIONAL: the template can only be used out of Mainland China.
                             GLOBAL: the template can only be used all over the world.
        :type  country_type: string or unicode

        :param signature_file_base_64: The base64 encoding string of the signature certificate picture
        :type  signature_file_base_64: string or unicode

        :param signature_file_format: The format of the signature certificate picture, only one of JPG, PNG,
                                      JPEG allowed.
        :type  signature_file_format: string or unicode

        :param config: None
        :type  config: BceClientConfiguration
        :return: Object
            {
              "content": "Baidu",
              "content_type": "Enterprise",
              "description": "test sdk",
              "country_type": "DOMESTIC",
              "signature_file_base64": "test-string-base64encoded",
              "signature_file_format": "png"
            }
        rB   rD   rE   rF   rG   rI   )keyr"   r-   r7   r9   r
   PUTr   r;   )
r,   r>   r?   r@   r.   rD   rO   rP   r-   r<   r$   r$   r%   update_signature   s   ,zSmsClient.update_signature)r.   c              	   C      t | jtjd||ddS )an  
        Get signature detail
        :param signature_id: The unique code identifying the signature
        :type  signature_id: string or unicode

        :param config: None
        :type  config: BceClientConfiguration

        :return: Object
            {
              "signature_id": "sms-sign-WWejWQ54455",
              "user_id": "bbede3f8c42e4113b6971fd09a57f494",
              "content": "Baidu",
              "content_type": "MobileApp",
              "description": "test sdk",
              "review": "",
              "status": "SUBMITTED",
              "country_type": "GLOBAL",
            }
        rG   rI   rR   r-   r7   )rL   ZGetSignatureResponser9   r
   GETr,   r.   r-   r$   r$   r%   get_signature_detail  s   zSmsClient.get_signature_detailc                 C      | j tjd||ddS )z
        Delete signature
        :param signature_id: The unique code identifying the signature
        :type  signature_id: string or unicode

        :param config: None
        :type  config: BceClientConfiguration

        :return:
        rG   rI   rW   r9   r
   DELETErY   r$   r$   r%   delete_signature  s   zSmsClient.delete_signature)namer>   sms_typer@   rD   c              
   C   s<   |||||d}t | jtjddt it||ddS )a  
        Create template with specific name and content

        :param name: Template name
        :type  name: string or unicode

        :param content: Template content like 'this is ${APP}, your code is ${VID}'
        :type  content: string or unicode

        :param sms_type: Business type of the template content, can be obtained from cloud.baidu.com
        :type  sms_type: string or unicode

        :param country_type: The country or region in which the template can be used.
                             The value of countryType could be DOMESTIC or INTERNATIONAL or GLOBAL.
                             DOMESTIC: the template can only be used in Mainland China.
                             INTERNATIONAL: the template can only be used out of Mainland China.
                             GLOBAL: the template can only be used all over the world.
        :type  country_type: string or unicode

        :param description: Description of the template
        :type  description: string or unicode

        :param config: None
        :type  config: BceClientConfiguration

        :return: Object
            {
                "template_id": "sms-tmpl-wHoJXL09355",
                "status": "SUBMITTED",
            }

        :rtype: baidubce.bce_response.BceResponse
        )r_   r>   smsTyperC   rD   r4   rH   rI   rJ   )	rL   ZCreateTemplateResponser9   r
   r:   rM   rN   r   r;   )r,   r_   r>   r`   r@   rD   r-   r<   r$   r$   r%   create_template*  s   #
zSmsClient.create_template)r/   r_   r>   r`   r@   c           	      C   s8   ||||d}|r||d< | j tjd|t||ddS )a  
        Update template when audition failed.
        :param template_id: The unique code identifying the template
        :type  template_id: string or unicode

        :param name: the name of template
        :type  name: string or unicode

        :param content: the content of template,such as 'this is ${APP}, your code is ${VID}'
        :type  content: string or unicode

        :param sms_type: The business type of the template content, can be obtained from cloud.baidu.com
        :type  sms_type: string or unicode

        :param country_type: The country or region in which the template can be used.
                             The value of countryType could be DOMESTIC or INTERNATIONAL or GLOBAL.
                             DOMESTIC: the template can only be used in Mainland China.
                             INTERNATIONAL: the template can only be used out of Mainland China.
                             GLOBAL: the template can only be used all over the world.
        :type  country_type: string or unicode

        :param description: Description of the template
        :type  description: string or unicode

        :param config: None
        :type  config: BceClientConfiguration

        :return:
        )r_   r>   ra   rC   rD   r4   rI   )http_methodfunction_namerR   r"   r-   r7   rS   )	r,   r/   r_   r>   r`   r@   rD   r-   r<   r$   r$   r%   update_templateW  s   zSmsClient.update_template)r/   c              	   C   rV   )u  
        Get template detail
        :param template_id: The ID of message template
        :type  template_id: string or unicode

        :param config: None
        :type  config: BceClientConfiguration

        :return:
            {
              "template_id": "sms-tmpl-wHoJXL09355",
              "user_id": "bbede3f8c42e4113b6971fd09a57f494",
              "name": "TemplateNameTest",
              "content": "${code}",
              "sms_type": "CommonNotice",
              "description": "test modify",
              "review": "通过",
              "status": "APPROVED",
              "country_type": "INTERNATIONAL",
            }
        r4   rI   )rd   rR   r-   r7   )rL   ZGetTemplateResponser9   r
   rX   r,   r/   r-   r$   r$   r%   get_template_detail  s   zSmsClient.get_template_detailc                 C   r[   )z
        Delete template
        :param template_id: The ID of message template
        :type  template_id: string or unicode

        :param config: None
        :type  config: BceClientConfiguration

        :return:
        r4   rI   )rc   rd   rR   r-   r7   r\   rf   r$   r$   r%   delete_template  s   zSmsClient.delete_templatec              	   C   s    t | jtjdddi|ddS )a  
        Query quota and rate-limit detail
        :param config: None
        :type  config: BceClientConfiguration

        :return:
            {
              "quota_oer_day": 100,
              "quota_per_month": 1000,
              "quota_remain_today": 100,
              "quota_remain_this_month": 1000,
              "apply_check_status": "PASS",
              "check_reply": "",
              "apply_quota_per_day": 10,
              "apply_quota_per_month": 10,
              "rate_limit_per_mobile_per_sign_by_minute": 5,
              "rate_limit_per_mobile_per_sign_by_hour": 10,
              "rate_limit_per_mobile_per_sign_by_day": 50,
              "rate_limit_white_list": true
            }
        quotaZ	userQuery rI   rd   rK   r-   r7   )rL   ZQueryQuotaResponser9   r
   rX   r+   r$   r$   r%   query_quota_rate  s   
zSmsClient.query_quota_rate)quota_per_dayquota_per_month(rate_limit_per_mobile_per_sign_by_minute&rate_limit_per_mobile_per_sign_by_hour%rate_limit_per_mobile_per_sign_by_dayc                 C   s,   |||||d}| j tjdt||ddS )aj  

        :param quota_per_day: Upper bound of the response-success request counts in one natural day.
        :type  quota_per_day: int

        :param quota_per_month: Upper bound of the response-success request counts in one natural month.
        :type  quota_per_month: int

        :param rate_limit_per_mobile_per_sign_by_minute: The limit with same mobile and signature in one minute
        :type  rate_limit_per_mobile_per_sign_by_minute: int

        :param rate_limit_per_mobile_per_sign_by_hour: Hourly limit with same mobile and signature
        :type  rate_limit_per_mobile_per_sign_by_hour: int

        :param rate_limit_per_mobile_per_sign_by_day: Daily rate limit with same mobile and signature
        :type  rate_limit_per_mobile_per_sign_by_day: int

        :param config: None
        :type  config: BceClientConfiguration

        :return:
        )ZquotaPerDayZquotaPerMonthZ!rateLimitPerMobilePerSignByMinuteZrateLimitPerMobilePerSignByHourZrateLimitPerMobilePerSignByDayri   rI   rd   r"   r-   r7   rS   )r,   rm   rn   ro   rp   rq   r-   r<   r$   r$   r%   update_quota_rate  s   zSmsClient.update_quota_ratec                 C      t tj||S r(   )r   
append_urir   ZURL_PREFIX_V3r-   rd   rR   r$   r$   r%   _get_path_v3     zSmsClient._get_path_v3c                 C   rt   r(   )r   ru   r   ZURL_PREFIX_V3_2rv   r$   r$   r%   _get_path_v3_2  rx   zSmsClient._get_path_v3_2r     c              
   C   s   g d}|d u st |dkr/g }|D ]}	|	  }
|
tjs$|
|v r)||
 q|  n|D ]}	|	  }
|
tjrD||
 q1|  t	| |||||||S )N)s   hosts   content-md5s   content-lengths   content-typer   )
lenstriplower
startswithr	   Z
BCE_PREFIXappendsortr   sign)credentialsrc   pathheadersrK   	timestampZexpiration_in_secondsZheaders_to_signZheaders_to_sign_listkZk_lowerr$   r$   r%   _bce_sms_sign  s0   


zSmsClient._bce_sms_signc                 C   s2   |d u r| j S | | t| j }|| |S r(   )r-   r)   copyZmerge_non_none_values)r,   r-   Z
new_configr$   r$   r%   _merge_config  s   

zSmsClient._merge_configr6   c
              
   C   s^   |  |}tjtjd|	 |||}
|d u rt}|d u r!ddd}t|tj|g||
|||S )N)r6   rI   s   */*s   application/json;charset=utf-8)s   Accepts   Content-Type)r   r'   rw   ry   r&   r   send_requestr   )r,   rc   rd   rR   r"   r   rK   r-   Zbody_parserr7   r   r$   r$   r%   r9     s   

zSmsClient._send_request)r0   phonec                 C   s@   |||d}|r||d< |r||d< | j tjdt||ddS )a  

        :param type: The value of type could be MerchantBlack or SignatureBlack
        :type type: str

        :param country_type: The value of countryType could be DOMESTIC or INTERNATIONAL
        :type country_type: str

        :param sms_type: Mobile of black, Support multiple mobile phone numbers, up to 200 maximum, separated by comma.
        :type sms_type: str

        :param signature_id_str: When the value of "type" is "SignatureBlack", this field is required.
        :type signature_id_str: str

        :param phone: When the value of "type" is "SignatureBlack", this field is required.
        :type phone: str

        :param config: None
        :type  config: BceClientConfiguration

        )r0   r   rC   ra   signatureIdStr	blacklistrI   rr   r8   )r,   r0   r   r@   r`   signature_id_strr-   r<   r$   r$   r%   create_mobile_black'  s   zSmsClient.create_mobile_blackc
              	   C   s   i }
|r||
d< |r||
d< |r||
d< |r||
d< |r ||
d< |r&||
d< |r,||
d< |r2||
d< t | jtjd	|
|	d
dS )a>  
        Get mobile black
        :param phone: Support multiple mobile phone numbers, up to 200 maximum, separated by comma.
        :type phone: str

        :param country_type: The value of countryType could be DOMESTIC or INTERNATIONAL
        :type  country_type: str

        :param sms_type: smsType
        :type  sms_type: str

        :param signature_id_str: signatureIdStr
        :type  signature_id_str: str

        :param start_time: format is yyyy-MM-dd
        :type  start_time: str

        :param end_time: format is yyyy-MM-dd
        :type  end_time: str

        :param page_no: The current page number
        :type  page_no: int

        :param page_size: The current page size, range from 1 to 99999
        :type  page_size: int

        :param config: None
        :type  config: BceClientConfiguration

        :return: Object
            {
                "blacklists": [
                    {
                        "phone": "17611111111",
                        "type": "SignatureBlack",
                        "smsType": "CommonNotice",
                        "signatureIdStr": "1234",
                        "updateDate": "2023-07-14 14:23:41"
                    }
                ],
                "totalCount": 1,
                "pageNo": 1,
                "pageSize": 100
            }
        r   rC   ra   r   	startTimeendTimeZpageNoZpageSizer   rI   rk   )rL   ZGetMobileBlackResponser9   r
   rX   )r,   r   r@   r`   r   
start_timeend_timeZpage_noZ	page_sizer-   
req_paramsr$   r$   r%   get_mobile_blackL  s(   0zSmsClient.get_mobile_black)phonesc                 C   s   d|i}| j tjd||ddS )a  
        Delete mobile_black
        :param phones: Support multiple mobile phone numbers, up to 200 maximum, separated by comma.
        :type  phones: string

        :param config: None
        :type  config: BceClientConfiguration

        :return:
        r   zblacklist/deleterI   rc   rd   rK   r-   r7   r\   )r,   r   r-   r   r$   r$   r%   delete_mobile_black  s
   zSmsClient.delete_mobile_black)r   r   alldomesticc           	   	   C   sV   |d |d |dd}|r||d< |r||d< |r||d< t | jtjd||d	d
S )u  
        Get messages statistics list
        :param start_time: The start of time condition, format: yyyy-MM-dd
        :type  start_time: str

        :param end_time: The end of time condition, format: yyyy-MM-dd
        :type  end_time: str

        :param sms_type: Queried message type, "all" as default
        :type  sms_type: str

        :param signature_id: Queried signature id
        :type  signature_id: str

        :param template_code: Queried template code, for instance: "sms-tmpl-xxxxxxxx"
        :type  template_code: str

        :param country_type: Queried country type, available values: "domestic", "international"
        :type  country_type: str

        :param config: None
        :type  config: BceClientConfiguration

        :return: Object:
            {
                "statisticsResults": [
                    {
                        "datetime": "合计",
                        "countryAlpha2Code": "",
                        "submitLongCount": "10",
                        "submitCount": "100",
                        "deliverSuccessCount": "99",
                        "deliverSuccessLongCount": "10",
                        "deliverFailureCount": "1",
                        "unknownCount": "0",
                        "deliverSuccessProportion": "0.99",
                        "deliverFailureProportion": "0.01",
                        "notExistCount": "0",
                        "signatureOrTemplateCount": "0",
                        "abnormalCount": "0",
                        "overclockingCount": "0",
                        "otherErrorCount": "0",
                        "blacklistCount": "0",
                        "routeErrorCount": "0",
                        "issueFailureCount": "0",
                        "parameterErrorCount": "0",
                        "illegalWordCount": "0",
                        "anomalyCount": "1",
                        "unknownErrorCount": "0",
                        "receiptProportion": "1.0",
                        "unknownProportion": "0",
                        "responseSuccessCount": "100",
                        "responseTimeoutCount": "0",
                        "responseSuccessProportion": "1.0"
                    },
                    {
                        "datetime": "2023-10-30",
                        "countryAlpha2Code": "",
                        "submitLongCount": "10",
                        "submitCount": "100",
                        "deliverSuccessCount": "99",
                        "deliverSuccessLongCount": "10",
                        "deliverFailureCount": "1",
                        "unknownCount": "0",
                        "deliverSuccessProportion": "0.99",
                        "deliverFailureProportion": "0.01",
                        "notExistCount": "0",
                        "signatureOrTemplateCount": "0",
                        "abnormalCount": "0",
                        "overclockingCount": "0",
                        "otherErrorCount": "0",
                        "blacklistCount": "0",
                        "routeErrorCount": "0",
                        "issueFailureCount": "0",
                        "parameterErrorCount": "0",
                        "illegalWordCount": "0",
                        "anomalyCount": "1",
                        "unknownErrorCount": "0",
                        "receiptProportion": "1.0",
                        "unknownProportion": "0",
                        "responseSuccessCount": "100",
                        "responseTimeoutCount": "0",
                        "responseSuccessProportion": "1.0"
                    },
                ]
            }
        z	 00:00:00z	 23:59:59day)r   r   ra   	dimensionrC   r3   ZtemplateCodesummaryrI   r   )rL   ZListStatisticsResponser9   r
   rX   )	r,   r   r   r`   r@   r.   Ztemplate_coder-   r   r$   r$   r%   list_statistics  s   \zSmsClient.list_statisticsr(   )NNNN)NrA   NNN)NN)r   rz   N)NNNNNNNr6   )NNN)	NNNNNNNNN)r   r   NNN) __name__
__module____qualname____doc__r*   r   r   r)   strdictr=   rQ   rU   rZ   r^   rb   re   rg   rh   rl   intrs   staticmethodrw   ry   r   r   r9   r   r   r   r   r$   r$   r$   r%   r'   N   st    

839,'
" 	

$
L
r'   )"r   r   r   loggingrM   http.clientr   Zbaidubce.services.sms.modelZservicesr   rL   Zbaidubcer   Zbaidubce.authr   Zbaidubce.bce_base_clientr   Z!baidubce.bce_client_configurationr   Zbaidubce.exceptionr   r   Zbaidubce.httpr   r	   r
   Zbaidubce.servicesZbaidubce.utilsr   	getLoggerr   Z_loggerr&   r'   r$   r$   r$   r%   <module>   s*   
"