o
    i(                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ e eZ	dZ
ejfZG dd deZdd Zd	d
 Zdd Zdd Zdd Zdd Zdd ZdddejdfddZee ZdS )    N)http_client)_helpers)readseektellc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )MemoryCachez8httplib2 Cache implementation which only caches locally.c                 C   s
   i | _ d S Ncache)self r   Q/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/oauth2client/transport.py__init__#   s   
zMemoryCache.__init__c                 C   s   | j |S r   )r
   getr   keyr   r   r   r   &   s   zMemoryCache.getc                 C   s   || j |< d S r   r	   )r   r   valuer   r   r   set)   s   zMemoryCache.setc                 C   s   | j |d  d S r   )r
   popr   r   r   r   delete,   s   zMemoryCache.deleteN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r       s    r   c                   C   s   t S )a  Return an HTTP object which caches results returned.

    This is intended to be used in methods like
    oauth2client.client.verify_id_token(), which calls to the same URI
    to retrieve certs.

    Returns:
        httplib2.Http, an HTTP object with a MemoryCache
    )_CACHED_HTTPr   r   r   r   get_cached_http0   s   
r   c                  O   s   t j| i |S )a>  Return a new HTTP object.

    Args:
        *args: tuple, The positional arguments to be passed when
               contructing a new HTTP object.
        **kwargs: dict, The keyword arguments to be passed when
                  contructing a new HTTP object.

    Returns:
        httplib2.Http, an HTTP object.
    )httplib2Http)argskwargsr   r   r   get_http_object=   s   r    c                 C   s   | du ri S t | S )zCreates a copy of the headers.

    Args:
        headers: dict, request headers to copy.

    Returns:
        dict, the copied headers or a new dictionary if the headers
        were None.
    N)dict)headersr   r   r   _initialize_headersL   s   
r#   c                 C   s4   |durd| v r|d | d  | d< | S || d< | S )a,  Adds a user-agent to the headers.

    Args:
        headers: dict, request headers to add / modify user
                 agent within.
        user_agent: str, the user agent to add.

    Returns:
        dict, the original headers passed in, but modified if the
        user agent is not None.
    Nz
user-agent r   )r"   
user_agentr   r   r   _apply_user_agentY   s   r&   c                 C   s   i }z+t | D ]"\}}t|t jst|}t|t js t|}t||t|< qW |S  ty@   ddlm	} ||d|w )a  Forces header keys and values to be strings, i.e not unicode.

    The httplib module just concats the header keys and values in a way that
    may make the message header a unicode string, which, if it then tries to
    contatenate to a binary request body may result in a unicode decode error.

    Args:
        headers: dict, A dictionary of headers.

    Returns:
        The same dictionary but with all the keys converted to strings.
    r   )NonAsciiHeaderErrorz: )
six	iteritems
isinstancebinary_typestrr   	_to_bytesUnicodeEncodeErroroauth2client.clientr'   )r"   cleankvr'   r   r   r   clean_headersn   s   	r3   c                    s4   |j dddtjdf fdd	}||_  |j _dS )a  Prepares an HTTP object's request method for auth.

    Wraps HTTP requests with logic to catch auth failures (typically
    identified via a 401 status code). In the event of failure, tries
    to refresh the token used and then retry the original request.

    Args:
        credentials: Credentials, the credentials used to identify
                     the authenticated user.
        http: httplib2.Http, an http object to be used to make
              auth requests.
    GETNc              	      s   j std  t|}| t|j d }t fddt	D r- 
 }t| | t|||\}}d}	t|	D ]8}
|jtvrM ||fS td|j|
d |	  | |d urk | t| | t|||\}}qA||fS )Nz1Attempting refresh to obtain initial access_tokenc                 3   s    | ]	}t  |d V  qd S r   getattr).0stream_propbodyr   r   	<genexpr>   s    z:wrap_http_for_auth.<locals>.new_request.<locals>.<genexpr>   z&Refreshing due to a %s (attempt %s/%s)   )access_token_LOGGERinfo_refreshr#   applyr&   r%   all_STREAM_PROPERTIESr   requestr3   rangestatusREFRESH_STATUS_CODESr   )urimethodr:   r"   redirectionsconnection_typebody_stream_positionrespcontentmax_refresh_attemptsrefresh_attemptcredentialsorig_request_methodr9   r   new_request   sB   










z'wrap_http_for_auth.<locals>.new_request)rE   r   DEFAULT_MAX_REDIRECTSrS   rS   httprU   r   rR   r   wrap_http_for_auth   s   -rY   c                    sF   |j t| |j  dddtjdf fdd	}||_ |j _dS )a  Prepares an HTTP object's request method for JWT access.

    Wraps HTTP requests with logic to catch auth failures (typically
    identified via a 401 status code). In the event of failure, tries
    to refresh the token used and then retry the original request.

    Args:
        credentials: _JWTAccessCredentials, the credentials used to identify
                     a service account that uses JWT access tokens.
        http: httplib2.Http, an http object to be used to make
              auth requests.
    r4   Nc           	         s   dj v rjd u sjrd  t | |||||S t|}t|j | ddd }	d|i\}}d| |d< t| ||t
|||S )Naud?r=   r   zBearer Authorization)_kwargsr>   access_token_expiredrefreshrE   r#   r&   r%   split_create_tokenr3   )	rI   rJ   r:   r"   rK   rL   uri_roottokenunused_expiryauthenticated_request_methodrS   rT   r   r   rU      s$   



z-wrap_http_for_jwt_access.<locals>.new_request)rE   rY   r   rV   rS   rW   r   re   r   wrap_http_for_jwt_access   s   
rg   r4   c                 C   s    t | d| }|||||||dS )a  Make an HTTP request with an HTTP object and arguments.

    Args:
        http: httplib2.Http, an http object to be used to make requests.
        uri: string, The URI to be requested.
        method: string, The HTTP method to use for the request. Defaults
                to 'GET'.
        body: string, The payload / body in HTTP request. By default
              there is no payload.
        headers: dict, Key-value pairs of request headers. By default
                 there are no headers.
        redirections: int, The number of allowed 203 redirects for
                      the request. Defaults to 5.
        connection_type: httplib.HTTPConnection, a subclass to be used for
                         establishing connection. If not set, the type
                         will be determined from the ``uri``.

    Returns:
        tuple, a pair of a httplib2.Response with the status code and other
        headers and the bytes of the content returned.
    rE   )rJ   r:   r"   rK   rL   r5   )rX   rI   rJ   r:   r"   rK   rL   http_callabler   r   r   rE      s
   
rE   )loggingr   r(   	six.movesr   oauth2clientr   	getLoggerr   r?   rD   UNAUTHORIZEDrH   objectr   r   r    r#   r&   r3   rY   rg   rV   rE   r   r   r   r   r   r   <module>   s*   
C2
