o
    ×èFhø$  ã                   @   s®  d 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„Z,dd„ Z-dd„ Z.ddd„Z/dd„ Z0dd„ Z1dd„ Z2dd„ Z3G dd„ dej4j5j6ƒZ7d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   úN/var/www/html/loop/nvenv/lib/python3.10/site-packages/firebase_admin/_utils.pyÚ_get_initialized_appO   s   
ÿr   c                 C   s   t | ƒ} |  ||¡S ©N)r   Ú_get_service)r   r   Úinitializerr   r   r   Úget_app_service`   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_requestse   s   

r/   c                 C   sL   t | tƒstjd | ¡| d�S |  d¡}|  d¡}t|ƒ}t|ƒ}||d�S )zêConstructs 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.
    z'Timed out while making an API call: {0}r1   z%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   ó   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   rD   Í   ó   rD   c                 C   rF   )z*Maps an RPC code to a platform error code.)Ú_RPC_CODE_TO_ERROR_CODEr6   r   rH   )r9   r   r   r   r7   Ñ   rI   r7   c                 C   rF   )z0Maps a platform error code to an exception type.)Ú_ERROR_CODE_TO_EXCEPTION_TYPEr6   r   r5   )r3   r   r   r   r8   Õ   rI   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   rW   ý   s   
z!EmulatorAdminCredentials.__init__c                 C   s   d S r   r   )rY   Úrequestr   r   r   Úrefresh  s   z EmulatorAdminCredentials.refreshN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rW   r[   r   r   r   r   rQ   ÷   s    rQ   r   )NN)8r_   rL   Úgoogle.authrS   r?   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ÚDataLossErrorrH   r5   ÚINTERNALÚInternalErrorÚUNAVAILABLErC   ÚDEADLINE_EXCEEDEDrA   rK   rG   rJ   r   r"   r/   r<   r)   r$   rD   r7   r8   r(   rT   rU   rV   rQ   r   r   r   r   Ú<module>   s˜   ÿþýüûúùø	÷
öõôóòñð÷ñ

(