
    gj]                     8   d dl Z d dlmZ d dlZd dlZd dlmZmZ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 d dlmZ d dlZerd dlZd d	lmZ n	 d dlZd d	lmZ n# eef$ r dZY nw xY w	 d d
lmZ dZn# e$ r dZY nw xY wed             Z G d d          Z dS )    N)asynccontextmanager)MappingOptionalTYPE_CHECKINGUnion)_exponential_backoff
exceptions)	transport)Credentials)mtls)TimeoutError)ClientTimeout)RequestTFc                   K   t          j                    | fdfd}	 |W V                dS #               w xY 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                  p    t          j                    z
  } | z
  }|dk    rt          d d          |S )Nr   z3Context manager exceeded the configured timeout of s.)time	monotonicr   )elapsed	remainingstarttotal_timeouts     f/var/www/app.qaans.com/backend/venv/lib/python3.11/site-packages/google/auth/aio/transport/sessions.py_remaining_timez&timeout_guard.<locals>._remaining_timeB   sP    .""U*!G+	>>WmWWW       c                    K   	              }t          j        | |           d {V }|S # t           j        t          f$ r}t          d|  d d          |d }~ww xY w)NzThe operation z$ exceeded the configured timeout of r   )asynciowait_forr   )coror   responseer   r   s       r   with_timeoutz#timeout_guard.<locals>.with_timeoutK   s      	'))I$-dI>>>>>>>>HO$l3 	 	 	\\\=\\\ 	s   &, AAAN)r   r   )timeoutr"   r   r   r   s     @@@r   timeout_guardr$   0   s       NEM           	s	   9 Ac                      e Zd ZdZ	 ddedeej                 fdZddZ	ddej
        ej
        ej        fdeded	ee         d
eeeef                  dedeeef         dee         dej        fdZ ej        e          ddej
        ej
        ej        fded	ee         d
eeeef                  dedeeef         dee         dej        fd            Z ej        e          ddej
        ej
        ej        fded	ee         d
eeeef                  dedeeef         dee         dej        fd            Z ej        e          ddej
        ej
        ej        fded	ee         d
eeeef                  dedeeef         dee         dej        fd            Z ej        e          ddej
        ej
        ej        fded	ee         d
eeeef                  dedeeef         dee         dej        fd            Z ej        e          ddej
        ej
        ej        fded	ee         d
eeeef                  dedeeef         dee         dej        fd            Zed             Z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                    t          |t                    s%t          j        dt	          |           d          || _        |}|st          rt                      }d| _        d | _	        d | _
        |t          j        d          || _        d S )Nz#The configured credentials of type zJ are invalid and must be of type `google.auth.aio.credentials.Credentials`Fzv`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_is_mtls_mtls_init_task_cached_certTransportError_auth_request)selfr'   r(   r4   s       r   __init__zAsyncAuthorizedSession.__init__   s     +{33 	( Td;6G6G  T  T  T   ($ 	-!2 	-*,,M#  + I   +r   c                     K    j         ' fd}t          j         |                       _          j          d{V S )a  Configure the client certificate and key for SSL connection.

        The function does nothing unless `GOOGLE_API_USE_CLIENT_CERTIFICATE` is
        explicitly set to `true`. In this case if client certificate and key are
        successfully obtained (from the given client_cert_callback or from application
        default SSL credentials), the underlying transport will be reconfigured
        to use mTLS.
        Note: This function does nothing if the `aiohttp` library is not
        installed.
        Important: Calling this method will close any ongoing API requests associated
        with the current session. To ensure a smooth transition, it is recommended
        to call this during session initialization.

        Args:
            client_cert_callback (Optional[Callable[[], (bytes, bytes)]]):
                The optional callback returns the client certificate and private
                key bytes both in PEM format.
                If the callback is None, application default SSL credentials
                will be used.

        Raises:
            google.auth.exceptions.MutualTLSChannelError: If mutual TLS channel
                creation failed for any reason.
        Nc                    K   t          j        t          j        j        j        j                   d {V } | s	d
_        d S 	 t          j        	           d {V \  
_        }}
j        r|
_	        t          j        t           j
        ||           d {V }t          r~t          
j        t                    rft          j        |          }t          j        |          }
j        }t          |          
_        |                                 d {V  d S d S d S d S # t&          j        t*          t,          f$ r}t'          j        |          }||d }~ww xY w)NF)ssl)	connector)session)r   _run_in_executorgoogleauthr
   _mtls_helpercheck_use_client_certr0   get_client_cert_and_keyr2   make_client_cert_ssl_contextr.   r*   r4   r/   aiohttpTCPConnectorClientSessioncloser	   ClientCertErrorImportErrorOSErrorMutualTLSChannelError)use_client_certcertkeyssl_contextr:   new_sessionold_auth_request
