
    h                     &   S SK r S SKrS SKrS SKJr  S SKJrJrJrJ	r	J
r
  \ R                  R                  SS5      r\ R                  R                  SS5      r\
" S5      rS	rS
\S\4S jrS\S
\4S jr      SS\S
\S\S\S\S\S\S\4S jjrg)    N)Any)IS_COLABLOGGERTQDM	TryExceptcolorstrULTRALYTICS_HUB_APIzhttps://api.ultralytics.comULTRALYTICS_HUB_WEBzhttps://hub.ultralytics.comzUltralytics HUB: z]If this issue persists please visit https://github.com/ultralytics/hub/issues for assistance.urlreturnc                     [         (       d  [        S5      eSSKJn  SSKJn  UR                  UR                  SU  S35      5        UR                  S5      $ )a  
Make an AJAX request with cookies attached in a Google Colab environment.

Args:
    url (str): The URL to make the request to.

Returns:
    (Any): The response data from the AJAX request.

Raises:
    OSError: If the function is not run in a Google Colab environment.
z:request_with_credentials() must run in a Colab environmentr   )output)displayz
            window._hub_tmp = new Promise((resolve, reject) => {
                const timeout = setTimeout(() => reject("Failed authenticating existing browser session"), 5000)
                fetch("a  ", {
                    method: 'POST',
                    credentials: 'include'
                })
                    .then((response) => resolve(response.json()))
                    .then((json) => {
                    clearTimeout(timeout);
                    }).catch((err) => {
                    clearTimeout(timeout);
                    reject(err);
                });
            });
            _hub_tmp)r   OSErrorgoogle.colabr   IPythonr   
Javascripteval_js)r   r   r   s      O/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/hub/utils.pyrequest_with_credentialsr      s\     8RSS#OO u 	
( >>*%%    methodc                    SSK nUR                  SS5      nU(       d  UR                  " X40 UD6$ UR                  " X4SS0UD6n[        [	        U[
        5      (       a  UR                  R                  SS5      OU5      n [        USSS	S
9nUR                  S	S9 H  nUR                  [        U5      5        M     UR                  5         U$ ! UR                  R                   a    UR                  5          U$ f = f)ax  
Make an HTTP request using the specified method and URL, with an optional progress bar.

Args:
    method (str): The HTTP method to use (e.g. 'GET', 'POST').
    url (str): The URL to send the request to.
    **kwargs (Any): Additional keyword arguments to pass to the underlying `requests.request` function.

Returns:
    (requests.Response): The response object from the HTTP request.

Notes:
    - If 'progress' is set to True, the progress bar will display the download progress for responses with a known
      content length.
    - If 'progress' is a number then progress bar will display assuming content length = progress.
r   NprogressFstreamTzcontent-lengthBi   )totalunit
unit_scaleunit_divisor)
chunk_size)requestspoprequestint
isinstanceboolheadersgetr   iter_contentupdatelenclose
exceptionsChunkedEncodingError)	r   r   kwargsr#   r   responser   pbardatas	            r   requests_with_progressr5   @   s    " zz*e,H6v66CDCFCHZRV=W=W  $$%5q9]efE%cdN))T):DKKD	" ;

 O 33 Os   AC *C?>C?retrytimeoutthreadcodeverboser   c                    ^^^^^ Sm[        TS9UUUUU4S j5       n	X4n
XxS'   U(       a%  [        R                  " XUSS9R                  5         gU	" U
0 UD6$ )a  
Make an HTTP request using the 'requests' library, with exponential backoff retries up to a specified timeout.

Args:
    method (str): The HTTP method to use for the request. Choices are 'post' and 'get'.
    url (str): The URL to make the request to.
    retry (int, optional): Number of retries to attempt before giving up.
    timeout (int, optional): Timeout in seconds after which the function will give up retrying.
    thread (bool, optional): Whether to execute the request in a separate daemon thread.
    code (int, optional): An identifier for the request, used for logging purposes.
    verbose (bool, optional): A flag to determine whether to print out to console or not.
    progress (bool, optional): Whether to show a progress bar during the request.
    **kwargs (Any): Keyword arguments to be passed to the requests function specified in method.

Returns:
    (requests.Response | None): The HTTP response object. If the request is executed in a separate thread, returns
        None.
)i  i  )r:   c                   > Sn[         R                   " 5       n[        T	S-   5       GH*  n[         R                   " 5       U-
  T:  a    U$ [        X40 UD6nUR                  S:  a    U$  UR	                  5       R                  SS5      nUS:X  a  UR                  T
;   a  UT	(       a	  ST	 S	T S
3OS-  nO1UR                  S:X  a!  UR                  nSUS    SUS    SUS    S
3nT(       a6  [        R                  " [         U S[         SUR                   ST S3	5        UR                  T
;  a  Us  $ [         R                  " SU-  5        GM-     U$ ! [         a    Sn Nf = f)zNMake HTTP requests with retries and timeouts, with optional progress tracking.N   i,  messagezNo JSON message.zUnable to read JSON.r   z
 Retrying zx for zs. i  zRate limit reached (zX-RateLimit-Remaining/zX-RateLimit-Limitz). Please retry after zRetry-After z (z #)   )timeranger5   status_codejsonr*   AttributeErrorr)   r   warningPREFIXHELP_MSGsleep)func_methodfunc_urlfunc_kwargsrt0imhr9   r6   retry_codesr7   r:   s           r   funcsmart_request.<locals>.func   sy    YY[uqy!A		b G+, + '{LLA}}s"& %+FFHLL,>? Av==K/%:eWF7)2>ROA]]c)		A.q1H/I.J!ANaLbKc d../.>-?rC  NNfXaS(2amm_BtfTU#VW==3HJJq!t/ "0 ! " +*+s   * EE'&E'r   T)targetargsr1   daemonN)r   	threadingThreadstart)r   r   r6   r7   r8   r9   r:   r   r1   rV   rY   rU   s     `` ``    @r   smart_requestr^   b   se    : Kw   < ;D!:tLRRTT$V$$r   )      TTF)osr[   rD   typingr   ultralytics.utilsr   r   r   r   r   environr*   HUB_API_ROOTHUB_WEB_ROOTrJ   rK   strr   r5   r&   r(   r^    r   r   <module>rj      s    
     zz~~35RSzz~~35RS	%	&j&&# &&# &&R3 S J C%C%	C% C% 	C%
 C% C% C% C%r   