o
    0=jÝ3  ã                   @   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   ú_C:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\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   Zget_appÚ
isinstanceZAppÚnameÚ
ValueErrorÚ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_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   

r/   r*   r+   .Ú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   
r5   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.
    ú2Unknown error while making a remote service call: ©r-   ÚcauseÚcoder-   )r-   N)r   Údictr   ÚUnknownErrorÚ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   
	þ


rA   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.
    ÚstatusN)r<   r#   ©r*   r-   r,   r9   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: r7   ú"Failed to establish a connection: Nr6   ©r-   r8   Úhttp_response)r   Úrequestsr   ÚTimeoutÚDeadlineExceededErrorÚConnectionErrorÚUnavailableErrorr"   r;   Ú_http_status_to_error_coder&   Ústrr>   ©r*   r-   r9   r@   r   r   r   r#   É   s*   þþ
þr#   c                 C   rB   )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   N)r<   r4   rD   r   r   r   r3   ð   rE   r3   c                 C   s”   t | tjƒrtjd| › �| d�S t | tjƒr tjd| › �| d�S t | tjƒr@|s.t| j	j
ƒ}|s4t| ƒ}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.
    rF   r7   rG   rH   r6   N)r   r1   ZTimeoutExceptionr   rL   ZConnectErrorrN   r2   rO   r"   r&   rP   r>   r;   rQ   r   r   r   r4   ÿ   s*   þþþr4   c                 C   ó   t  | tj¡S )z-Maps an HTTP status to a platform error code.N)Ú_HTTP_STATUS_TO_ERROR_CODEr<   r   ÚUNKNOWN)rC   r   r   r   rO   &  ó   rO   c                 C   rR   )z*Maps an RPC code to a platform error code.N)Ú_RPC_CODE_TO_ERROR_CODEr<   r   rT   )r?   r   r   r   r=   *  rU   r=   c                 C   rR   )z0Maps a platform error code to an exception type.N)Ú_ERROR_CODE_TO_EXCEPTION_TYPEr<   r   r;   )r9   r   r   r   r>   .  rU   r>   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*   r-   z&Unexpected HTTP response with status: z; body: N)ÚjsonÚloadsr   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   rb   V  s   
z!EmulatorAdminCredentials.__init__c                 C   s   d S r    r   )rd   Úrequestr   r   r   ÚrefreshZ  s   z EmulatorAdminCredentials.refreshN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rb   rf   r   r   r   r   r\   P  s    r\   r    )NN)Drj   rX   Úplatformr   Útypingr   r   Zgoogle.authr^   rJ   r1   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ResourceExhaustedErrorÚ	CANCELLEDÚCancelledErrorZ	DATA_LOSSZDataLossErrorrT   r;   ZINTERNALZInternalErrorÚUNAVAILABLErN   ZDEADLINE_EXCEEDEDrL   rW   rS   rV   r   r   r!   r/   Ú	HTTPErrorZFirebaseErrorr5   rA   r(   r#   r3   r4   rO   r=   r>   r'   r_   r`   ra   r\   r   r   r   r   Ú<module>   s´   ÿþýüûúùø	÷
öõôóòñð÷ñ
þÿþ
ý
''