o
    tBhx6                     @   s  d Z ddlZddlZddlZddl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 Z.dd	 Z/d$d
dZ0dd Z1dd Z2d%ddZ3d$ddZ4dd Z5d&ddZ6dd Z7dd Z8dd Z9dd Z:d d! Z;G d"d# d#ej<j=j>Z?dS )'z)Internal utilities common to all modules.    N)
exceptions)	i  i  i  i  i  i  i  i  i  )                        	   
                  c                 C   sN   | du rt  S t| t jrt | j}| |urtd| S tdt| )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}".)firebase_adminget_app
isinstanceAppname
ValueErrorformattype)appinitialized_app r   l/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/firebase_admin/_utils.py_get_initialized_appS   s   
r   c                 C   s   t | } | ||S N)r   _get_service)r   r   initializerr   r   r   get_app_serviced   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_requestsi   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.
    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-   
error_codeerr_typer   r   r   handle_operation_error   s   
	


r<   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)r6   r$   )r*   r-   r,   r3   r   r   r   r)      s   
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: {0}r1   %Failed to establish a connection: {0}Nr0   r-   r2   http_response)r   requestsr   TimeoutDeadlineExceededErrorr   ConnectionErrorUnavailableErrorr#   r5   _http_status_to_error_coder'   strr8   )r*   r-   r3   r;   r   r   r   r$      s*   
r$   c                 C   sj   t | tjjst| S | j }| jj}t	||\}}t
| }d}|r*|| |||}|r.|S t| |||S )au  Constructs a ``FirebaseError`` from the given googleapiclient error.

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

    Args:
        error: An error raised by the googleapiclient 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_googleapiclient``, but may return ``None``.

    Returns:
        FirebaseError: A ``FirebaseError`` that can be raised to the user code.
    N)r   googleapiclienterrors	HttpErrorhandle_googleapiclient_errorr%   r&   respr=   r(   )_http_response_from_googleapiclient_error_handle_func_googleapiclient)r*   r+   r%   r'   r,   r-   rA   r.   r   r   r   *handle_platform_error_from_googleapiclient   s   
rP   c                 C   s   | d}t| |||S )a  Constructs a ``FirebaseError`` from the given GCP error.

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

    Returns:
        FirebaseError: A ``FirebaseError`` that can be raised to the user code or None.
    r=   )r6   rL   )r*   r-   r,   rA   r3   r   r   r   rO      s   
rO   c                 C   s   t | tjst | tjrdt| v rtjd| | dS t | tj	r,tj
d| | dS t | tjjs=tjd| | dS |sEt| jj}|sKt| }|sQt| }t|}||| |dS )a&  Constructs a ``FirebaseError`` from the given googleapiclient 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 googleapiclient 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.
        http_response: A requests HTTP response object to associate with the exception (optional).
            If not specified, one will be created from the ``error``.

    Returns:
        FirebaseError: A ``FirebaseError`` that can be raised to the user code.
    z	timed outr>   r1   r?   r0   r@   )r   sockettimeoutr*   rH   r   rD   r   httplib2ServerNotFoundErrorrF   rI   rJ   rK   r5   rG   rM   r=   rN   r8   )r*   r-   r3   rA   r;   r   r   r   rL      s4   
rL   c                 C   s&   t j }t| j|_| jj|_	|S )zMCreates a requests HTTP Response object from the given googleapiclient error.)
rB   modelsResponseioBytesIOr%   rawrM   r=   r'   )r*   rM   r   r   r   rN   *  s   

rN   c                 C      t | tjS )z-Maps an HTTP status to a platform error code.)_HTTP_STATUS_TO_ERROR_CODEr6   r   UNKNOWN)r=   r   r   r   rG   2     rG   c                 C   rZ   )z*Maps an RPC code to a platform error code.)_RPC_CODE_TO_ERROR_CODEr6   r   r\   )r9   r   r   r   r7   6  r]   r7   c                 C   rZ   )z0Maps a platform error code to an exception type.)_ERROR_CODE_TO_EXCEPTION_TYPEr6   r   r5   )r3   r   r   r   r8   :  r]   r8   c                 C   sb   i }zt | }t|tr|}W n	 ty   Y nw |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   r4   r   r6   r   )r%   r'   dataparsed_bodyr,   msgr   r   r   r(   ?  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   rk   b  s   
z!EmulatorAdminCredentials.__init__c                 C   s   d S r   r   )rm   requestr   r   r   refreshf  s   z EmulatorAdminCredentials.refreshN)__name__
__module____qualname____doc__rk   ro   r   r   r   r   re   \  s    re   r   )NN)NNN)@rs   rW   r`   rQ   google.authrg   rI   rS   rB   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\   r5   INTERNALInternalErrorUNAVAILABLErF   DEADLINE_EXCEEDEDrD   r_   r[   r^   r   r"   r/   r<   r)   r$   rP   rO   rL   rN   rG   r7   r8   r(   rh   ri   rj   re   r   r   r   r   <module>   s   	



(
-