o
     i4                     @   s8  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 i 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,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Z3	d*de
j4deedeej5 f  dej5fddZ6dd Z7dd Z8d+ddZ9de
j4dej5fddZ:d+de
j4dej5f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   u/var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/env/lib/python3.10/site-packages/firebase_admin/_utils.pyget_metrics_headerQ   s   r   c                 C   sP   | du rt  S t| t jrt | j}| |urtd| S tdt|  d)z3Returns a reference to an initialized App instance.NzKIllegal app argument. App instance not initialized via the firebase module.zNIllegal app argument. Argument must be of type firebase_admin.App, but given "z".)r   get_app
isinstanceAppname
ValueErrortype)appinitialized_appr   r   r   _get_initialized_appT   s   r"   c                 C   s   t | } | ||S N)r"   _get_service)r    r   initializerr   r   r   get_app_servicef   s   r&   c                 C   s^   | j du r	t| S | j }|j }|j}t||\}}d}|r%|| ||}|r)|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_requestsk   s   

r4   r/   r0   .returnc                 C   s`   t | tjr,| j}|j }|j}t||\}}d}|r"|| ||}|r&|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   
r:   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.
    2Unknown error while making a remote service call: r2   causecoder2   )r2   )r   dictr   UnknownErrorget_rpc_code_to_error_code_error_code_to_exception_type)r/   rpc_coder2   
error_codeerr_typer   r   r   handle_operation_error   s   
	


rG   c                 C      | 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)rA   r(   r/   r2   r1   r>   r   r   r   r-         
r-   c                 C   s   t | tjjrtjd|  | dS t | tjjr"tjd|  | dS | jdu r1tjd|  | dS |s9t	| 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: r<   "Failed to establish a connection: Nr;   r2   r=   http_response)r   requestsr   TimeoutDeadlineExceededErrorConnectionErrorUnavailableErrorr'   r@   _http_status_to_error_coder+   strrC   r/   r2   r>   rF   r   r   r   r(      s*   
r(   c                 C   rH   )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.
    rI   )rA   r9   rJ   r   r   r   r8      rK   r8   c                 C   s   t | tjrtjd|  | dS t | tjr tjd|  | dS t | tjrEtd|  |s3t	| j
j}|s9t| }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.
    rL   r<   rM   zprinting status errorrN   r;   )r   r6   TimeoutExceptionr   rR   ConnectErrorrT   r7   printrU   r'   r+   rV   rC   r@   rW   r   r   r   r9      s,   
r9   c                 C      t | tjS )z-Maps an HTTP status to a platform error code.)_HTTP_STATUS_TO_ERROR_CODErA   r   UNKNOWN)rI   r   r   r   rU   '     rU   c                 C   r[   )z*Maps an RPC code to a platform error code.)_RPC_CODE_TO_ERROR_CODErA   r   r]   )rD   r   r   r   rB   +  r^   rB   c                 C   r[   )z0Maps a platform error code to an exception type.)_ERROR_CODE_TO_EXCEPTION_TYPErA   r   r@   )r>   r   r   r   rC   /  r^   rC   c                 C   sf   i }zt | }t|tr|}W n	 ty   Y nw |di }|d}|s/d| 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/   r2   z&Unexpected HTTP response with status: z; body: )jsonloadsr   r?   r   rA   )r)   r+   dataparsed_bodyr1   msgr   r   r   r,   4  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   rl   W  s   
z!EmulatorAdminCredentials.__init__c                 C   s   d S r#   r   )rn   requestr   r   r   refresh[  s   z EmulatorAdminCredentials.refreshN)__name__
__module____qualname____doc__rl   rp   r   r   r   r   rf   Q  s    rf   r#   )NN)Drt   ra   platformr   typingr   r   google.authrh   rP   r6   r   r   INVALID_ARGUMENTInvalidArgumentErrorFAILED_PRECONDITIONFailedPreconditionErrorOUT_OF_RANGEOutOfRangeErrorUNAUTHENTICATEDUnauthenticatedErrorPERMISSION_DENIEDPermissionDeniedError	NOT_FOUNDNotFoundErrorABORTEDAbortedErrorALREADY_EXISTSAlreadyExistsErrorCONFLICTConflictErrorRESOURCE_EXHAUSTEDResourceExhaustedError	CANCELLEDCancelledError	DATA_LOSSDataLossErrorr]   r@   INTERNALInternalErrorUNAVAILABLErT   DEADLINE_EXCEEDEDrR   r`   r\   r_   r   r"   r&   r4   	HTTPErrorFirebaseErrorr:   rG   r-   r(   r8   r9   rU   rB   rC   r,   ri   rj   rk   rf   r   r   r   r   <module>   s   	



'(