
    h&                     H    d dl Z d dlmZmZmZmZmZ ddlmZ  G d d      Z	y)    N)DictAnyOptionalUnionList   )UnauthorizedErrorc                   B   e Zd Zddedee   fdZdeeef   f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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Z	 	 	 d deee      deee      de	de
eeef   ej                  f   fdZd!dede	de
eeef   ej                  f   fdZy)"KeysManagementClientNbase_urlapi_keyc                 >    |j                  d      | _        || _        y)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   s      U/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/client/keys.py__init__zKeysManagementClient.__init__   s     "-    returnc                 J    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   headerss     r   _get_headersz!KeysManagementClient._get_headers   s0     "#56==)0'@GO$r   pagesizeuser_idteam_idorganization_idkey_hash	key_aliasreturn_full_objectinclude_team_keysreturn_requestc                    | j                    d}i }|||d<   |||d<   |||d<   |||d<   |||d<   |||d<   |||d	<   |t        |      j                         |d
<   |	t        |	      j                         |d<   t        j                  d|| j                         |      }|
r|S t        j                         }	 |j                  |j                               }|j                          |j                         S # t        j                  j                  $ r*}|j                  j                  dk(  rt        |       d}~ww xY 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sessionr5   es                    r   listzKeysManagementClient.list   sp   J  	*!# !F6N!F6N 'F9 'F9&(7F$%!)F: "+F;)+./A+B+H+H+JF'((*-.?*@*F*F*HF&'""5#t7H7H7JSYZN""$	||GOO$56H%%'==?""",, 	zz%%,'**	s   ?>C> >E%E  Emodelsaliasesspendduration	budget_idconfigc                 4   | j                    d}i }|||d<   |||d<   |||d<   |||d<   |||d<   |||d<   |||d	<   |||d
<   |	|	|d<   t        j                  d|| j                         |      }|
r|S t        j                         }	 |j                  |j                               }|j                          |j                         S # t        j                  j                  $ r*}|j                  j                  dk(  rt        |       d}~ww xY 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/generateNr<   r=   r>   r?   r"   r   r   r@   rA   POSTr   r2   r)   r   r,   r-   r   r.   r/   r0   r1   r2   r3   r4   r5   r6   r	   )r   r<   r=   r>   r?   r"   r   r   r@   rA   r%   r7   datar8   r9   r5   r:   s                    r   generatezKeysManagementClient.generatei   sI   L  .!#DN%DO!DM'D  )D%DO%DO  )D#DN""638I8I8KRVWN""$	||GOO$56H%%'==?""",, 	zz%%,'**	s   >C D-%DDkeyskey_aliasesc                    | j                    d}||d}t        j                  d|| j                         |      }|r|S t        j                         }	 |j                  |j                               }|j                          |j                         S # t        j                  j                  $ r*}	|	j                  j                  dk(  rt        |	       d}	~	ww xY 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)rH   rI   rC   rD   r)   NrE   )
r   rH   rI   r%   r7   rF   r8   r9   r5   r:   s
             r   deletezKeysManagementClient.delete   s    ,  , &

 ""638I8I8KRVWN""$	||GOO$56H%%'==?""",, 	zz%%,'**	s   >B C1%CCkeyc                    | j                    d| }t        j                  d|| j                               }|r|S t        j                         }	 |j                  |j                               }|j                          |j                         S # t        j                  j                  $ r*}|j                  j                  dk(  rt        |       d}~ww xY 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/keys/info?key=r'   )r   r)   NrE   )r   rL   r%   r7   r8   r9   r5   r:   s           r   infozKeysManagementClient.info   s      u5""5#t7H7H7JKN""$	||GOO$56H%%'==?""",, 	zz%%,'**	s   >B C-%CC)N)
NNNNNNNNNF)NNF)F)__name__
__module____qualname__r*   r   r   r   r   intboolr   r   r,   r-   r;   r   floatrG   rK   rN    r   r   r   r      s   	  	 x} 	 
d38n 
 #"!%!%)-"&#'-1,0$IsmI smI #	I
 #I "#I 3-I C=I %TNI $D>I I 
tCH~x///	0IZ '+,0!%"&#'!%!%#'+/$Ic#I $sCx.)I 	I
 3-I C=I #I #I C=I c3h(I I 
tCH~x///	0IZ %)+/$	*tCy!* d3i(* 	*
 
tCH~x///	0*X T eDcNT\TdTdDd>e r   r   )
r,   typingr   r   r   r   r   r3   r	   r   rU   r   r   <module>rW      s     3 3 )w wr   