
    {i                        S r SSKJr  SSKrSSKrSSKJrJrJrJ	r	  \(       a  SSK
Jr  \R                  " \5      r " S S5      rg)zProvide the RateLimiter class.    )annotationsN)TYPE_CHECKINGAnyCallableMapping)Responsec                  T    \ rS rSrSrS	S jr          S
S jrSS jrSS jrSr	g)RateLimiter   z}Facilitates the rate limiting of requests to Reddit.

Rate limits are controlled based on feedback from requests to Reddit.

c               H    SU l         SU l        SU l        SU l        Xl        g)z*Create an instance of the RateLimit class.N)	remainingnext_request_timestampreset_timestampusedwindow_size)selfr   s     M/home/james-whalen/.local/lib/python3.13/site-packages/prawcore/rate_limit.py__init__RateLimiter.__init__   s&    '+48#-1 $	 +    c                    U R                  5         U" 5       US'   U" U0 UD6nU R                  UR                  5        U$ )a  Rate limit the call to ``request_function``.

:param request_function: A function call that returns an HTTP response object.
:param set_header_callback: A callback function used to set the request headers.
    This callback is called after any necessary sleep time occurs.
:param args: The positional arguments to ``request_function``.
:param kwargs: The keyword arguments to ``request_function``.

headers)delayupdater   )r   request_functionset_header_callbackargskwargsresponses         r   callRateLimiter.call   s@      	

/1y#T4V4H$$%r   c                    U R                   c  gU R                   [        R                  " 5       -
  nUS::  a  gSUS S3n[        R                  U5        [        R                  " U5        g)z;Sleep for an amount of time to remain under the rate limit.Nr   z
Sleeping: z0.2fz seconds prior to call)r   timelogdebugsleep)r   sleep_secondsmessages      r   r   RateLimiter.delay3   s\    &&.33diikAA}T22HI		'

=!r   c                N   SU;  a8  U R                   b*  U =R                   S-  sl         U =R                  S-  sl        g[        R                  " 5       n[        US   5      n[	        US   5      U l         [        US   5      U l        X#-   U l        U R                   S::  a  U R
                  U l        g[        U R
                  U[        [        UU R                  U R                  U R                   U R                  -   -  U R                  -  -
  -
  S5      S5      -   5      U l        g)af  Update the state of the rate limiter based on the response headers.

This method should only be called following an HTTP request to Reddit.

Response headers that do not contain ``x-ratelimit`` fields will be treated as a
single request. This behavior is to error on the safe-side as such responses
should trigger exceptions that indicate invalid behavior.

zx-ratelimit-remainingN   zx-ratelimit-resetzx-ratelimit-usedr   
   )
r   r   r#   intfloatr   r   minmaxr   )r   response_headersnowseconds_to_resets       r   r   RateLimiter.update>   s    #*::~~)!#		Q	iik/0CDE/0GHI();<=	"5>>Q*.*>*>D'&)  $((++t~~		/IJTYYVX
  
'
#r   )r   r   r   r   r   N)r   r-   returnNone)
r   zCallable[[Any], Response]r   zCallable[[], dict[str, str]]r   r   r   r   r5   r   )r5   r6   )r1   zMapping[str, str]r5   r6   )
__name__
__module____qualname____firstlineno____doc__r   r    r   r   __static_attributes__ r   r   r
   r
      sK    ,3 : 	
  
,	")
r   r
   )r;   
__future__r   loggingr#   typingr   r   r   r   requests.modelsr   	getLogger__package__r$   r
   r=   r   r   <module>rD      s9    $ "   8 8($Y
 Y
r   