
    {iU0                       S r SSKJr  SSKrSSKrSSKrSSKJrJr  SSK	J
r
  SSKJr  SSKJrJr  SSKJr  SS	KJrJrJr  SS
KJr  SSKJr  SSKJrJr  SSKJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*  SSK+J,r,  SSK-J.r.  \(       a  SSK/J0r0  SSK1J2r2  SSKJ3r3  SSK4J5r5  \Rl                  " \75      r8 " S S\5      r9 " S S5      r:S\4     SS jjr; " S S\95      r<g)zBprawcore.sessions: Provides prawcore.Session and prawcore.session.    )annotationsN)ABCabstractmethod)deepcopy)pformat)TYPE_CHECKINGAny)urljoin)ChunkedEncodingErrorConnectionErrorReadTimeout)codes   )BaseAuthorizer)TIMEOUTWINDOW_SIZE)BadJSON
BadRequestConflictInvalidInvocationNotFoundRedirectRequestExceptionServerErrorSpecialErrorTooLargeTooManyRequestsUnavailableForLegalReasons
URITooLong)RateLimiter)authorization_error_class)BufferedReader)Response)
Authorizer)	Requestorc                  6    \ rS rSrSr\SS j5       rSS jrSrg)	RetryStrategy/   zAn abstract class for scheduling request retries.

The strategy controls both the number and frequency of retry attempts.

Instances of this class are immutable.

c                    g N selfs    K/home/james-whalen/.local/lib/python3.13/site-packages/prawcore/sessions.py_sleep_secondsRetryStrategy._sleep_seconds8   s        c                    U R                  5       nUb3  SUS S3n[        R                  U5        [        R                  " U5        gg)z0Sleep until we are ready to attempt the request.Nz
Sleeping: z0.2fz seconds prior to retry)r/   logdebugtimesleep)r-   sleep_secondsmessages      r.   r6   RetryStrategy.sleep<   sE    ++-$"="66MNGIIgJJ}% %r1   r+   Nreturnzfloat | Noner;   None)	__name__
__module____qualname____firstlineno____doc__r   r/   r6   __static_attributes__r+   r1   r.   r'   r'   /   s       &r1   r'   c                     \ rS rSrSr\\\4rSS\	S   \	S   \	S   \	S   \	S	   1r
0 \	S   \_\	S
   \_\	S   \_\	S   \_\	S   \_\	S   \_\	S   \_\	S   \_\	S   \_\	S   \_\	S   \_\	S   \_\	S	   \_\	S   \_\	S   \_\	S   \_S\_S\0Er\	S   \	S   \	S   1r\          S&S j5       r\S'S j5       rS(S jrS)S jr\4     S*S jjr                      S+S jr                   S,S jr! S-                 S.S! jjr"S/S" jr#S)S# jr$S S S S \%4               S0S$ jjr&S%r'g )1SessionE   z3The low-level connection interface to Reddit's API.i  i
  bad_gatewaygateway_timeoutinternal_server_errorrequest_timeoutservice_unavailablebad_requestconflictfound	forbidden
media_typemoved_permanently	not_foundrequest_entity_too_largerequest_uri_too_largetoo_many_requestsunauthorizedunavailable_for_legal_reasonsacceptedcreatedokc                    [         R                  SX[        R                  " 5       5        [         R                  S[        U 5      5        [         R                  S[        U5      5        g )NzFetching: %s %s at %szData: %sz
Params: %s)r3   r4   r5   r   )datamethodparamsurls       r.   _log_requestSession._log_requestj   s?     			)6		D		*gdm,		,0r1   c                B    U R                   R                  R                  $ r*   )_authorizer_authenticator
_requestorr,   s    r.   re   Session._requestoru   s    ..999r1   c                    U $ )2Allow this object to be used as a context manager.r+   r,   s    r.   	__enter__Session.__enter__y   s    r1   c                $    U R                  5         g)rh   N)close)r-   _argss     r.   __exit__Session.__exit__}   s    

r1   c                    [        U[        5      (       d  SU 3n[        U5      eXl        [	        US9U l        [        U l        g)zPrepare the connection to Reddit's API.

:param authorizer: An instance of :class:`.Authorizer`.
:param window_size: The size of the rate limit reset window in seconds.

zinvalid Authorizer: )window_sizeN)
isinstancer   r   rc   r    _rate_limiterFiniteRetryStrategy_retry_strategy_class)r-   
authorizerrq   msgs       r.   __init__Session.__init__   sC     *n55(5C#C((%([A%8"r1   c                    U(       a  [        U5      OUR                  n[        R                  SXU
5        U R	                  UUUUUU	U
UR                  5       S9$ )Nz Retrying due to %s status: %s %s)r\   filesjsonr]   r^   timeoutr_   retry_strategy_state)reprstatus_coder3   warning_request_with_retriesconsume_available_retry)r-   r\   r{   r|   r]   r^   responser~   saved_exceptionr}   r_   statuss               r.   	_do_retrySession._do_retry   sb     +:o&x?S?S6L))!5!M!M!O * 

 
	
