U
    ™ÀÂh)$  ã                   @   s    d Z ddlmZ ddlmZmZmZmZmZm	Z	 ddl
Z
ddlZddlmZ ddlmZ G dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZeZdS )z@Lightweight wrapper around requests library, with async support.é    )Úasynccontextmanager)ÚAnyÚAsyncGeneratorÚDictÚLiteralÚOptionalÚUnionN)Ú	BaseModel)ÚResponsec                   @   sÌ  e Zd ZU dZdZeeeef  ed< dZ	ee
j ed< dZee ed< dZee ed< G dd	„ d	ƒZeeejd
œdd„Zeeeef eejdœdd„Zeeeef eejdœdd„Zeeeef eejdœdd„Zeeejd
œdd„Zeeeeee
jdf dœdd„ƒZeeeee
jdf d
œdd„ƒZeeeeef eee
jdf dœdd„ƒZeeeeef eee
jdf dœdd„ƒZeeeeef eee
jdf dœdd „ƒZeeeee
jdf d
œd!d"„ƒZ dS )#ÚRequestszÇWrapper around requests to handle auth and async.

    The main purpose of this wrapper is to handle authentication (by saving
    headers) and enable easy async methods on the same base object.
    NÚheadersÚ
aiosessionÚauthTÚverifyc                   @   s   e Zd ZdZdZdS )zRequests.ConfigTÚforbidN©Ú__name__Ú
__module__Ú__qualname__Zarbitrary_types_allowedÚextra© r   r   úJ/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/requests.pyÚConfig   s   r   ©ÚurlÚkwargsÚreturnc                 K   s    t j|f| j| j| jdœ|—ŽS )ú GET the URL and return the text.©r   r   r   )ÚrequestsÚgetr   r   r   ©Úselfr   r   r   r   r   r       s    ÿ  ÿÿzRequests.get©r   Údatar   r   c                 K   s"   t j|f|| j| j| jdœ|—ŽS )ú$POST to the URL and return the text.©Újsonr   r   r   )r   Úpostr   r   r   ©r"   r   r$   r   r   r   r   r(   "   s    ÿûúzRequests.postc                 K   s"   t j|f|| j| j| jdœ|—ŽS )ú"PATCH the URL and return the text.r&   )r   Úpatchr   r   r   r)   r   r   r   r+   -   s    ÿûúzRequests.patchc                 K   s"   t j|f|| j| j| jdœ|—ŽS )ú PUT the URL and return the text.r&   )r   Úputr   r   r   r)   r   r   r   r-   8   s    ÿûúzRequests.putc                 K   s    t j|f| j| j| jdœ|—ŽS )ú#DELETE the URL and return the text.r   )r   Údeleter   r   r   r!   r   r   r   r/   C   s    ÿ  ÿÿzRequests.delete)Úmethodr   r   r   c                 K  s¬   | j sht ¡ 4 I dH šD}|j||f| j| jdœ|—Ž4 I dH š}|V  W 5 Q I dH R X W 5 Q I dH R X n@| j j||f| j| jdœ|—Ž4 I dH š}|V  W 5 Q I dH R X dS )zMake an async request.N)r   r   )r   ÚaiohttpÚClientSessionÚrequestr   r   )r"   r0   r   r   ÚsessionÚresponser   r   r   Ú	_arequestI   s0    þüû(þüûzRequests._arequestc              
   K  s4   | j d|f|Ž4 I dH š}|V  W 5 Q I dH R X dS )ú/GET the URL and return the text asynchronously.ÚGETN©r6   ©r"   r   r   r5   r   r   r   Úagetb   s    zRequests.agetc              
   K  s<   | j d|fd|i|—Ž4 I dH š}|V  W 5 Q I dH R X dS )ú3POST to the URL and return the text asynchronously.ÚPOSTr'   Nr9   ©r"   r   r$   r   r5   r   r   r   Úapostj   s    "zRequests.apostc              
   K  s<   | j d|fd|i|—Ž4 I dH š}|V  W 5 Q I dH R X dS )ú1PATCH the URL and return the text asynchronously.ÚPATCHr'   Nr9   r>   r   r   r   Úapatchr   s    "zRequests.apatchc              
   K  s<   | j d|fd|i|—Ž4 I dH š}|V  W 5 Q I dH R X dS )ú/PUT the URL and return the text asynchronously.ÚPUTr'   Nr9   r>   r   r   r   Úaputz   s    "zRequests.aputc              
   K  s4   | j d|f|Ž4 I dH š}|V  W 5 Q I dH R X dS )ú2DELETE the URL and return the text asynchronously.ÚDELETENr9   r:   r   r   r   Úadelete‚   s    zRequests.adelete)!r   r   r   Ú__doc__r   r   r   ÚstrÚ__annotations__r   r1   r2   r   r   r   Úboolr   r   r
   r    r(   r+   r-   r/   r   r   ÚClientResponser6   r;   r?   rB   rE   rH   r   r   r   r   r      sZ   
  þ þ 
 þ 
 þ 
 þ þr   c                   @   sZ  e Zd ZU dZdZeeeef  ed< dZ	ee
j ed< dZee ed< dZed ed< d	Zeed
< G dd„ dƒZeedœdd„ƒZeeeeeef f dœdd„Ze
jeeeeef f dœdd„Zeeeeeeef f dœdd„Zeeeef eeeeeef f dœdd„Zeeeef eeeeeef f dœdd„Zeeeef eeeeeef f dœdd„Zeeeeeeef f dœdd „Zeeeeeeef f dœd!d"„Z eeeef eeeeeef f dœd#d$„Z!eeeef eeeeeef f dœd%d&„Z"eeeef eeeeeef f dœd'd(„Z#eeeeeeef f dœd)d*„Z$dS )+ÚGenericRequestsWrapperz,Lightweight wrapper around requests library.Nr   r   r   Útext©rO   r'   Úresponse_content_typeTr   c                   @   s   e Zd ZdZdZdS )zGenericRequestsWrapper.ConfigTr   Nr   r   r   r   r   r   ”   s   r   )r   c                 C   s   t | j| j| j| jdS )N)r   r   r   r   )r   r   r   r   r   )r"   r   r   r   r   ˜   s    üzGenericRequestsWrapper.requests)r5   r   c                 C   s6   | j dkr|jS | j dkr"| ¡ S td| j › ƒ‚d S ©NrO   r'   zInvalid return type: ©rQ   rO   r'   Ú
ValueError©r"   r5   r   r   r   Ú_get_resp_content¡   s
    

