U
    ^g                     @  s   d dl mZ d dlZd dlZd dlZd dlmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZ d dlmZ ejrdd	lmZ G d
d dZejdddG dd dZejdddG dd dZdS )    )annotationsN)CallableIteratorListOptionalTupleUnion)request)Response)RateLimitItem
parse_many)RateLimiter   )Limiterc                   @  s   e Zd ZU dZded< ded< ded< ded< d	dd
ddddddZeddddZeddddZeddddZ	eddddZ
dS )RequestLimitzJ
    Provides details of a rate limit within the context of a request
    r   limitstrkeyboolbreachedsharedr   z	List[str]None)	extensionr   request_argsr   r   returnc                 C  s:   t || _|| _|| _|j| | _|| _|| _d | _	d S N)
weakrefproxyr   r   r   Zkey_forr   r   r   _window)selfr   r   r   r   r    r    :/tmp/pip-unpacked-wheel-8ut481ky/flask_limiter/wrappers.py__init__"   s    zRequestLimit.__init__r   r   c                 C  s   t t| jjS r   )typingcastr   r   limiterr   r    r    r!   r&   2   s    zRequestLimit.limiterzTuple[int, int]c                 C  s$   | j s| jj| jf| j | _ | j S r   )r   r&   Zget_window_statsr   r   r'   r    r    r!   window6   s    zRequestLimit.windowintc                 C  s   t | jd d S )z/Timestamp at which the rate limit will be resetr   r   )r)   r(   r'   r    r    r!   reset_at=   s    zRequestLimit.reset_atc                 C  s
   | j d S )z&Quantity remaining for this rate limitr   )r(   r'   r    r    r!   	remainingC   s    zRequestLimit.remainingN)__name__
__module____qualname____doc____annotations__r"   propertyr&   r(   r*   r+   r    r    r    r!   r      s   
r   T)equnsafe_hashc                   @  s   e Zd ZU dZded< ded< ded< dZd	ed
< dZded< dZded< dZded< dZ	ded< dZ
ded< dZded< dZded< dZd	ed< ddddZed	dd d!Zeddd"d#Zed$dd%d&Zed	dd'd(Zd)dd)d*d+d,ZdS )-Limitz@
    simple wrapper to encapsulate limits and their context
    r   r   Callable[[], str]Zkey_func*Optional[Union[str, Callable[[str], str]]]_scopeFr   
per_methodNOptional[Tuple[str, ...]]methodsOptional[str]error_messageOptional[Callable[[], bool]]exempt_whenOptional[bool]override_defaults$Optional[Callable[[Response], bool]]deduct_when6Optional[Callable[[RequestLimit], Optional[Response]]]	on_breachr   zUnion[Callable[[], int], int]_costr   r   r#   c                 C  s    | j rtdd | j D | _ d S )Nc                 S  s   g | ]}|  qS r    )lower).0kr    r    r!   
<listcomp>_   s     z'Limit.__post_init__.<locals>.<listcomp>)r:   tupler'   r    r    r!   __post_init__]   s    zLimit.__post_init__c                 C  s   | j r|   S dS )zCheck if the limit is exempt.F)r>   r'   r    r    r!   	is_exempta   s    zLimit.is_exemptc                 C  s    t | jr| tjpdS | jS )N )callabler7   r	   endpointr'   r    r    r!   scopei   s    zLimit.scoper)   c                 C  s   t | jtr| jS |  S r   )
isinstancerE   r)   r'   r    r    r!   costq   s    z
Limit.costc                 C  s   | j dk	otj | j kS )z4Check if the limit is not applicable for this methodN)r:   r	   methodrF   r'   r    r    r!   method_exemptx   s    zLimit.method_exemptr   )rO   rS   r   c                 C  sN   | j }|r&| jr|}q*| d| }n|}| jrJ|s8t|d|  7 }|S )z
        Derive final bucket (scope) for this limit given the endpoint
        and request method. If the limit is shared between multiple
        routes, the scope does not include the endpoint.
        :)rP   r   r8   AssertionErrorupper)r   rO   rS   Zlimit_scoperP   r    r    r!   	scope_for~   s    zLimit.scope_for)r,   r-   r.   r/   r0   r8   r:   r<   r>   r@   rB   rD   rE   r   rK   r1   rL   rP   rR   rT   rX   r    r    r    r!   r4   J   s.   
r4   c                   @  s   e Zd ZU dZded< ded< dZded< dZd	ed
< dZded< dZded< dZ	ded< dZ
ded< dZded< dZded< dZded< dZded< ddddZdS )
LimitGroupzf
    represents a group of related limits either from a string or a callable
    that returns one
    zUnion[Callable[[], str], str]limit_providerr5   key_functionNr6   rP   r9   r:   r;   r<   r=   r>   Fr?   r@   rA   rB   rC   rD   r   r8   z'Optional[Union[Callable[[], int], int]]rR   r   zIterator[Limit]r#   c                 c  sp   t | jr|  n| j}|r$t|ng }|D ]>}t|| j| j| j| j| j| j	| j
| j| j| jp`d| jV  q,d S )Nr   )rN   rZ   r   r4   r[   rP   r8   r:   r<   r>   r@   rB   rD   rR   r   )r   Z	limit_strZlimit_itemsr   r    r    r!   __iter__   s(    
zLimitGroup.__iter__)r,   r-   r.   r/   r0   rP   r:   r<   r>   r@   rB   rD   r8   rR   r   r\   r    r    r    r!   rY      s   
rY   )
__future__r   dataclassesr$   r   r   r   r   r   r   r   Zflaskr	   Zflask.wrappersr
   Zlimitsr   r   Zlimits.strategiesr   TYPE_CHECKINGr   r   r   	dataclassr4   rY   r    r    r    r!   <module>   s    9H