
    dh                     v    S r SSKJrJrJrJr  SSKrSSKJr  SSK	J
r
JrJr  SSKJr  SSKJr   " S S	\
5      rg)
z%Util that sends messages via Infobip.    )AnyDictListOptionalNget_from_dict_or_env)	BaseModel
ConfigDictmodel_validator)HTTPAdapter)Retryc                      \ rS rSr% SrSr\\   \S'   Sr	\\   \S'   \
" SS9r\" S	S
9\S\S\4S j5       5       rS\R$                  4S jrS\S\\   S\S\4S jrS\S\S\S\S\4
S jr     SS\S\S\S\S\S\4S jjrSrg)InfobipAPIWrapper   z&Wrapper for Infobip API for messaging.Ninfobip_api_keyzhttps://api.infobip.cominfobip_base_urlforbid)extrabefore)modevaluesreturnc                 F    [        USS5      US'   [        USS5      US'   U$ )z,Validate that api key exists in environment.r   INFOBIP_API_KEYr   INFOBIP_BASE_URLr   )clsr   s     ]/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/utilities/infobip.pyvalidate_environment&InfobipAPIWrapper.validate_environment   s?     %9%'8%
 ! &:&(:&
!"     c                     [        SS/ SQS9n[        US9n[        R                  " 5       nUR	                  SU5        UR
                  R                  SU R                   3SS	.5        U$ )
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   )selfretry_strategyadaptersessions       r   _get_requests_session'InfobipAPIWrapper._get_requests_session"   so     %6!

  +~F""$j'*#'(<(<'=!>;	
 r    senderdestination_phone_numberstextc                    SU Vs/ sH  nSU0PM	     snUUS./0nU R                  5       nUR                  R                  SS05        UR                  U R                   S3US9nUR                  5       n UR                  S:w  a  US	   S
   S   $   US   S   S   $ s  snf ! [         a     gf = f! [         a      gf = f)zSend an SMS message.messagesto)destinationsfromr6   Content-Typezapplication/jsonz/sms/2/text/advanced)json   requestErrorserviceExceptionr6   Failed to send messager   	messageIdECould not get message ID from response, message was sent successfully)r2   r,   r-   postr   r=   status_codeKeyError)	r.   r4   r5   r6   destinationr=   r1   responseresponse_jsons	            r   	_send_smsInfobipAPIWrapper._send_sms5   s   
  @Y%?X{+?X% # 

 %)$>$>$@ 2	
 '.ll$$%%9: '3 '

 'mmo	,##s*$^45GHPP +
	 ,Q/<<;%2  	,+	,
  	W	s)   B7B$ 
B4 $
B10B14CC
from_emailto_emailsubjectbodyc                     SSK Jn  UUUUS.nU" US9nU R                  5       n	U	R                  R                  SUR                  05        U	R                  U R                   S3US	9n
U
R                  5       n U
R                  S
:w  a  US   S   S   $   US   S   S   $ ! [         a  n[        S5      UeSnAff = f! [         a     gf = f! [         a      gf = f)zSend an email message.r   )MultipartEncoderz^Unable to import requests_toolbelt, please install it with `pip install -U requests-toolbelt`.N)r;   r9   rN   r6   )fieldsr<   z/email/3/send)datar>   r?   r@   r6   rA   r8   rB   rC   )requests_toolbeltrQ   ImportErrorr2   r,   r-   content_typerD   r   r=   rE   rF   )r.   rL   rM   rN   rO   rQ   e	form_datarS   r1   rH   rI   s               r   _send_emailInfobipAPIWrapper._send_email_   s.   
	: 	
	  y1$($>$>$@ 1 1	
 '.ll$$%]3 '3 '

 'mmo	,##s*$^45GHPP +
	 ,Q/<<G  	6 	>  	,+	,
  	W	s;   B" :C  
C "
B=,B88B= 
CCCCr9   channelc                 n   US:X  aE  US:X  a  [        S5      eUS:X  a  [        S5      eUS:X  a  [        S5      eU R                  UU/US9$ US:X  aV  US:X  a  [        S5      eUS:X  a  [        S	5      eUS:X  a  [        S
5      eUS:X  a  [        S5      eU R                  UUUUS9$ [        SU S35      e)Nsms z)Sender must be specified for SMS messagesz.Destination must be specified for SMS messagesz'Body must be specified for SMS messages)r4   r5   r6   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)rL   rM   rN   rO   zChannel z is not supported)
ValueErrorrJ   rY   )r.   rO   r9   r4   rN   r[   s         r   runInfobipAPIWrapper.run   s     e| !LMMRx !QRRrz !JKK>>+-$ "  
 | !NOORx !STT"} !OPPrz !LMM##!	 $   xy0ABCCr     )r^   r^   r^   r^   r]   )__name__
__module____qualname____firstlineno____doc__r   r   str__annotations__r   r
   model_configr   classmethodr   r   r   r)   r*   r2   r   rJ   rY   ra   __static_attributes__rc   r    r   r   r      s%   0%)OXc])&?hsm?L (#$ 3   $x'7'7 &((6:3i(GJ(	(T..),.7:.BE.	.d +D+D +D 	+D
 +D +D 
+D +Dr    r   )rh   typingr   r   r   r   r)   langchain_core.utilsr   pydanticr	   r
   r   requests.adaptersr   urllib3.utilr   r   rc   r    r   <module>rs      s0    + , ,  5 ; ; ) nD	 nDr    