o
    BvgI                  
   @   s   d Z ddlZddlmZmZmZ zddlZW n ey' Z zededZ[ww ddl	m
Z
 ddl	mZ ddlmZ G dd	 d	ejZG d
d dejZdS )zCTransport adapter for Asynchronous HTTP Requests based on aiohttp.
    N)AsyncGeneratorMappingOptionalzjThe aiohttp library is not installed from please install the aiohttp package to use the aiohttp transport.)_helpers)
exceptions)	transportc                   @   s   e Zd ZdZdejfddZee	e
jdefddZee	e
jdeeef fdd	Ze	e
jddedeedf fddZe	e
jdefddZe	e
jdd ZdS )Responseaz  
    Represents an HTTP response and its data. It is returned by ``google.auth.aio.transport.sessions.AsyncAuthorizedSession``.

    Args:
        response (aiohttp.ClientResponse): An instance of aiohttp.ClientResponse.

    Attributes:
        status_code (int): The HTTP status code of the response.
        headers (Mapping[str, str]): The HTTP headers of the response.
    responsec                 C   s
   || _ d S N)	_response)selfr	    r   ^/var/www/html/portfolio/venv/lib/python3.10/site-packages/google/auth/aio/transport/aiohttp.py__init__-   s   
zResponse.__init__returnc                 C   s   | j jS r
   )r   statusr   r   r   r   status_code0   s   zResponse.status_codec                 C   s   dd | j j D S )Nc                 S   s   i | ]\}}||qS r   r   ).0keyvaluer   r   r   
<dictcomp>8   s    z$Response.headers.<locals>.<dictcomp>)r   headersitemsr   r   r   r   r   5   s   zResponse.headers   
chunk_sizeNc              
   C  sT   z| j j|2 z	3 d H W }|V  q	6 W d S  tjy) } ztd|d }~ww )Nz'Failed to read from the payload stream.)r   contentiter_chunkedaiohttpClientPayloadErrorr   ResponseError)r   r   chunkexcr   r   r   r   :   s   zResponse.contentc              
      s<   z	| j  I d H W S  tjy } ztd|d }~ww )Nz!Failed to read the response body.)r   readr   ClientResponseErrorr   r    )r   r"   r   r   r   r#   F   s   zResponse.readc                    s   | j   d S r
   )r   closer   r   r   r   r%   M   s   zResponse.close)r   )__name__
__module____qualname____doc__r   ClientResponser   propertyr   copy_docstringr   r   intr   r   strr   r   bytesr   r#   r%   r   r   r   r   r   !   s    




r   c                   @   sl   e Zd ZdZddejfddZdddejfde	de	d	e
e d
e
ee	e	f  dedejfddZdddZdS )Requesta  Asynchronous Requests request adapter.

    This class is used internally for making requests using aiohttp
    in a consistent way. If you use :class:`google.auth.aio.transport.sessions.AsyncAuthorizedSession`
    you do not need to construct or use this class directly.

    This class can be useful if you want to configure a Request callable
    with a custom ``aiohttp.ClientSession`` in :class:`AuthorizedSession` or if
    you want to manually refresh a :class:`~google.auth.aio.credentials.Credentials` instance::

        import aiohttp
        import google.auth.aio.transport.aiohttp

        # Default example:
        request = google.auth.aio.transport.aiohttp.Request()
        await credentials.refresh(request)

        # Custom aiohttp Session Example:
        session = session=aiohttp.ClientSession(auto_decompress=False)
        request = google.auth.aio.transport.aiohttp.Request(session=session)
        auth_sesion = google.auth.aio.transport.sessions.AsyncAuthorizedSession(auth_request=request)

    Args:
        session (aiohttp.ClientSession): An instance :class:`aiohttp.ClientSession` used
            to make HTTP requests. If not specified, a session will be created.

    .. automethod:: __call__
    Nsessionc                 C   s   || _ d| _d S )NF)_session_closed)r   r1   r   r   r   r   p   s   
zRequest.__init__GETurlmethodbodyr   timeoutr   c              
      s   z-| j r
td| jst | _tj|d}| jj||f|||d|I dH }t|W S  tj	yG }	 ztd| d}
|
|	d}	~	w t
jy_ }	 ztd| d}||	d}	~	ww )	a  
        Make an HTTP request using aiohttp.

        Args:
            url (str): The URL to be requested.
            method (Optional[str]):
                The HTTP method to use for the request. Defaults to 'GET'.
            body (Optional[bytes]):
                The payload or body in HTTP request.
            headers (Optional[Mapping[str, str]]):
                Request headers.
            timeout (float): The number of seconds to wait for a
                response from the server. If not specified or if None, the
                requests default timeout will be used.
            kwargs: Additional arguments passed through to the underlying
                aiohttp :meth:`aiohttp.Session.request` method.

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

        Raises:
            - google.auth.exceptions.TransportError: If the request fails or if the session is closed.
            - google.auth.exceptions.TimeoutError: If the request times out.
        zsession is closed.)total)datar   r8   NzFailed to send request to .zRequest timed out after z	 seconds.)r3   r   TransportErrorr2   r   ClientSessionClientTimeoutrequestr   ClientErrorasyncioTimeoutError)r   r5   r6   r7   r   r8   kwargsclient_timeoutr	   
caught_exc
client_exctimeout_excr   r   r   __call__t   s:   "



zRequest.__call__c                    s(   | j s| jr| j I dH  d| _ dS )zY
        Close the underlying aiohttp session to release the acquired resources.
        NT)r3   r2   r%   r   r   r   r   r%      s   
zRequest.closer
   )r   N)r&   r'   r(   r)   r   r=   r   r   _DEFAULT_TIMEOUT_SECONDSr.   r   r/   r   floatr   rH   r%   r   r   r   r   r0   R   s*    
>r0   )r)   rA   typingr   r   r   r   ImportErrorrE   google.authr   r   google.auth.aior   r   r0   r   r   r   r   <module>   s$   1