U
    %FZhd4                      @   s  d Z ddlZddlmZ ddlmZmZ ddlZddl	Z	ddl
Z
ddlZddlmZ ejejejejejejejejejejejejejejejejejejejej ej!ej"ej#ej$ej%ej&ej'ej(ej)ej*ej+ej,iZ-ejejejejejejejej'ej)d	Z.ej!ej%ejej+ejejejejejejejej'ej)ej#ejdZ/dd	 Z0d
d Z1dd Z2d)ddZ3d*e
j4eedeej5 f  ej5dddZ6dd Z7dd Z8d+ddZ9e
j4ej5dddZ:d,e
j4ej5dddZ;dd  Z<d!d" Z=d#d$ Z>d%d& Z?G d'd( d(ej@jAjBZCdS )-z)Internal utilities common to all modules.    N)python_version)CallableOptional)
exceptions)	i  i  i  i  i  i  i  i  i  )                        	   
                  c                   C   s   dt   dtj S )Nz
gl-python/z fire-admin/)r   firebase_admin__version__ r   r   \/home/aprabhat/apps/x.techxrdev.in/venv/lib/python3.8/site-packages/firebase_admin/_utils.pyget_metrics_headerQ   s    r   c                 C   sR   | dkrt  S t| t jr<t | j}| |k	r8td| S tdt| dS )z3Returns a reference to an initialized App instance.NzKIllegal app argument. App instance not initialized via the firebase module.zTIllegal app argument. Argument must be of type  firebase_admin.App, but given "{0}".)r   Zget_app
isinstanceZAppname
ValueErrorformattype)appZinitialized_appr   r   r   _get_initialized_appT   s    r    c                 C   s   t | } | ||S N)r    Z_get_service)r   r   Zinitializerr   r   r   get_app_servicee   s    r"   c                 C   s^   | j dkrt| S | j }|j }|j}t||\}}d}|rJ|| ||}|rR|S t| ||S )ag  Constructs a ``FirebaseError`` from the given requests error.

    This can be used to handle errors returned by Google Cloud Platform (GCP) APIs.

    Args:
        error: An error raised by the requests module while making an HTTP call to a GCP API.
        handle_func: A function that can be used to handle platform errors in a custom way. When
            specified, this function will be called with three arguments. It has the same
            signature as ```_handle_func_requests``, but may return ``None``.

    Returns:
        FirebaseError: A ``FirebaseError`` that can be raised to the user code.
    N)responsehandle_requests_errorcontentdecodestatus_code_parse_platform_error_handle_func_requestserrorhandle_funcr#   r%   r'   
error_dictmessageexcr   r   r   #handle_platform_error_from_requestsj   s    

r0   .)r+   r,   returnc                 C   s`   t | tjrX| j}|j }|j}t||\}}d}|rD|| ||}|rL|S t| ||S t	| S )a^  Constructs a ``FirebaseError`` from the given httpx error.

    This can be used to handle errors returned by Google Cloud Platform (GCP) APIs.

    Args:
        error: An error raised by the httpx module while making an HTTP call to a GCP API.
        handle_func: A function that can be used to handle platform errors in a custom way. When
            specified, this function will be called with three arguments. It has the same
            signature as ```_handle_func_httpx``, but may return ``None``.

    Returns:
        FirebaseError: A ``FirebaseError`` that can be raised to the user code.
    N)
r   httpxHTTPStatusErrorr#   r%   r&   r'   r(   _handle_func_httpxhandle_httpx_errorr*   r   r   r    handle_platform_error_from_httpx   s    
r6   c                 C   sL   t | tstjd| | dS | d}| d}t|}t|}||dS )zConstructs a ``FirebaseError`` from the given operation error.

    Args:
        error: An error returned by a long running operation.

    Returns:
        FirebaseError: A ``FirebaseError`` that can be raised to the user code.
    5Unknown error while making a remote service call: {0}r.   causecoder.   )r.   )r   dictr   UnknownErrorr   get_rpc_code_to_error_code_error_code_to_exception_type)r+   rpc_coder.   Z
error_codeerr_typer   r   r   handle_operation_error   s    	


rB   c                 C   s   | d}t| ||S )a|  Constructs a ``FirebaseError`` from the given GCP error.

    Args:
        error: An error raised by the requests module while making an HTTP call.
        message: A message to be included in the resulting ``FirebaseError``.
        error_dict: Parsed GCP error response.

    Returns:
        FirebaseError: A ``FirebaseError`` that can be raised to the user code or None.
    status)r=   r$   r+   r.   r-   r:   r   r   r   r)      s    
r)   c                 C   s   t | tjjr"tjd| | dS t | tjjrDtjd| | dS | jdkrbtj	d| | dS |srt
| jj}|s~t| }t|}||| | jdS )ao  Constructs a ``FirebaseError`` from the given requests error.

    This method is agnostic of the remote service that produced the error, whether it is a GCP
    service or otherwise. Therefore, this method does not attempt to parse the error response in
    any way.

    Args:
        error: An error raised by the requests module while making an HTTP call.
        message: A message to be included in the resulting ``FirebaseError`` (optional). If not
            specified the string representation of the ``error`` argument is used as the message.
        code: A GCP error code that will be used to determine the resulting error type (optional).
            If not specified the HTTP status code on the error response is used to determine a
            suitable error code.

    Returns:
        FirebaseError: A ``FirebaseError`` that can be raised to the user code.
    'Timed out while making an API call: {0}r8   %Failed to establish a connection: {0}Nr7   r.   r9   http_response)r   requestsr   TimeoutDeadlineExceededErrorr   ConnectionErrorUnavailableErrorr#   r<   _http_status_to_error_coder'   strr?   r+   r.   r:   rA   r   r   r   r$      s*    
r$   )r+   r1   c                 C   s   | d}t| ||S )ay  Constructs a ``FirebaseError`` from the given GCP error.

    Args:
        error: An error raised by the httpx module while making an HTTP call.
        message: A message to be included in the resulting ``FirebaseError``.
        error_dict: Parsed GCP error response.

    Returns:
        FirebaseError: A ``FirebaseError`` that can be raised to the user code or None.
    rC   )r=   r5   rD   r   r   r   r4      s    
r4   c                 C   s   t | tjr tjd| | dS t | tjr@tjd| | dS t | tjrt	d|  |sft
| jj}|srt| }t|}||| | jdS tjd| | dS )ai  Constructs a ``FirebaseError`` from the given httpx error.

    This method is agnostic of the remote service that produced the error, whether it is a GCP
    service or otherwise. Therefore, this method does not attempt to parse the error response in
    any way.

    Args:
        error: An error raised by the httpx module while making an HTTP call.
        message: A message to be included in the resulting ``FirebaseError`` (optional). If not
            specified the string representation of the ``error`` argument is used as the message.
        code: A GCP error code that will be used to determine the resulting error type (optional).
            If not specified the HTTP status code on the error response is used to determine a
            suitable error code.

    Returns:
        FirebaseError: A ``FirebaseError`` that can be raised to the user code.
    rE   r8   rF   zprinting status errorrG   r7   )r   r2   ZTimeoutExceptionr   rK   r   ZConnectErrorrM   r3   printrN   r#   r'   rO   r?   r<   rP   r   r   r   r5      s,    
r5   c                 C   s   t | tjS )z-Maps an HTTP status to a platform error code.)_HTTP_STATUS_TO_ERROR_CODEr=   r   UNKNOWN)rC   r   r   r   rN   &  s    rN   c                 C   s   t | tjS )z*Maps an RPC code to a platform error code.)_RPC_CODE_TO_ERROR_CODEr=   r   rS   )r@   r   r   r   r>   *  s    r>   c                 C   s   t | tjS )z0Maps a platform error code to an exception type.)_ERROR_CODE_TO_EXCEPTION_TYPEr=   r   r<   )r:   r   r   r   r?   .  s    r?   c                 C   sd   i }zt | }t|tr|}W n tk
r4   Y nX |di }|d}|s\d|| }||fS )a(  Parses an HTTP error response from a Google Cloud Platform API and extracts the error code
    and message fields.

    Args:
        content: Decoded content of the response body.
        status_code: HTTP status code.

    Returns:
        tuple: A tuple containing error code and message.
    r+   r.   z4Unexpected HTTP response with status: {0}; body: {1})jsonloadsr   r;   r   r=   r   )r%   r'   dataZparsed_bodyr-   msgr   r   r   r(   3  s    


r(   c                   @   s    e Zd ZdZdd Zdd ZdS )EmulatorAdminCredentialsz Credentials for use with the firebase local emulator.

    This is used instead of user-supplied credentials or ADC.  It will silently do nothing when
    asked to refresh credentials.
    c                 C   s   t jjj|  d| _d S )Nowner)googleauthcredentialsCredentials__init__token)selfr   r   r   r`   V  s    z!EmulatorAdminCredentials.__init__c                 C   s   d S r!   r   )rb   requestr   r   r   refreshZ  s    z EmulatorAdminCredentials.refreshN)__name__
__module____qualname____doc__r`   rd   r   r   r   r   rZ   P  s   rZ   )N)N)NN)NN)Drh   rV   platformr   typingr   r   Zgoogle.authr\   rI   r2   r   r   ZINVALID_ARGUMENTZInvalidArgumentErrorZFAILED_PRECONDITIONZFailedPreconditionErrorZOUT_OF_RANGEZOutOfRangeErrorZUNAUTHENTICATEDZUnauthenticatedErrorZPERMISSION_DENIEDZPermissionDeniedError	NOT_FOUNDZNotFoundErrorZABORTEDZAbortedErrorZALREADY_EXISTSZAlreadyExistsErrorCONFLICTZConflictErrorZRESOURCE_EXHAUSTEDZResourceExhaustedErrorZ	CANCELLEDZCancelledErrorZ	DATA_LOSSZDataLossErrorrS   r<   ZINTERNALZInternalErrorUNAVAILABLErM   ZDEADLINE_EXCEEDEDrK   rU   rR   rT   r   r    r"   r0   	HTTPErrorZFirebaseErrorr6   rB   r)   r$   r4   r5   rN   r>   r?   r(   r]   r^   r_   rZ   r   r   r   r   <module>   s                   
 
'(