o
    ưi#/                     @   sB   d dl mZmZmZmZmZ d dlZddlmZ G dd dZ	dS )    )AnyDictListOptionalUnionN   )UnauthorizedErrorc                   @   s8  e Zd Zd'dedee fddZdeeef fddZ											d(d
ee dee dee dee dee dee dee dee	 dee	 de	de
eeef ejf fddZ											d(deee  deeeef  dee dee dee dee dee dee deeeef  de	de
eeef ejf fddZ				d)deee  deee  de	de
eeef ejf fd d!Z							d*d"edeee  deeeef  dee dee dee dee dee de
eeef ejf fd#d$Zd+d"ede	de
eeef ejf fd%d&ZdS ),KeysManagementClientNbase_urlapi_keyc                 C   s   | d| _|| _dS )a  
        Initialize the KeysManagementClient.

        Args:
            base_url (str): The base URL of the LiteLLM proxy server (e.g., "http://localhost:8000")
            api_key (Optional[str]): API key for authentication. If provided, it will be sent as a Bearer token.
        /N)rstrip	_base_url_api_key)selfr
   r    r   P/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/client/keys.py__init__	   s   
zKeysManagementClient.__init__returnc                 C   s"   ddi}| j rd| j  |d< |S )z
        Get the headers for API requests, including authorization if api_key is set.

        Returns:
            Dict[str, str]: Headers to use for API requests
        zContent-Typezapplication/jsonzBearer Authorization)r   )r   headersr   r   r   _get_headers   s   z!KeysManagementClient._get_headersFpagesizeuser_idteam_idorganization_idkey_hash	key_aliasreturn_full_objectinclude_team_keysreturn_requestc              
   C   s*  | j  d}i }|dur||d< |dur||d< |dur ||d< |dur(||d< |dur0||d< |dur8||d< |dur@||d	< |durLt| |d
< |	durXt|	 |d< tjd||  |d}|
rg|S t }z|| }|	  |
 W S  tjjy } z|jjdkrt| d}~ww )a  
        List all API keys with optional filtering and pagination.

        Args:
            page (Optional[int]): Page number for pagination
            size (Optional[int]): Number of items per page
            user_id (Optional[str]): Filter keys by user ID
            team_id (Optional[str]): Filter keys by team ID
            organization_id (Optional[str]): Filter keys by organization ID
            key_hash (Optional[str]): Filter by specific key hash
            key_alias (Optional[str]): Filter by key alias
            return_full_object (Optional[bool]): Whether to return the full key object
            include_team_keys (Optional[bool]): Whether to include team keys in the response
            return_request (bool): If True, returns the prepared request object instead of executing it

        Returns:
            Union[Dict[str, Any], requests.Request]: Either the response from the server or
            a prepared request object if return_request is True. The response contains a list
            of API keys with their configurations.

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            requests.exceptions.RequestException: If the request fails with any other error
        z	/key/listNr   r   r   r   r   r   r   r   r    GET)r   params  )r   strlowerrequestsRequestr   Sessionsendprepareraise_for_statusjson
exceptions	HTTPErrorresponsestatus_coder   )r   r   r   r   r   r   r   r   r   r    r!   urlr#   requestsessionr0   er   r   r   list    sD   %
zKeysManagementClient.listmodelsaliasesspendduration	budget_idconfigc              
   C   s  | j  d}i }|dur||d< |dur||d< |dur ||d< |dur(||d< |dur0||d< |dur8||d< |dur@||d	< |durH||d
< |	durP|	|d< tjd||  |d}|
r_|S t }z|| }|  | W S  tj	j
y } z|jjdkrt| d}~ww )a  
        Generate an API key based on the provided data.

        Docs: https://docs.litellm.ai/docs/proxy/virtual_keys

        Args:
            models (Optional[List[str]]): List of allowed models for this key
            aliases (Optional[Dict[str, str]]): Model alias mappings
            spend (Optional[float]): Maximum spend limit for this key
            duration (Optional[str]): Duration for which the key is valid (e.g. "24h", "7d")
            key_alias (Optional[str]): Alias/name for the key for easier identification
            team_id (Optional[str]): Team ID to associate the key with
            user_id (Optional[str]): User ID to associate the key with
            budget_id (Optional[str]): Budget ID to associate the key with
            config (Optional[Dict[str, Any]]): Additional configuration parameters
            return_request (bool): If True, returns the prepared request object instead of executing it

        Returns:
            Union[Dict[str, Any], requests.Request]: Either the response from the server or
            a prepared request object if return_request is True

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            requests.exceptions.RequestException: If the request fails with any other error
        z/key/generateNr7   r8   r9   r:   r   r   r   r;   r<   POSTr   r-   r$   r   r'   r(   r   r)   r*   r+   r,   r-   r.   r/   r0   r1   r   )r   r7   r8   r9   r:   r   r   r   r;   r<   r!   r2   datar3   r4   r0   r5   r   r   r   generatek   sD   &
