Ë
    ûûàgƒ  ã                   ó    — d Z ddlZddlmZ ddlmZ ddlmZ  G d„ d«      Z	 G d„ d	e	«      Z
 G d
„ de
«      Z G d„ de
«      Z G d„ de
«      Zy)z'
Provides various throttling policies.
é    N)Úcache)ÚImproperlyConfigured)Úapi_settingsc                   ó"   — e Zd ZdZd„ Zd„ Zd„ Zy)ÚBaseThrottlez&
    Rate throttling of requests.
    c                 ó   — t        d«      ‚)zT
        Return `True` if the request should be allowed, `False` otherwise.
        z#.allow_request() must be overridden©ÚNotImplementedError©ÚselfÚrequestÚviews      úW/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/rest_framework/throttling.pyÚallow_requestzBaseThrottle.allow_request   s   € ô "Ð"GÓHÐHó    c                 ó^  — |j                   j                  d«      }|j                   j                  d«      }t        j                  }|�C|dk(  s|€|S |j	                  d«      }|t        |t        |«      «          }|j                  «       S |rdj                  |j	                  «       «      S |S )zå
        Identify the machine making the request by parsing HTTP_X_FORWARDED_FOR
        if present and number of proxies is > 0. If not use all of
        HTTP_X_FORWARDED_FOR if it is available, if not use REMOTE_ADDR.
        ÚHTTP_X_FORWARDED_FORÚREMOTE_ADDRr   ú,Ú )	ÚMETAÚgetr   ÚNUM_PROXIESÚsplitÚminÚlenÚstripÚjoin)r   r   ÚxffÚremote_addrÚnum_proxiesÚaddrsÚclient_addrs          r   Ú	get_identzBaseThrottle.get_ident   s    € ð �l‰l×ÑÐ5Ó6ˆØ—l‘l×&Ñ& }Ó5ˆÜ"×.Ñ.ˆàÐ"Ø˜aÒ 3 ;Ø"Ð"Ø—I‘I˜c“NˆEØ¤ [´#°e³*Ó!=Ð =Ñ>ˆKØ×$Ñ$Ó&Ð&á'*ˆr�w‰w�s—y‘y“{Ó#Ð;°Ð;r   c                  ó   — y)zm
        Optionally, return a recommended number of seconds to wait before
        the next request.
        N© ©r   s    r   ÚwaitzBaseThrottle.wait*   s   € ð
 r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r(   r&   r   r   r   r      s   „ ñòIò<ó&r   r   c                   ó|   — e Zd ZdZeZej                  ZdZdZ	e
j                  Zd„ Zd„ Zd„ Zd„ Zd„ Zd	„ Zd
„ Zd„ Zy)ÚSimpleRateThrottlea¡  
    A simple cache implementation, that only requires `.get_cache_key()`
    to be overridden.

    The rate (requests / seconds) is set by a `rate` attribute on the Throttle
    class.  The attribute is a string of the form 'number_of_requests/period'.

    Period should be one of: ('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day')

    Previous request information used for throttling is stored in the cache.
    zthrottle_%(scope)s_%(ident)sNc                 ó˜   — t        | dd «      s| j                  «       | _        | j                  | j                  «      \  | _        | _        y )NÚrate)ÚgetattrÚget_rater0   Ú
