o
    ;h'                     @  s  U d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlmZ G dd dedZG dd deZG dd deZeddG dd deZee ee B ee B ZeeedZded< dS )z
Rate limiting strategies
    )annotationsN)ABCMetaabstractmethod)floorinf)versionadded)SlidingWindowCounterSupport   )RateLimitItem)MovingWindowSupportStorageStorageTypes)cast)WindowStatsc                   @  sV   e Zd ZdddZedddddZedddddZedddZdddZdS )RateLimiterstorager   c                 C  s   t |tsJ || _d S N)
isinstancer   r   selfr    r   D/var/www/html/venv/lib/python3.10/site-packages/limits/strategies.py__init__   s   
zRateLimiter.__init__r	   costitemr
   identifiersstrr   intreturnboolc                G     t 
        Consume the rate limit

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :param cost: The cost of this hit, default 1
        NotImplementedErrorr   r   r   r   r   r   r   hit      
zRateLimiter.hitc                G  r!   )a  
        Check the rate limit without consuming from it.

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
          instance of the limit
        :param cost: The expected cost to be consumed, default 1
        r$   r&   r   r   r   test&   r(   zRateLimiter.testr   c                 G  r!   )
  
        Query the reset time and remaining amount for the limit

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :return: (reset time, remaining)
        r$   r   r   r   r   r   r   get_window_stats2   r(   zRateLimiter.get_window_statsNonec                 G  s   | j |j| S r   )r   clearkey_forr+   r   r   r   r.   >   s   zRateLimiter.clearNr   r   r   r
   r   r   r   r   r   r    r   r
   r   r   r   r   r   r
   r   r   r   r-   )	__name__
__module____qualname__r   r   r'   r)   r,   r.   r   r   r   r   r      s    
r   )	metaclassc                      sL   e Zd ZdZd fddZdddddZdddddZdddZ  ZS )MovingWindowRateLimiterz4
    Reference: :ref:`strategies:moving window`
    r   r   c                   s4   t |dst |dstd|j t | d S )Nacquire_entryget_moving_windowz@MovingWindowRateLimiting is not implemented for storage of type hasattrr%   	__class__superr   r   r=   r   r   r   G   s   z MovingWindowRateLimiter.__init__r	   r   r   r
   r   r   r   r   r   r    c                G  s&   t t| jj|j| |j| |dS )a  
        Consume the rate limit

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :param cost: The cost of this hit, default 1
        :return: (reset time, remaining)
        amount)r   r   r   r9   r/   rA   
get_expiryr&   r   r   r   r'   Q   s   zMovingWindowRateLimiter.hitc                G  s0   t t| j|j| |j| d |j| kS a  
        Check if the rate limit can be consumed

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :param cost: The expected cost to be consumed, default 1
        r	   )r   r   r   r:   r/   rA   rB   r&   r   r   r   r)   `   s   zMovingWindowRateLimiter.testr   c                 G  sB   t t| j|j| |j| \}}||  }t||j| S )a  
        returns the number of requests remaining within this limit.

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :return: tuple (reset time, remaining)
        )r   r   r   r:   r/   rA   rB   r   )r   r   r   Zwindow_startZwindow_itemsresetr   r   r   r,   s   s   	z(MovingWindowRateLimiter.get_window_statsr0   r1   r2   )	r4   r5   r6   __doc__r   r'   r)   r,   __classcell__r   r   r?   r   r8   B   s    
r8   c                   @  s:   e Zd ZdZdddddZdddddZdddZdS )FixedWindowRateLimiterz3
    Reference: :ref:`strategies:fixed window`
    r	   r   r   r
   r   r   r   r   r   r    c                G  s"   | j j|j| | |d|jkS )r#   r@   )r   incrr/   rB   rA   r&   r   r   r   r'      s   zFixedWindowRateLimiter.hitc                G  s    | j |j| |j| d k S rC   )r   getr/   rA   r&   r   r   r   r)      s    
zFixedWindowRateLimiter.testr   c                 G  s:   t d|j| j|j|  }| j|j| }t||S )r*   r   )maxrA   r   rI   r/   rB   r   )r   r   r   	remainingrD   r   r   r   r,      s   	
z'FixedWindowRateLimiter.get_window_statsNr1   r2   )r4   r5   r6   rE   r'   r)   r,   r   r   r   r   rG      s
    rG   z4.1)versionc                      s`   e Zd ZdZd  fddZd!ddZddd"ddZddd"ddZd#ddZd$ddZ	  Z
S )%SlidingWindowCounterRateLimiterz=
    Reference: :ref:`strategies:sliding window counter`
    r   r   c                   s4   t |dr
t |dstd|j t | d S )Nget_sliding_windowacquire_sliding_window_entryzHSlidingWindowCounterRateLimiting is not implemented for storage of type r;   r   r?   r   r   r      s   z(SlidingWindowCounterRateLimiter.__init__r   r
   previous_countr   previous_expires_infloatcurrent_countr   c                 C  s   || |   | S )zu
        Return the approximated by weighting the previous window count and adding the current window count.
        )rB   )r   r   rP   rQ   rS   r   r   r   _weighted_count   s   
z/SlidingWindowCounterRateLimiter._weighted_countr	   r   r   r   r   r    c                G  s$   t t| j|j| |j| |S r"   )r   r   r   rO   r/   rA   rB   r&   r   r   r   r'      s   	z#SlidingWindowCounterRateLimiter.hitc                G  sD   t t| j|j| | \}}}}| |||||j| d k S rC   )r   r   r   rN   r/   rB   rT   rA   )r   r   r   r   rP   rQ   rS   _r   r   r   r)      s   	
z$SlidingWindowCounterRateLimiter.testr   c              
   G  s   t t| j|j| | \}}}}td|jt| 	|||| }t

 }|s0|s0t||S | }	tt}
}|rA||	|  }
|rG||	 }t|t|
| |S )a  
        Query the reset time and remaining amount for the limit.

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :return: WindowStats(reset time, remaining)
        r   )r   r   r   rN   r/   rB   rJ   rA   r   rT   timer   r   min)r   r   r   rP   rQ   rS   Zcurrent_expires_inrK   nowZexpiryZprevious_reset_inZcurrent_reset_inr   r   r   r,      s2   	



z0SlidingWindowCounterRateLimiter.get_window_statsr-   c                 G  s   t t| j|j| | S r   )r   r   r   Zclear_sliding_windowr/   rB   r+   r   r   r   r.     s   z%SlidingWindowCounterRateLimiter.clearr0   )
r   r
   rP   r   rQ   rR   rS   r   r   rR   r1   r2   r3   )r4   r5   r6   rE   r   rT   r'   r)   r,   r.   rF   r   r   r?   r   rM      s    


&rM   )zsliding-window-counterzfixed-windowzmoving-windowzdict[str, KnownStrategy]
STRATEGIES) rE   
__future__r   rV   abcr   r   mathr   r   Zdeprecated.sphinxr   Zlimits.storage.baser   Zlimitsr
   r   r   r   r   typingr   utilr   r   r8   rG   rM   typeZKnownStrategyrY   __annotations__r   r   r   r   <module>   s4    -B3n