zKeysManagementClient.generatekeyskey_aliasesc           
   
   C   s   | j  d}||d}tjd||  |d}|r|S t }z|| }|  | W S  tj	j
yG }	 z|	jjdkrBt|	 d}	~	ww )a  
        Delete existing keys

        Args:
            keys (List[str]): List of API keys to delete
            key_aliases (List[str]): List of key aliases to delete
            return_request (bool): If True, returns the prepared request object instead of executing it

        Returns:
            Union[Dict[str, Any], requests.Request]: Either the response from the server or
            a prepared request object if return_request is True

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            requests.exceptions.RequestException: If the request fails with any other error
        z/key/delete)rB   rC   r=   r>   r$   Nr?   )
r   rB   rC   r!   r2   r@   r3   r4   r0   r5   r   r   r   delete   s$   
zKeysManagementClient.deletekeyc	                 C   s   | j  d}	d|i}
|dur||
d< |dur||
d< |dur"||
d< |dur*||
d< |dur2||
d< |dur:||
d	< |durB||
d
< tjd|	|  |
d}t }d}z|| }|j}|  |	 W S  t
yt   t
d| w )a  
        Update an existing API key's parameters.

        Args:
            models: Optional[List[str]] = None,
            aliases: Optional[Dict[str, str]] = None,
            spend: Optional[float] = None,
            duration: Optional[str] = None,
            key_alias: Optional[str] = None,
            team_id: Optional[str] = None,
            user_id: Optional[str] = None,

        Returns:
            Union[Dict[str, Any], requests.Request]: Either the response from the server or
            a prepared request object if return_request is True

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            requests.exceptions.RequestException: If the request fails with any other error
        z/key/updaterE   Nr   r   r   r7   r9   r:   r8   r=   r>   zError updating key: )r   r'   r(   r   r)   r*   r+   textr,   r-   	Exception)r   rE   r7   r8   r9   r:   r   r   r   r2   r@   r3   r4   Zresponse_textr0   r   r   r   update   s6   
zKeysManagementClient.updatec              
   C   s   | j  d| }tjd||  d}|r|S t }z|| }|  | W S  tj	j
yC } z|jjdkr>t| d}~ww )a?  
        Get information about API keys.

        Args:
            key (str): The key hash to get information about
            return_request (bool): If True, returns the prepared request object instead of executing it

        Returns:
            Union[Dict[str, Any], requests.Request]: Either the response from the server or a prepared request object if return_request is True

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            requests.exceptions.RequestException: If the request fails with any other error
        z/key/info?key=r"   )r   r$   Nr?   )r   rE   r!   r2   r3   r4   r0   r5   r   r   r   info  s   
zKeysManagementClient.info)N)
NNNNNNNNNF)NNF)NNNNNNN)F)__name__
__module____qualname__r%   r   r   r   r   intboolr   r   r'   r(   r6   r   floatrA   rD   rH   rI   r   r   r   r   r	      s    	

M
	

M


/
	

.=r	   )
typingr   r   r   r   r   r'   r.   r   r	   r   r   r   r   <module>   s    