parse_rateÚnum_requestsÚdurationr'   s    r   Ú__init__zSimpleRateThrottle.__init__D   s7   € Ü�t˜V TÔ*ØŸ™›ˆDŒIØ+/¯?©?¸4¿9¹9Ó+EÑ(ˆÔ˜4�=r   c                 ó   — t        d«      ‚)z³
        Should return a unique cache-key which can be used for throttling.
        Must be overridden.

        May return `None` if the request should not be throttled.
        z#.get_cache_key() must be overriddenr	   r   s      r   Úget_cache_keyz SimpleRateThrottle.get_cache_keyI   s   € ô "Ð"GÓHÐHr   c                 óæ   — t        | dd«      s$d| j                  j                  z  }t        |«      ‚	 | j                  | j
                     S # t        $ r d| j
                  z  }t        |«      ‚w xY w)zR
        Determine the string representation of the allowed request rate.
        ÚscopeNz9You must set either `.scope` or `.rate` for '%s' throttlez+No default throttle rate set for '%s' scope)r1   Ú	__class__r)   r   ÚTHROTTLE_RATESr:   ÚKeyError)r   Úmsgs     r   r2   zSimpleRateThrottle.get_rateR   st   € ô �t˜W dÔ+ØNØ—>‘>×*Ñ*ñ+ˆCä& sÓ+Ð+ð	,Ø×&Ñ& t§z¡zÑ2Ð2øÜò 	,Ø?À$Ç*Á*ÑLˆCÜ& sÓ+Ð+ð	,ús   ³A Á$A0c                 óh   — |€y|j                  d«      \  }}t        |«      }dddddœ|d      }||fS )	z‰
        Given the request rate string, return a two tuple of:
        <allowed number of requests>, <period of time in seconds>
        )NNú/é   é<   i  i€Q )ÚsÚmÚhÚdr   )r   Úint)r   r0   ÚnumÚperiodr4   r5   s         r   r3   zSimpleRateThrottle.parse_ratea   sH   € ð
 ˆ<ØØ—j‘j “o‰ˆˆVÜ˜3“xˆØ ¨$°UÑ;¸FÀ1¹IÑFˆØ˜hÐ'Ð'r   c                 ór  — | j                   €y| j                  ||«      | _        | j                  €y| j                  j	                  | j                  g «      | _        | j                  «       | _        | j
                  ry| j
                  d   | j                  | j                  z
  k  rP| j
                  j                  «        | j
                  r*| j
                  d   | j                  | j                  z
  k  rŒPt        | j
                  «      | j                  k\  r| j                  «       S | j                  «       S )z«
        Implement the check to see if the request should be throttled.

        On success calls `throttle_success`.
        On failure calls `throttle_failure`.
        Téÿÿÿÿ)r0   r8   Úkeyr   r   ÚhistoryÚtimerÚnowr5   Úpopr   r4   Úthrottle_failureÚthrottle_successr   s      r   r   z SimpleRateThrottle.allow_requestm   sê   € ð �9‰9ÐØà×%Ñ% g¨tÓ4ˆŒØ�8‰8ÐØà—z‘z—~‘~ d§h¡h°Ó3ˆŒØ—:‘:“<ˆŒð �lŠl˜tŸ|™|¨BÑ/°4·8±8¸d¿m¹mÑ3KÒKØ�L‰L×ÑÔð �lŠl˜tŸ|™|¨BÑ/°4·8±8¸d¿m¹mÑ3KÓKäˆt�|‰|Ó × 1Ñ 1Ò1Ø×(Ñ(Ó*Ð*Ø×$Ñ$Ó&Ð&r   c                 óÆ   — | j                   j                  d| j                  «       | j                  j	                  | j
                  | j                   | j                  «       y)zd
        Inserts the current request's timestamp along with the key
        into the cache.
        r   T)rM   ÚinsertrO   r   ÚsetrL   r5   r'   s    r   rR   z#SimpleRateThrottle.throttle_success†   s?   € ð
 	�‰×Ñ˜A˜tŸx™xÔ(Ø�
‰
�‰�t—x‘x §¡¨t¯}©}Ô=Ør   c                  ó   — y)zP
        Called when a request to the API has failed due to throttling.
        Fr&   r'   s    r   rQ   z#SimpleRateThrottle.throttle_failure�   s   € ð r   c                 óø   — | j                   r*| j                  | j                  | j                   d   z
  z
  }n| j                  }| j                  t	        | j                   «      z
  dz   }|dk  ry|t        |«      z  S )zG
        Returns the recommended next request time in seconds.
        rK   rA   r   N)rM   r5   rO   r4   r   Úfloat)r   Úremaining_durationÚavailable_requestss      r   r(   zSimpleRateThrottle.wait•   sq   € ð �<Š<Ø!%§¡°$·(±(¸T¿\¹\È"Ñ=MÑ2MÑ!NÑà!%§¡Ðà!×.Ñ.´°T·\±\Ó1BÑBÀQÑFÐØ Ò"Øà!¤EÐ*<Ó$=Ñ=Ð=r   )r)   r*   r+   r,   Údefault_cacher   ÚtimerN   Úcache_formatr:   r   ÚDEFAULT_THROTTLE_RATESr<   r6   r8   r2   r3   r   rR   rQ   r(   r&   r   r   r.   r.   2   sV   „ ñ
ð €EØ�I‰I€EØ1€LØ€EØ!×8Ñ8€NòFò
Iò,ò
(ò'ò2òó>r   r.   c                   ó   — e Zd ZdZdZd„ Zy)ÚAnonRateThrottlez–
    Limits the rate of API calls that may be made by a anonymous users.

    The IP address of the request will be used as the unique cache key.
    Úanonc                 óž   — |j                   r|j                   j                  ry | j                  | j                  | j	                  |«      dœz  S ©N©r:   Úident)ÚuserÚis_authenticatedr]   r:   r$   r   s      r   r8   zAnonRateThrottle.get_cache_key­   sC   € Ø�<Š<˜GŸL™L×9Ò9Øà× Ñ Ø—Z‘ZØ—^‘^ GÓ,ñ$
ñ 
ð 	
r   N©r)   r*   r+   r,   r:   r8   r&   r   r   r`   r`   ¥   s   „ ñð
 €Eó