caught_excnew_excclient_cert_callbackr5   s            r   _do_configurezDAsyncAuthorizedSession.configure_mtls_channel.<locals>._do_configure   s     (,(=K)6L) ) # # # # # # ' $)DMF2
 #:;OPPPPPPPP	 } ;,0),0,A =tS- - ' ' ' ' ' '
 - 	; .2 2 	; )0(<(M(M(MI*1*?)*T*T*TK/3/A,1?1T1T1TD."2"8"8":"::::::::::!; ;	; 	; 	; 	; . 2 2 2
 )>zJJG!z12s   CD# #E?EE)r1   r   create_task)r5   rS   rT   s   `` r   configure_mtls_channelz-AsyncAuthorizedSession.configure_mtls_channel   sh      2 '(2 (2 (2 (2 (2 (2T $+#6}}#G#GD ))))))))r   methodurldataheadersmax_allowed_timer#   total_attemptsreturnc                   K   | j         r	 | j          d{V  n# t          $ r Y nw xY wt          j        |          }	|i }t	          |          4 d{V }
 |
| j                            | j        |||                     d{V  d}t          &t          |t                    r|j
        |j
        nd}n+t          |t          t          f          rt          |          }|	2 3 d{V } |
 | j        |||||fi |           d{V }|j        t          j        vr n>6 	 ddd          d{V  n# 1 d{V swxY w Y   |S )aY  
        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, aiohttp.ClientTimeout):
                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.
                total_attempts (int):
                The total number of retry attempts.

                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 timeout 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`.
        N)r\   g        )r1   	Exceptionr   AsyncExponentialBackoffr$   r-   before_requestr4   r   r*   totalintfloatstatus_coder
   DEFAULT_RETRYABLE_STATUS_CODES)r5   rW   rX   rY   rZ   r[   r#   r\   kwargsretriesr"   actual_timeout_r    s                 r   requestzAsyncAuthorizedSession.request   s     V  	*********     '>)
 
 
 ?G !122 	 	 	 	 	 	 	l,!00&W          %(N(Z-O-O(29-2KQTGc5\22 0!&w #       a!-&D&VT7N FL " "      
 'y/WWWE X #7	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	, s+    
&&BD7%D#+9D77
EEc           	      :   K    | j         d||||||fi | d{V S )  
        Args:
                url (str): The URI to be requested.
                data (Optional[bytes]): The payload or body in HTTP request.
                headers (Optional[Mapping[str, str]]): Request headers.
                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.
                timeout (float, aiohttp.ClientTimeout):
                The amount of time in seconds to wait for the server response
                with each individual request.
                total_attempts (int):
                The total number of retry attempts.

                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 timeout 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`.
        GETNrk   r5   rX   rY   rZ   r[   r#   r\   rg   s           r   getzAsyncAuthorizedSession.get.  a      T "T\	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 		
r   c           	      :   K    | j         d||||||fi | d{V S )rm   POSTNro   rp   s           r   postzAsyncAuthorizedSession.postc  sa      T "T\	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 		
r   c           	      :   K    | j         d||||||fi | d{V S )rm   PUTNro   rp   s           r   putzAsyncAuthorizedSession.put  rr   r   c           	      :   K    | j         d||||||fi | d{V S )rm   PATCHNro   rp   s           r   patchzAsyncAuthorizedSession.patch  sa      T "T\	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 		
r   c           	      :   K    | j         d||||||fi | d{V S )rm   DELETENro   rp   s           r   deletezAsyncAuthorizedSession.delete  sa      T "T\	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 		
r   c                     | j         S )z#Indicates if mutual TLS is enabled.)r0   r5   s    r   is_mtlszAsyncAuthorizedSession.is_mtls7  s     }r   c                 H   K   | j                                          d{V  dS )z<
        Close the underlying auth request session.
        N)r4   rF   r   s    r   rF   zAsyncAuthorizedSession.close<  s5        &&(((((((((((r   )N)r]   N)__name__
__module____qualname____doc__r   r   r
   r   r6   rV   _DEFAULT_TIMEOUT_SECONDSDEFAULT_MAX_RETRY_ATTEMPTSstrbytesr   rd   r   r   rc   Responserk   	functoolswrapsrq   ru   rx   r{   r~   propertyr   rF    r   r   r&   r&   \   s       # #L UY+ +&+6>y?P6Q+ + + +(G* G* G* G*Z !%/3"+"D/8/Q(1(LM MM M uo	M
 '#s(+,M  M um+,M !M 
	M M M M^ Y_W !%/3"+"D/8/Q(1(L2
 2
2
 uo2
 '#s(+,	2

  2
 um+,2
 !2
 
	2
 2
 2
 2
h Y_W !%/3"+"D/8/Q(1(L2
 2
2
 uo2
 '#s(+,	2

  2
 um+,2
 !2
 
	2
 2
 2
 2
h Y_W !%/3"+"D/8/Q(1(L2
 2
2
 uo2
 '#s(+,	2

  2
 um+,2
 !2
 
	2
 2
 2
 2
h Y_W !%/3"+"D/8/Q(1(L2
 2
2
 uo2
 '#s(+,	2

  2
 um+,2
 !2
 
	2
 2
 2
 2
h Y_W !%/3"+"D/8/Q(1(L2
 2
2
 uo2
 '#s(+,	2

  2
 um+,2
 !2
 
	2
 2
 2
 2
h   X) ) ) ) ) )r   r&   )!r   
contextlibr   r   r   typingr   r   r   r   google.authr   r	   google.auth.aior
   google.auth.aio.credentialsr   google.auth.aio.transportr   google.auth.exceptionsr   "google.auth.transport._mtls_helperr=   rC   r   rH   AttributeError!google.auth.aio.transport.aiohttpr   r/   r.   r$   r&   r   r   r   <module>r      s    * * * * * *      : : : : : : : : : : : : 8 8 8 8 8 8 8 8 % % % % % % 3 3 3 3 3 3 * * * * * * / / / / / / ) ) ) ) 	NNN%%%%%%%)))))))(   KKKKKK    ( ( (Vd) d) d) d) d) d) d) d) d) d)s$   
A 	A('A(,A5 5A?>A?