o
    „Ýhw  ã                   @   s^   d dl Z d dlmZ d dlmZ d dlmZ G dd„ dƒZG dd„ deƒZG d	d
„ d
eƒZdS )é    N)ÚCountryCode)Úconvert_response)Úprefix_country_codec                   @   sX   e Zd ZdZdZdZdZdZdd„ Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )Ú
BaseClientz.Base class for both OTPClient & AsyncOTPClientzhttps://api.msg91.comz/api/sendotp.phpz/api/retryotp.phpz/api/verifyRequestOTP.phpc                 C   s
   || _ d S ©N)Úauth_key)Úselfr   © r	   úX/var/www/html/magazine_api/magazine_env/lib/python3.10/site-packages/msg91_otp/client.pyÚ__init__   s   
zBaseClient.__init__c                 C   ó   | j | j S r   )Úbase_urlÚotp_endpoint©r   r	   r	   r
   Úget_otp_url   ó   zBaseClient.get_otp_urlc                 C   r   r   )r   Úotp_retry_endpointr   r	   r	   r
   Úget_resend_otp_url   r   zBaseClient.get_resend_otp_urlc                 C   r   r   )r   Úverify_otp_endpointr   r	   r	   r
   Úget_verify_otp_url   r   zBaseClient.get_verify_otp_urlc                 K   sÊ   t tjt|ƒƒ}|| jdœ}d|v r| d¡|d< d|v r$| d¡|d< d|v rD| d¡|d< d|v rCd|d v rC|d  dd¡|d< nd|v rXd|d vrX|d  ¡ d |d< d|v rc| d¡|d< |S )	N©ÚmobileÚauthkeyÚmessageZsenderÚotpz##OTP##Ú z ##OTP##Z
otp_length)r   r   ÚINDIAÚstrr   ÚgetÚreplaceÚstrip©r   ÚreceiverÚkwargsÚpayloadr	   r	   r
   Úget_otp_payload   s$   þ€zBaseClient.get_otp_payloadc                 K   s    t tjt|ƒƒ}|| jdœ}tS )Nr   )r   r   r   r   r   Zpaylaodr!   r	   r	   r
   Úget_resend_otp_payload6   s
   þz!BaseClient.get_resend_otp_payloadc                 C   s.   t tjt|ƒƒ}| j||dœ}ddi}||fS )N)r   r   r   zContent-typez!application/x-www-form-urlencoded)r   r   r   r   r   )r   r   Ú	otp_valuer$   Úheadersr	   r	   r
   Úget_verify_payload_headers=   s   ýz%BaseClient.get_verify_payload_headersN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r%   r&   r)   r	   r	   r	   r
   r      s    r   c                   @   ó(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú	OTPClientzJSynchronous OTP client
    Use this class for blocking request to API
    c                 K   s8   |   ¡ }| j|fi |¤Ž}tj||d}t||d}|S )a  Request the Service to send OTP message to given number
        Args:
            receiver(str): 10 digit mobile number with country code of reciever

            message(str, optional): text message to send along with OTP
            sender(str, optional): the name to appear in SMS as sender
            otp (int, optional): the opt value to send, if not service will generate
            otp_length (int, optional): the length of otp. default 4, max 9
        Returns:
            a response object with status and status message
        ©Úparams)Ú
identifier)r   r%   ÚhttpxÚpostr   )r   r"   r#   Úotp_urlr$   Úservice_responseÚ	_responser	   r	   r
   Úsend_otpK   s
   zOTPClient.send_otpc                 K   s4   |   ¡ }| j|fi |¤Ž}tj||d}t|ƒ}|S )zÇResend OTP request
        Args:
            receiver(str): 10 digit mobile number with country code of receiver
        Returns:
            a response object with status and status message
        r0   )r   r&   r3   r4   r   )r   r"   r#   Ú	retry_urlr$   r6   r7   r	   r	   r
   Ú
resend_otp]   s
   zOTPClient.resend_otpc                 C   s4   |   ¡ }|  ||¡\}}tj|||d}t|ƒ}|S )a  Request to verify OTP with given mobile number
        Args:
            mobile(str): mobile number to verify otp
            otp_value(int): the otp value to verify against
        Returns:
             a response object with status and status message
        ©r1   r(   )r   r)   r3   r4   r   )r   r   r'   Ú
verify_urlr$   r(   r6   r7   r	   r	   r
   Ú
verify_otpj   s
   zOTPClient.verify_otpN©r*   r+   r,   r-   r8   r:   r=   r	   r	   r	   r
   r/   G   s
    r/   c                   @   r.   )	ÚAsyncOTPClientzOAsynchronous OTP client
    Use this class for non-blocking request to API
    c              	   Ë   óx   |   ¡ }| j|fi |¤Ž}t ¡ 4 I d H š}|j||dI d H }W d   ƒI d H  n1 I d H s1w   Y  t|ƒ}|S ©Nr0   )r   r%   r3   ÚAsyncClientr4   r   )r   r"   r#   r5   r$   Úclientr6   r7   r	   r	   r
   r8   }   ó   €(ÿzAsyncOTPClient.send_otpc              	   Ë   r@   rA   )r   Zget_resend_payloadr3   rB   r4   r   )r   r"   r#   r9   r$   rC   r6   r7   r	   r	   r
   r:   …   rD   zAsyncOTPClient.resend_otpc           	   	   Ã   sx   |   ¡ }|  ||¡\}}t ¡ 4 I d H š}|j|||dI d H }W d   ƒI d H  n1 I d H s1w   Y  t|ƒ}|S )Nr;   )r   r)   r3   rB   r4   r   )	r   r   r'   r<   r$   r(   rC   r6   r7   r	   r	   r
   r=      s   €(ÿzAsyncOTPClient.verify_otpNr>   r	   r	   r	   r
   r?   y   s
    r?   )	r3   Zmsg91_otp.country_coder   Zmsg91_otp.utilsr   r   r   r/   r?   r	   r	   r	   r
   Ú<module>   s    ?2