U
    h                     @   sh   d Z ddlmZmZmZ ddlZddlmZmZ ddl	m
Z
 ddlmZ ddlmZ G dd	 d	eZdS )
z%Util that sends messages via Infobip.    )DictListOptionalN)	BaseModelroot_validatorget_from_dict_or_env)HTTPAdapter)Retryc                   @   s   e Zd ZU dZdZee ed< dZee ed< G dd dZ	e
dd	eed
ddZejdddZeee eedddZeeeeedddZdeeeeeedddZdS )InfobipAPIWrapperz&Wrapper for Infobip API for messaging.Ninfobip_api_keyzhttps://api.infobip.cominfobip_base_urlc                   @   s   e Zd ZdZdS )zInfobipAPIWrapper.ConfigZforbidN)__name__
__module____qualname__extra r   r   I/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/infobip.pyConfig   s   r   T)pre)valuesreturnc                 C   s$   t |dd|d< t |dd|d< |S )z,Validate that api key exists in environment.r   ZINFOBIP_API_KEYr   ZINFOBIP_BASE_URLr   )clsr   r   r   r   validate_environment   s        z&InfobipAPIWrapper.validate_environment)r   c                 C   sT   t dddddddgd}t|d	}t }|d
| |jd| j dd |S )z0Get a requests session with the correct headers.      i  i  i  i  i  )totalbackoff_factorstatus_forcelist)max_retrieszhttps://zApp zinfobip-langchain-community)Authorizationz
User-Agent)r
   r	   requestsSessionmountheadersupdater   )selfZretry_strategyadaptersessionr   r   r   _get_requests_session    s    

z'InfobipAPIWrapper._get_requests_session)senderdestination_phone_numberstextr   c                 C   s   ddd |D ||dgi}|   }|jddi |j| j d|d}| }z |jd	krp|d
 d d W S W n tk
r   Y dS X z|d d d W S  tk
r   Y dS X dS )zSend an SMS message.messagesc                 S   s   g | ]}d |iqS )tor   ).0Zdestinationr   r   r   
<listcomp>:   s    z/InfobipAPIWrapper._send_sms.<locals>.<listcomp>)destinationsfromr,   Content-Typezapplication/jsonz/sms/2/text/advanced)json   requestErrorserviceExceptionr,   Failed to send messager   	messageIdECould not get message ID from response, message was sent successfullyN)r)   r$   r%   postr   r4   status_codeKeyError)r&   r*   r+   r,   r4   r(   responseresponse_jsonr   r   r   	_send_sms3   s>     

zInfobipAPIWrapper._send_sms)
from_emailto_emailsubjectbodyr   c              
   C   s   zddl m} W n, tk
r< } ztd|W 5 d}~X Y nX ||||d}||d}|  }	|	jd|ji |	j| j d|d	}
|
	 }z |
j
d
kr|d d d W S W n tk
r   Y dS X z|d d d W S  tk
r   Y dS X dS )zSend an email message.r   )MultipartEncoderz^Unable to import requests_toolbelt, please install it with `pip install -U requests-toolbelt`.N)r2   r.   rC   r,   )fieldsr3   z/email/3/send)datar5   r6   r7   r,   r8   r-   r9   r:   )Zrequests_toolbeltrE   ImportErrorr)   r$   r%   content_typer;   r   r4   r<   r=   )r&   rA   rB   rC   rD   rE   eZ	form_datarG   r(   r>   r?   r   r   r   _send_email]   sH    
 

zInfobipAPIWrapper._send_email sms)rD   r.   r*   rC   channelr   c                 C   s   |dkrJ|dkrt d|dkr(t d|dkr8t d| j||g|dS |dkr|dkrbt d|dkrrt d	|dkrt d
|dkrt d| j||||dS t d| dd S )NrM   rL   z)Sender must be specified for SMS messagesz.Destination must be specified for SMS messagesz'Body must be specified for SMS messages)r*   r+   r,   emailz+Sender must be specified for email messagesz0Destination must be specified for email messagesz,Subject must be specified for email messagesz)Body must be specified for email messages)rA   rB   rC   rD   zChannel z is not supported)
ValueErrorr@   rK   )r&   rD   r.   r*   rC   rN   r   r   r   run   s8    zInfobipAPIWrapper.run)rL   rL   rL   rL   rM   )r   r   r   __doc__r   r   str__annotations__r   r   r   r   r   r!   r"   r)   r   r@   rK   rQ   r   r   r   r   r      s>   

  +   2     r   )rR   typingr   r   r   r!   Zlangchain_core.pydantic_v1r   r   Zlangchain_core.utilsr   Zrequests.adaptersr	   Zurllib3.utilr
   r   r   r   r   r   <module>   s   