o
    Bvg(                     @   s   d dl Z d dlmZ d dlZd dlZd dlmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZ z
d dlmZ d	ZW n eyI   d
ZY nw edd ZG dd dZdS )    N)asynccontextmanager)MappingOptional)_exponential_backoff
exceptions)	transport)Credentials)TimeoutError)RequestTFc                   sF   t  | fdd  fdd}z	|V  W    dS    w )a  
    timeout_guard is an asynchronous context manager to apply a timeout to an asynchronous block of code.

    Args:
        timeout (float): The time in seconds before the context manager times out.

    Raises:
        google.auth.exceptions.TimeoutError: If the code within the context exceeds the provided timeout.

    Usage:
        async with timeout_guard(10) as with_timeout:
            await with_timeout(async_function())
    c                     s0   t    } |  }|dkrtd d|S )Nr   z3Context manager exceeded the configured timeout of s.)time	monotonicr	   )elapsed	remaining)starttotal_timeout _/var/www/html/portfolio/venv/lib/python3.10/site-packages/google/auth/aio/transport/sessions.py_remaining_time4   s   
z&timeout_guard.<locals>._remaining_timec              
      sX   z  }t | |I d H }|W S  t jtfy+ } ztd|  d d|d }~ww )NzThe operation z$ exceeded the configured timeout of r   )asynciowait_forr	   )coror   responsee)r   r   r   r   with_timeout=   s   z#timeout_guard.<locals>.with_timeoutN)r   r   )timeoutr   r   )r   r   r   r   timeout_guard"   s   	
r   c                   @   s  e Zd ZdZ	ddedeej fddZddej	ej	fde
de
d	ee d
eee
e
f  dededejfddZeeddej	ej	fde
d	ee d
eee
e
f  dededejfddZeeddej	ej	fde
d	ee d
eee
e
f  dededejfddZeeddej	ej	fde
d	ee d
eee
e
f  dededejfddZeeddej	ej	fde
d	ee d
eee
e
f  dededejfddZeeddej	ej	fde
d	ee d
eee
e
f  dededejfddZdddZdS )AsyncAuthorizedSessiona  This is an asynchronous implementation of :class:`google.auth.requests.AuthorizedSession` class.
    We utilize an instance of a class that implements :class:`google.auth.aio.transport.Request` configured
    by the caller or otherwise default to `google.auth.aio.transport.aiohttp.Request` if the external aiohttp
    package is installed.

    A Requests Session class with credentials.

    This class is used to perform asynchronous requests to API endpoints that require
    authorization::

        import aiohttp
        from google.auth.aio.transport import sessions

        async with sessions.AsyncAuthorizedSession(credentials) as authed_session:
            response = await authed_session.request(
                'GET', 'https://www.googleapis.com/storage/v1/b')

    The underlying :meth:`request` implementation handles adding the
    credentials' headers to the request and refreshing credentials as needed.

    Args:
        credentials (google.auth.aio.credentials.Credentials):
            The credentials to add to the request.
        auth_request (Optional[google.auth.aio.transport.Request]):
            An instance of a class that implements
            :class:`~google.auth.aio.transport.Request` used to make requests
            and refresh credentials. If not passed,
            an instance of :class:`~google.auth.aio.transport.aiohttp.Request`
            is created.

    Raises:
        - google.auth.exceptions.TransportError: If `auth_request` is `None`
            and the external package `aiohttp` is not installed.
        - google.auth.exceptions.InvalidType: If the provided credentials are
            not of type `google.auth.aio.credentials.Credentials`.
    Ncredentialsauth_requestc                 C   sT   t |tstdt| d|| _|}|strt }|d u r%td|| _	d S )Nz#The configured credentials of type zJ are invalid and must be of type `google.auth.aio.credentials.Credentials`zv`auth_request` must either be configured or the external package `aiohttp` must be installed to use the default value.)

isinstancer   r   InvalidTypetype_credentialsAIOHTTP_INSTALLEDAiohttpRequestTransportError_auth_request)selfr   r   r'   r   r   r   __init__t   s   

zAsyncAuthorizedSession.__init__methodurldataheadersmax_allowed_timer   returnc              	      s   t jtjd}t|4 I dH ?}	|	| j| j|||I dH  |2 z 3 dH W }
|	| j|||||fi |I dH }|jtj	vrB nq"6 W d  I dH  |S 1 I dH sUw   Y  |S )a  
        Args:
                method (str): The http method used to make the request.
                url (str): The URI to be requested.
                data (Optional[bytes]): The payload or body in HTTP request.
                headers (Optional[Mapping[str, str]]): Request headers.
                timeout (float):
                The amount of time in seconds to wait for the server response
                with each individual request.
                max_allowed_time (float):
                If the method runs longer than this, a ``Timeout`` exception is
                automatically raised. Unlike the ``timeout`` parameter, this
                value applies to the total method execution time, even if
                multiple requests are made under the hood.

                Mind that it is not guaranteed that the timeout error is raised
                at ``max_allowed_time``. It might take longer, for example, if
                an underlying request takes a lot of time, but the request
                itself does not timeout, e.g. if a large file is being
                transmitted. The timout error will be raised after such
                request completes.

        Returns:
                google.auth.aio.transport.Response: The HTTP response.

        Raises:
                google.auth.exceptions.TimeoutError: If the method does not complete within
                the configured `max_allowed_time` or the request exceeds the configured
                `timeout`.
        )total_attemptsN)
r   AsyncExponentialBackoffr   DEFAULT_MAX_RETRY_ATTEMPTSr   r#   before_requestr'   status_codeDEFAULT_RETRYABLE_STATUS_CODES)r(   r*   r+   r,   r-   r.   r   kwargsretriesr   _r   r   r   r   request   s,   )


zAsyncAuthorizedSession.requestc                    $   | j d|||||fi |I d H S )NGETr9   r(   r+   r,   r-   r.   r   r6   r   r   r   get      
zAsyncAuthorizedSession.getc                    r:   )NPOSTr<   r=   r   r   r   post   r?   zAsyncAuthorizedSession.postc                    r:   )NPUTr<   r=   r   r   r   put   r?   zAsyncAuthorizedSession.putc                    r:   )NPATCHr<   r=   r   r   r   patch   r?   zAsyncAuthorizedSession.patchc                    r:   )NDELETEr<   r=   r   r   r   delete   r?   zAsyncAuthorizedSession.deletec                    s   | j  I dH  dS )z<
        Close the underlying auth request session.
        N)r'   close)r(   r   r   r   rH     s   zAsyncAuthorizedSession.close)N)r/   N)__name__
__module____qualname____doc__r   r   r   r
   r)   _DEFAULT_TIMEOUT_SECONDSstrbytesr   floatResponser9   	functoolswrapsr>   rA   rC   rE   rG   rH   r   r   r   r   r   N   s    &
	
=r   )r   
contextlibr   rR   r   typingr   r   google.authr   r   google.auth.aior   google.auth.aio.credentialsr   google.auth.exceptionsr	   !google.auth.aio.transport.aiohttpr
   r%   r$   ImportErrorr   r   r   r   r   r   <module>   s$   
+