o
    Ph                     @   s   d dl m Z  d dlmZ d dlmZ d dlmZmZ d dlZ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ZdS )    )copy)loads)time)ListUnionN)CaseInsensitiveDict)RequestStatusError)LRUDictCachec                   @   sD   e Zd ZdejddfddZeddefddZd	d
 Z	dd Z
dS )Responserequest_objreturnNc                 C   s   |j | _ |j| _|j| _|j| _|j| _|j| _|j| _|j| _|j| _|j	| _	|j
| _
|j| _|j| _|j| _|j| _|j| _|j| _d S N)status_codeheadersrawurlencodinghistoryreasoncookieselapsedrequestcontentapparent_encodingis_redirectis_permanent_redirectlinksnextok)selfr    r    T/var/www/html/env_mimamsha/lib/python3.10/site-packages/translatepy/utils/request.py__init__   s"   zResponse.__init__utf-8c                 C   s8   z| j |W S  ty   t| j || Y S w )z6Returns the text/str version of the response (decoded))r   decode	Exceptionstrencode)r   r   r    r    r!   textB   s
   zResponse.textc                 C   s(   | j dkrt| j djt| j ddS )zFRaise an exception if the status code of the response is less than 400i  zRequest Status Code: {code})codeN)r   r   formatr&   r   r    r    r!   raise_for_statusJ   s   
zResponse.raise_for_statusc                 K   s   t | jfi |S r   )r   r(   )r   kwargsr    r    r!   jsonO   s   zResponse.json)r#   )__name__
__module____qualname__requestsr
   r"   propertyr&   r(   r,   r.   r    r    r    r!   r
      s    4r
   c                   @   s   e Zd Zddeeef deeef fddZddefdd	Z	ded
e
fddZded
e
fddZed
efddZejdefddZdd ZdS )RequestN   
proxy_urlscache_durationc                 C   s   t  ddddd}t | _t | _t|| _|| _	d| _
t|tr&|gn	|dur.t|ng | _t| jdkr>dg| _dS dS )a  
        translatepy's version of `requests.Session`

        It includes caching, headers management and proxy management

        Parameters:
        ----------
            proxy_urls : str | list
                The URL(s) for the proxies to be used (they will be used as HTTP and HTTPS proxies)
            cache_duration : int | float
                The duration of the cache for GET requests

        Returns:
        --------
            Response:
                The response for the request
        z*/*zen-US,en-GB; q=0.5zgzip, deflatez
keep-alive)z
User-AgentAcceptzAccept-LanguagezAccept-Encoding
Connectionr   N)pyuseragentsrandomr2   Sessionsessionr	   GETCACHEfloatr7   r   _proxies_index
isinstancer&   listproxieslen)r   r6   r7   HEADERSr    r    r!   r"   T   s   

&zRequest.__init__r   c                 C   s$   |dur| j j||d dS dS )z$Internal function to set the proxiesN)httphttps)r=   rC   update)r   r   r    r    r!   _set_session_proxies   s   zRequest._set_session_proxiesr   c                 K   sf   |  | j| j  | jj|fi |}t|}|  | jt| jd kr.|  jd7  _|S d| _|S )a  
        Makes a POST request with the given URL

        Parameters:
        ----------
            url : str
                The URL to send a POST request to
            **kwargs : parameters
                This is the options that will be passed to requests.Session.post

        Returns:
        --------
            Response:
                The response for the request
           r   )rI   rC   r@   r=   postr
   closerD   )r   r   r-   r   resultr    r    r!   rK      s   zRequest.postc                 K   s   t |t | }|| jv r!t | j| d  | jk r!| j| d S | | j| j  | jj|fi |}t	|}|
  | jt| jd krN|  jd7  _nd| _t t|d| j|< |S )a  
        Makes a GET request with the given URL

        Parameters:
        ----------
            url : str
                The URL to send a GET request to
            **kwargs : parameters
                This is the options that will be passed to requests.Session.get

        Returns:
        --------
            Response:
                The response for the request
        	timestampresponserJ   r   )rN   rO   )r&   r>   r   r7   rI   rC   r@   r=   getr
   rL   rD   r   )r   r   r-   
_cache_keyr   rM   r    r    r!   rP      s   $zRequest.getc                 C   s   | j jS )zThe headers set for the session)r=   r   r+   r    r    r!   r      s   zRequest.headersheader_key_valuec              	   C   sR   |  D ]"\}}|du rz	| jj| W q ty   Y qw | jj| qdS )zSetter for the headersN)itemsr=   r   popr%   rH   )r   rR   keyvaluer    r    r!   r      s   c                 C   s   | j   dS )zClosing the sessionN)r=   rL   r+   r    r    r!   __del__   s   zRequest.__del__)Nr5   r   )r/   r0   r1   r   r&   r   intr?   r"   rI   r
   rK   rP   r3   r   r   setterdictrW   r    r    r    r!   r4   S   s    $-!r4   )r   r.   r   r   typingr   r   r:   r2   requests.modelsr   translatepy.exceptionsr   translatepy.utils.lru_cacherr	   r
   r4   r    r    r    r!   <module>   s    F