z(GenericRequestsWrapper._get_resp_contentc                 Ã   sD   | j dkr| ¡ I d H S | j dkr0| ¡ I d H S td| j › ƒ‚d S rR   rS   rU   r   r   r   Ú_aget_resp_content©   s
    

z)GenericRequestsWrapper._aget_resp_contentr   c                 K   s   |   | jj|f|Ž¡S )r   )rV   r   r    r!   r   r   r   r    ³   s    zGenericRequestsWrapper.getr#   c                 K   s   |   | jj||f|Ž¡S )r%   )rV   r   r(   r)   r   r   r   r(   ·   s    zGenericRequestsWrapper.postc                 K   s   |   | jj||f|Ž¡S )r*   )rV   r   r+   r)   r   r   r   r+   ½   s    zGenericRequestsWrapper.patchc                 K   s   |   | jj||f|Ž¡S )r,   )rV   r   r-   r)   r   r   r   r-   Ã   s    zGenericRequestsWrapper.putc                 K   s   |   | jj|f|Ž¡S )r.   )rV   r   r/   r!   r   r   r   r/   É   s    zGenericRequestsWrapper.deletec                 Ë   sL   | j j|f|Ž4 I dH š$}|  |¡I dH W  5 Q I dH R £ S Q I dH R X dS )r7   N)r   r;   rW   r:   r   r   r   r;   Í   s    zGenericRequestsWrapper.agetc                 Ë   sN   | j j||f|Ž4 I dH š$}|  |¡I dH W  5 Q I dH R £ S Q I dH R X dS )r<   N)r   r?   rW   r>   r   r   r   r?   Ò   s    zGenericRequestsWrapper.apostc                 Ë   sN   | j j||f|Ž4 I dH š$}|  |¡I dH W  5 Q I dH R £ S Q I dH R X dS )r@   N)r   rB   rW   r>   r   r   r   rB   Ù   s    zGenericRequestsWrapper.apatchc                 Ë   sN   | j j||f|Ž4 I dH š$}|  |¡I dH W  5 Q I dH R £ S Q I dH R X dS )rC   N)r   rE   rW   r>   r   r   r   rE   à   s    zGenericRequestsWrapper.aputc                 Ë   sL   | j j|f|Ž4 I dH š$}|  |¡I dH W  5 Q I dH R £ S Q I dH R X dS )rF   N)r   rH   rW   r:   r   r   r   rH   ç   s    zGenericRequestsWrapper.adelete)%r   r   r   rI   r   r   r   rJ   rK   r   r1   r2   r   r   rQ   r   r   rL   r   Úpropertyr   r   r
   r   rV   rM   rW   r    r(   r+   r-   r/   r;   r?   rB   rE   rH   r   r   r   r   rN   ‹   s^   
 	þ
" 
 þ 
 þ 
 þ"" 
 þ 
 þ 
 þrN   c                   @   s"   e Zd ZU dZdZed ed< dS )ÚJsonRequestsWrapperz‰Lightweight wrapper around requests library, with async support.

    The main purpose of this wrapper is to always return a json output.r'   rP   rQ   N©r   r   r   rI   rQ   r   rK   r   r   r   r   rY   í   s   
rY   c                   @   s"   e Zd ZU dZdZed ed< dS )ÚTextRequestsWrapperz‰Lightweight wrapper around requests library, with async support.

    The main purpose of this wrapper is to always return a text output.rO   rP   rQ   NrZ   r   r   r   r   r[   õ   s   
r[   )rI   Ú
contextlibr   Útypingr   r   r   r   r   r   r1   r   Zlangchain_core.pydantic_v1r	   r
   r   rN   rY   r[   ZRequestsWrapperr   r   r   r   Ú<module>   s    b	