r1   c	                    U R                   R                  U R                  R                  U R                  UUSUUUUUS9
n	[
        R                  SU	R                  U	R                  R                  S5      U	R                  R                  S5      U	R                  R                  S5      U	R                  R                  S5      [        R                  " 5       5        U	S 4$ ! [         aS  n
UR                  5       (       a%  [        U
R                  U R                  5      (       d  e S U
R                  4s S n
A
$ S n
A
ff = f)NF)allow_redirectsr\   r{   r|   r^   r}   z?Response: %s (%s bytes) (rst-%s:rem-%s:used-%s ratelimit) at %scontent-lengthzx-ratelimit-resetzx-ratelimit-remainingzx-ratelimit-used)rs   callre   request_set_header_callbackr3   r4   r   headersgetr5   r   should_retry_on_failurerr   original_exceptionRETRY_EXCEPTIONS)r-   r\   r{   r|   r]   r^   r~   r}   r_   r   	exceptions              r.   _make_requestSession._make_request   s&   	6))..'')) % / H IIQ$$  $$%56  $$%89  $$%<=  $$%78		 T>! 	6(@@BB!00$2G2G  5555	6s   CC" "
D?,AD:4D?:D?Nc	                   Uc  U R                  5       nUR                  5         U R                  XXW5        U R                  UUUUUUUU5      u  pSnU	bN  U	R                  [
        S   :X  a7  U R                  R                  5         [        U R                  S5      (       a  SnUR                  5       (       a>  U(       d  U	b  U	R                  U R                  ;   a  U R                  UUUUUU	UU
UU5
      $ U	R                  U R                  ;   a  U R                  U	R                     " U	5      eU	R                  [
        S   :X  a  g U	R                  U R                  ;   d   SU	R                   35       eU	R                  R                  S5      S:X  a  g	 U	R!                  5       $ ! ["         a    [%        U	5      S ef = f)
NFrV   refreshT
no_contentzUnexpected status code: r   0 )ru   r6   r`   r   r   r   rc   _clear_access_tokenhasattrr   RETRY_STATUSESr   STATUS_EXCEPTIONSSUCCESS_STATUSESr   r   r|   
ValueErrorr   )r-   r\   r{   r|   r]   r^   r}   r_   r~   r   r   do_retrys               r.   r   Session._request_with_retries   s     '#'#=#=#? ""$$4$($6$6 	%
! H$8$8E.<Q$Q002t''337799(H,@,@DDWDW,W>>$  4#9#99(()=)=>xHH5#66  D$9$99	=%h&:&:%;<	=9 01S8	.==?" 	.(#-	.s   F& &F=c                    U R                   R                  5       (       d5  [        U R                   S5      (       a  U R                   R                  5         SSU R                   R                   30$ )Nr   Authorizationzbearer )rc   is_validr   r   access_tokenr,   s    r.   r   Session._set_header_callback  sZ    ((**wt7G7G/S/S$$&74+;+;+H+H*I!JKKr1   c                8    U R                   R                  5         g)z+Close the session and perform any clean up.N)re   rl   r,   s    r.   rl   Session.close  s    r1   c           
     d   [        U5      =(       d    0 nSUS'   [        U[        5      (       a)  [        U5      nSUS'   [        UR	                  5       5      n[        U[        5      (       a  [        U5      nSUS'   [        U R                  R                  U5      nU R                  UUUUUUUS9$ )a  Return the json content from the resource at ``path``.

:param method: The request verb. E.g., ``"GET"``, ``"POST"``, ``"PUT"``.
:param path: The path of the request. This path will be combined with the
    ``oauth_url`` of the Requestor.
:param data: Dictionary, bytes, or file-like object to send in the body of the
    request.
:param files: Dictionary, mapping ``filename`` to file-like object.
:param json: Object to be serialized to JSON in the body of the request.
:param params: The query parameters to send with the request.
:param timeout: Specifies a particular timeout, in seconds.

Automatically refreshes the access token if it becomes invalid and a refresh
token is available.

:raises: :class:`.InvalidInvocation` in such a case if a refresh token is not
    available.

r   raw_jsonr|   api_type)r\   r{   r|   r]   r^   r}   r_   )	r   rr   dictsorteditemsr
   re   	oauth_urlr   )	r-   r]   pathr\   r{   r|   r^   r}   r_   s	            r.   r   Session.request!  s    : &!'RzdD!!D>D%D$**,'DdD!!D>D%Ddoo//6)) * 
 	
r1   )rc   rs   ru   )
r\   zlist[tuple[str, str]] | Noner]   strr^   dict[str, int]r_   r   r;   r=   )r;   r%   )r;   rE   r<   )rv   zBaseAuthorizer | Nonerq   intr;   r=   )r\   list[tuple[str, Any]]r{   dict[str, BufferedReader]r|   dict[str, Any]r]   r   r^   r   r   zResponse | Noner~   rt   r   zException | Noner}   floatr_   r   r;   dict[str, Any] | str | None)r\   r   r{   r   r|   r   r]   r   r^   r   r~   rt   r}   r   r_   r   r;   z.tuple[Response, None] | tuple[None, Exception]r*   )r\   r   r{   r   r|   r   r]   r   r^   r   r}   r   r_   r   r~   zFiniteRetryStrategy | Noner;   r   )r;   zdict[str, str])r]   r   r   r   r\   dict[str, Any] | Noner{   z dict[str, BufferedReader] | Noner|   r   r^   r   r}   r   r;   r   )(r>   r?   r@   rA   rB   r   r   r   r   r   r   r   r   r   r   r!   r   r   r   r   r   r   r   r   staticmethodr`   propertyre   ri   rn   r   rx   r   r   r   r   rl   r   r   rC   r+   r1   r.   rE   rE   E   s   =,o{Km %& #$Nmkmj 	j8 	g	
 	k5 	 + 	%& 	l\ 	!"H 	kH 	()8 	%&
 	#$k 	!"O 	n8  	+	
%%& 	['( 	[), j)5+;U4[I1*11 1 	1
 
1 1 : : '9)9 9 
	9$
#
 )
 	

 
 
 "
 2
 *
 
 
 
%
6*6#*6 )*6 	*6
 *6 *6 2*6 *6 *6 
8*6j <@<.#<. )<. 	<.
 <. <. <. <. 9<. 
%<.|L
  '+26&*(, /
/
 /
 $	/

 0/
 $/
 &/
 /
 
%/
 /
r1   rE   c                    [        XS9$ )zReturn a :class:`.Session` instance.

:param authorizer: An instance of :class:`.Authorizer`.
:param window_size: The size of the rate limit reset window in seconds.

rv   rq   )rE   r   s     r.   sessionr   S  s     jBBr1   c                  D    \ rS rSrSrS	S
S jjrSS jrSS jrSS jrSr	g)rt   i`  zCA ``RetryStrategy`` that retries requests a finite number of times.c                    Xl         g)zbInitialize the strategy.

:param retries: Number of times to attempt a request (default: ``3``).

N_retries)r-   retriess     r.   rx   FiniteRetryStrategy.__init__c  s	      r1   c                    U R                   S:  a/  U R                   S:X  a  SOSnUS[        R                  " 5       -  -   $ g )N      r   )r   random)r-   bases     r.   r/   "FiniteRetryStrategy._sleep_secondsk  s9    ==1*1D!fmmo---r1   c                >    [        U 5      " U R                  S-
  5      $ )zAllow one fewer retry.r   )typer   r,   s    r.   r   +FiniteRetryStrategy.consume_available_retryq  s    Dz$--!+,,r1   c                     U R                   S:  $ )zEReturn ``True`` if and only if the strategy will allow another retry.r   r   r,   s    r.   r   +FiniteRetryStrategy.should_retry_on_failureu  s    }}q  r1   r   N)r   )r   r   r;   r=   r:   )r;   rt   )r;   bool)
r>   r?   r@   rA   rB   rx   r/   r   r   rC   r+   r1   r.   rt   rt   `  s    M -!r1   rt   )rv   r$   rq   r   r;   rE   )=rB   
__future__r   loggingr   r5   abcr   r   copyr   pprintr   typingr   r	   urllib.parser
   requests.exceptionsr   r   r   requests.status_codesr   authr   constr   r   
exceptionsr   r   r   r   r   r   r   r   r   r   r   r   r   
rate_limitr    utilr!   ior"   requests.modelsr#   r$   	requestorr%   	getLogger__package__r3   r'   rE   r   rt   r+   r1   r.   <module>r      s    H "    #   %   R R '   '    $ +!( $$&C &,K
 K
^ ""
C
C
C 
C!- !r1   