r   r`   c                   ó   — e Zd ZdZdZd„ Zy)ÚUserRateThrottlezç
    Limits the rate of API calls that may be made by a given user.

    The user id will be used as a unique cache key if the user is
    authenticated.  For anonymous requests, the IP address of the request will
    be used.
    rf   c                 óÎ   — |j                   r-|j                   j                  r|j                   j                  }n| j                  |«      }| j                  | j
                  |dœz  S rc   ©rf   rg   Úpkr$   r]   r:   ©r   r   r   re   s       r   r8   zUserRateThrottle.get_cache_keyÁ   sR   € Ø�<Š<˜GŸL™L×9Ò9Ø—L‘L—O‘O‰Eà—N‘N 7Ó+ˆEà× Ñ Ø—Z‘ZØñ$
ñ 
ð 	
r   Nrh   r&   r   r   rj   rj   ·   s   „ ñð €Eó	
r   rj   c                   ó2   ‡ — e Zd ZdZdZd„ Zˆ fd„Zd„ Zˆ xZS )ÚScopedRateThrottlea,  
    Limits the rate of API calls by different amounts for various parts of
    the API.  Any view that has the `throttle_scope` property set will be
    throttled.  The unique cache key will be generated by concatenating the
    user id of the request, and the scope of the view being accessed.
    Úthrottle_scopec                  ó   — y )Nr&   r'   s    r   r6   zScopedRateThrottle.__init__Ö   s   € ð 	r   c                 óð   •— t        || j                  d «      | _        | j                  sy| j                  «       | _        | j                  | j                  «      \  | _        | _        t        ‰| �%  ||«      S )NT)
r1   Ú
scope_attrr:   r2   r0   r3   r4   r5   Úsuperr   )r   r   r   r;   s      €r   r   z ScopedRateThrottle.allow_requestÛ   sb   ø€ ä˜T 4§?¡?°DÓ9ˆŒ
ð �zŠzØð —M‘M“OˆŒ	Ø+/¯?©?¸4¿9¹9Ó+EÑ(ˆÔ˜4œ=ô ‰wÑ$ W¨dÓ3Ð3r   c                 óÎ   — |j                   r-|j                   j                  r|j                   j                  }n| j                  |«      }| j                  | j
                  |dœz  S )zØ
        If `view.throttle_scope` is not set, don't apply this throttle.

        Otherwise generate the unique cache key by concatenating the user id
        with the `.throttle_scope` property of the view.
        rd   rl   rn   s       r   r8   z ScopedRateThrottle.get_cache_keyë   sT   € ð �<Š<˜GŸL™L×9Ò9Ø—L‘L—O‘O‰Eà—N‘N 7Ó+ˆEà× Ñ Ø—Z‘ZØñ$
ñ 
ð 	
r   )	r)   r*   r+   r,   rt   r6   r   r8   Ú__classcell__)r;   s   @r   rp   rp   Í   s   ø„ ñð "€Jòô
4ö 
r   rp   )r,   r\   Údjango.core.cacher   r[   Údjango.core.exceptionsr   Úrest_framework.settingsr   r   r.   r`   rj   rp   r&   r   r   ú<module>r{      sY   ðñó å 4Ý 7å 0÷#ñ #ôLp>˜ô p>ôf
Ð)ô 
ô$
Ð)ô 
ô,-
Ð+õ -
r   