U
    Ê¼µbw  ã                   @   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	   ú4/tmp/pip-unpacked-wheel-c1x4am86/msg91_otp/client.pyÚ__init__   s    zBaseClient.__init__c                 C   s   | j | j S r   )Úbase_urlÚotp_endpoint©r   r	   r	   r
   Úget_otp_url   s    zBaseClient.get_otp_urlc                 C   s   | j | j S r   )r   Úotp_retry_endpointr   r	   r	   r
   Úget_resend_otp_url   s    zBaseClient.get_resend_otp_urlc                 C   s   | j | j S r   )r   Úverify_otp_endpointr   r	   r	   r
   Úget_verify_otp_url   s    zBaseClient.get_verify_otp_urlc                 K   sÊ   t tjt|ƒƒ}|| jdœ}d|kr2| d¡|d< d|krH| d¡|d< d|krˆ| d¡|d< d|kr°d|d kr°|d  dd¡|d< n(d|kr°d|d kr°|d  ¡ d |d< d|krÆ| 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                   @   s(   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   s4   |   ¡ }| j|f|Ž}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   s0   |   ¡ }| j|f|Ž}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
        r-   )r   r$   r0   r1   r   )r   r    r!   Ú	retry_urlr"   r3   r4   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
        ©r.   r&   )r   r'   r0   r1   r   )r   r   r%   Ú
verify_urlr"   r&   r3   r4   r	   r	   r
   Ú
verify_otpj   s
    zOTPClient.verify_otpN©r(   r)   r*   r+   r5   r7   r:   r	   r	   r	   r
   r,   G   s   r,   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚAsyncOTPClientzOAsynchronous OTP client
    Use this class for non-blocking request to API
    c              
   Ë   sX   |   ¡ }| j|f|Ž}t ¡ 4 I d H š}|j||d�I d H }W 5 Q I d H R X t|ƒ}|S ©Nr-   )r   r#   r0   ÚAsyncClientr1   r   )r   r    r!   r2   r"   Úclientr3   r4   r	   r	   r
   r5   }   s    $zAsyncOTPClient.send_otpc              
   Ë   sX   |   ¡ }| j|f|Ž}t ¡ 4 I d H š}|j||d�I d H }W 5 Q I d H R X t|ƒ}|S r=   )r   Zget_resend_payloadr0   r>   r1   r   )r   r    r!   r6   r"   r?   r3   r4   r	   r	   r
   r7   …   s    $zAsyncOTPClient.resend_otpc           	   
   Ã   s\   |   ¡ }|  ||¡\}}t ¡ 4 I d H š}|j|||d�I d H }W 5 Q I d H R X t|ƒ}|S )Nr8   )r   r'   r0   r>   r1   r   )	r   r   r%   r9   r"   r&   r?   r3   r4   r	   r	   r
   r:   �   s    &zAsyncOTPClient.verify_otpNr;   r	   r	   r	   r
   r<   y   s   r<   )	r0   Zmsg91_otp.country_coder   Zmsg91_otp.utilsr   r   r   r,   r<   r	   r	   r	   r
   Ú<module>   s   ?2