
    g                       d dl mZ d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlZd dlmZ dd	lmZ  ej"                         d
z  Z	 edz  Z	  G d de	      Z G d de	      Z G d de	      ZddZddZddZy)    )annotationsN)datetimetimezone)Path)	TypedDict)urljoin)LogfireConfigError   )UnexpectedResponsez.logfirezdefault.tomlc                  &    e Zd ZU dZded<   ded<   y)UserTokenDatazUser token data.strtoken
expirationN__name__
__module____qualname____doc____annotations__     K/var/www/openai/venv/lib/python3.12/site-packages/logfire/_internal/auth.pyr   r      s    JOr   r   c                      e Zd ZU dZded<   y)DefaultFilez!Content of the default.toml file.zdict[str, UserTokenData]tokensNr   r   r   r   r   r      s    +$$r   r   c                  &    e Zd ZU dZded<   ded<   y)NewDeviceFlowz.Matches model of the same name in the backend.r   device_codefrontend_auth_urlNr   r   r   r   r   r   #   s    8r   r   c                    t        j                         d   }t        |d      }	 | j                  |d|i      }t	        j
                  |       |j                         }|d   |d   fS # t        j                  $ r}t        d      |d}~ww xY w)	aV  Request a device code from the Logfire API.

    Args:
        session: The `requests` session to use.
        base_api_url: The base URL of the Logfire instance.

    Returns:
    return data['device_code'], data['frontend_auth_url']
        The device code and the frontend URL to authenticate the device at, as a `NewDeviceFlow` dict.
    r
   z/v1/device-auth/new/machine_name)paramsz Failed to request a device code.Nr   r    )
platformunamer   postr   raise_for_statusrequestsRequestExceptionr	   json)sessionbase_api_urlr"   device_auth_endpointresedatas          r   request_device_coder1   *   s     >>#A&L"<1GHLll/8VlW++C0 ((*D%8 999 $$ L !CD!KLs   *A) )B<BBc                8   t        |d|       }d}	 	 | j                  |d      }t        j                  |       |j	                         }|r|S ># t
        j                  $ r5}|dz  }|dk\  rt        d      |t        j                  d       Y d	}~Dd	}~ww xY w)
a  Poll the Logfire API for the user token.

    This function will keep polling the API until it receives a user token, not that
    each request should take ~10 seconds as the API endpoint will block waiting for the user to
    complete authentication.

    Args:
        session: The `requests` session to use.
        device_code: The device code to poll for.
        base_api_url: The base URL of the Logfire instance.

    Returns:
        The user token.
    z/v1/device-auth/wait/r      )timeoutr
      zFailed to poll for token.z%Failed to poll for token. Retrying...N)
r   getr   r'   r*   r(   r)   r	   warningswarn)r+   r   r,   auth_endpointerrorsr.   opt_user_tokenr/   s           r   poll_for_tokenr<   @   s     L,A+*OPMF
	&++mR+8C//4 4788:N%%  (( 	CaKF{()DE1LMMABB		Cs   (A B$+BBc                "   | d   j                         D ]y  \  }}t        j                  |d   j                  d            j	                  t
        j                        }||k(  sQt        j                  t
        j                        |k  sy y y)zjCheck if the user is logged in.

    Returns:
        True if the user is logged in, False otherwise.
    r   r   Z)tzinfo)tzTF)itemsr   fromisoformatrstripreplacer   utcnow)r0   logfire_urlurlinfoexpiry_dates        r   is_logged_inrK   `   sx     (^))+	T,,T,-?-F-Fs-KLTT\d\h\hTi+(,,(,,"?+"M	 ,
 r   )r+   requests.Sessionr,   r   returnztuple[str, str])r+   rL   r   r   r,   r   rM   r   )r0   r   rG   r   rM   bool)
__future__r   r$   r7   r   r   pathlibr   typingr   urllib.parser   r(   logfire.exceptionsr	   utilsr   homeHOME_LOGFIREDEFAULT_FILEr   r   r   r1   r<   rK   r   r   r   <module>rX      sy    "   '      1 %tyy{Z' An, %I %) %I :,&@r   