o
    ig                     @  s   d dl mZ d dlmZ d dlm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mZmZ dd
lmZ de ZG dd deZG dd deZG dd dZdS )    )annotations)Any)urljoin)ResponseSession)Self)LogfireConfigError)VERSION   )	UserTokenUserTokenCollection)UnexpectedResponsezlogfire/c                   @  s   e Zd ZdS )ProjectAlreadyExistsN)__name__
__module____qualname__ r   r   ^/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/logfire/_internal/client.pyr      s    r   c                   @  s   e Zd ZdddZdS )	InvalidProjectNamereasonstrreturnNonec                C  s
   || _ d S N)r   )selfr   r   r   r   __init__   s   
zInvalidProjectName.__init__N)r   r   r   r   )r   r   r   r   r   r   r   r   r      s    r   c                   @  s   e Zd ZdZd4ddZed5ddZd6d7ddZddd8ddZd6d9ddZ	ddd:dd Z
d;d"d#Zd<d%d&Zd;d'd(Zd=d+d,Zd>d-d.Zd>d/d0Zd?d2d3ZdS )@LogfireClientzA Logfire HTTP client to interact with the API.

    Args:
        user_token: The user token to use when authenticating against the API.
    
user_tokenr   r   r   c                 C  s@   |j rtd|j| _|j| _t | _| jj| jt	d d S )Nz"The provided user token is expired)Authorizationz
User-Agent)

is_expiredRuntimeErrorbase_urltoken_tokenr   _sessionheadersupdate	UA_HEADER)r   r   r   r   r   r   "   s   zLogfireClient.__init__r!   
str | Noner   c                 C  s   | t  |dS )a  Create a client from the provided base URL.

        Args:
            base_url: The base URL to use when looking for a user token. If `None`, will prompt
                the user into selecting a token from the token collection (or, if only one available,
                use it directly). The token collection will be created from the `~/.logfire/default.toml`
                file (or an empty one if no such file exists).
        )r   )r   	get_token)clsr!   r   r   r   from_url*   s   
zLogfireClient.from_urlNendpointr   paramsdict[str, Any] | Noner   c                 C  &   | j jt| j||d}t| |S )Nr-   )r$   getr   r!   r   raise_for_status)r   r,   r-   responser   r   r   _get_raw6      
zLogfireClient._get_rawr0   error_messager   c             
   C  6   z	|  || W S  ty } zt||d }~ww r   )r4   jsonr   r   )r   r,   r-   r6   er   r   r   _get;      
zLogfireClient._getbody
Any | Nonec                 C  r/   )N)r8   )r$   postr   r!   r   r2   )r   r,   r<   r3   r   r   r   	_post_rawA   r5   zLogfireClient._post_rawr<   c             
   C  r7   r   )r?   r8   r   r   )r   r,   r<   r6   r9   r   r   r   _postF   r;   zLogfireClient._postlist[dict[str, Any]]c                 C     | j dddS )z,Get the organizations of the logged-in user.z)/v1/organizations/available-for-projects/z&Error retrieving list of organizationsr6   r:   r   r   r   r   get_user_organizationsL   s   z$LogfireClient.get_user_organizationsdict[str, Any]c                 C  rC   )z)Get information about the logged-in user.z/v1/account/mez!Error retrieving user informationrD   rE   rF   r   r   r   get_user_informationR      z"LogfireClient.get_user_informationc                 C  rC   )z'Get the projects of the logged-in user.z/v1/writable-projects/z!Error retrieving list of projectsrD   rE   rF   r   r   r   get_user_projectsV   rJ   zLogfireClient.get_user_projectsorganizationproject_namec              
   C  s   z| j d| dd|id}W | S  tyG } z)|j}|jdkr$t|jdkr?| d d }|d	 d
dgkr?t|d tdd}~ww )zCreate a new project.

        Args:
            organization: The organization that should hold the new project.
            project_name: The name of the project to be created.

        Returns:
            The newly created project.
        /v1/organizations/z	/projectsrM   r@   i  i  detailr   locr<   msgzError creating new projectN)r?   r   r3   status_coder   r8   r   r   )r   rL   rM   r3   r9   rerrorr   r   r   create_new_projectZ   s   


z LogfireClient.create_new_projectc                 C  s   | j d| d| dddS )zECreate a write token for the given project in the given organization.rN   
/projects/z/write-tokens/z"Error creating project write tokenrD   rA   r   rL   rM   r   r   r   create_write_tokenr   s   z LogfireClient.create_write_tokenc                 C  s"   | j d| d| dddiddS )zDCreate a read token for the given project in the given organization.rN   rV   z/read-tokensdescriptionzCreated by Logfire CLIz!Error creating project read token)r<   r6   rW   rX   r   r   r   create_read_tokeny   
   zLogfireClient.create_read_tokenissuec                 C  s"   | j d| d| dd|iddS )z/Get a prompt to be used with your favorite LLM.rN   rV   z/promptsr]   zError retrieving prompt)r-   r6   rE   )r   rL   rM   r]   r   r   r   
get_prompt   r\   zLogfireClient.get_prompt)r   r   r   r   )r!   r(   r   r   r   )r,   r   r-   r.   r   r   )r,   r   r-   r.   r6   r   r   r   )r,   r   r<   r=   r   r   )r,   r   r<   r=   r6   r   r   r   )r   rB   )r   rH   )rL   r   rM   r   )rL   r   rM   r   r   rH   )rL   r   rM   r   r]   r   r   rH   )r   r   r   __doc__r   classmethodr+   r4   r:   r?   rA   rG   rI   rK   rU   rY   r[   r^   r   r   r   r   r      s     






r   N)
__future__r   typingr   urllib.parser   requestsr   r   typing_extensionsr   logfire.exceptionsr   logfire.versionr	   authr   r   utilsr   r'   	Exceptionr   r   r   r   r   r   r   <module>   s    
