U
    
ڲg                     @   sl   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mZ ddlmZ dddZ	dd Z
dd	d
Zdd ZdS )z:Internal utilities for interacting with Google API client.    N)
exceptions)_utilsc                 C   sl   t | tjjst| S | j }| jj}t	
||\}}t| }d}|rV|| |||}|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)
isinstancegoogleapiclienterrors	HttpErrorhandle_googleapiclient_errorcontentdecoderespstatusr   Z_parse_platform_error)_http_response_from_googleapiclient_error_handle_func_googleapiclient)errorZhandle_funcr	   status_code
error_dictmessagehttp_responseexc r   ?/tmp/pip-unpacked-wheel-p0r7i5ii/firebase_admin/_gapic_utils.py*handle_platform_error_from_googleapiclient   s    
r   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   )getr   )r   r   r   r   coder   r   r   r   8   s    
r   c                 C   s   t | tjs$t | tjr8dt| kr8tjd| | dS t | tj	rXtj
d| | dS t | tjjsztjd| | dS |st| jj}|st| }|st| }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 outz'Timed out while making an API call: {0})r   causez%Failed to establish a connection: {0}z5Unknown error while making a remote service call: {0})r   r   r   )r   sockettimeoutr   strr   ZDeadlineExceededErrorformathttplib2ZServerNotFoundErrorZUnavailableErrorr   r   r   ZUnknownErrorr   Z_http_status_to_error_coder   r   r   Z_error_code_to_exception_type)r   r   r   r   Zerr_typer   r   r   r   H   s6    


r   c                 C   s&   t j }t| j|_| jj|_	|S )zMCreates a requests HTTP Response object from the given googleapiclient error.)
requestsmodelsResponseioBytesIOr	   rawr   r   r   )r   r   r   r   r   r   u   s    

r   )N)NNN)__doc__r#   r   r   r   r    Zfirebase_adminr   r   r   r   r   r   r   r   r   r   <module>   s   

-