
    (                        S SK r S SKrS SKrS SKJr  S SKJr  \ R                  " \5      r	Sr
\R                  4r " S S\5      rS rS rS	 rS
 rS rS rS rSSS\R,                  S4S jr\R0                  " \" 5       5      rg)    N)http_client)_helpers)readseektellc                   0    \ rS rSrSrS rS rS rS rSr	g)	MemoryCache!   z8httplib2 Cache implementation which only caches locally.c                     0 U l         g Ncache)selfs    5platform/bq/third_party/oauth2client_4_0/transport.py__init__MemoryCache.__init__$   s	    
    c                 8    U R                   R                  U5      $ r   )r   getr   keys     r   r   MemoryCache.get'   s    zz~~c""r   c                      X R                   U'   g r   r   )r   r   values      r   setMemoryCache.set*   s    

3r   c                 <    U R                   R                  US 5        g r   )r   popr   s     r   deleteMemoryCache.delete-   s    

sD!r   r   N)
__name__
__module____qualname____firstlineno____doc__r   r   r   r   __static_attributes__ r   r   r	   r	   !   s    B# "r   r	   c                      [         $ )zReturn an HTTP object which caches results returned.

This is intended to be used in methods like
oauth2client_4_0.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   get_cached_httpr*   1   s
     r   c                  .    [         R                  " U 0 UD6$ )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kwargss     r   get_http_objectr0   >   s     ==$)&))r   c                 "    U c  0 $ [        U 5      $ )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.
)dict)headerss    r   _initialize_headersr4   M   s     23d7m3r   c                 @    Ub  SU ;   a  US-   U S   -   U S'   U $ XS'   U $ )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.
z
user-agent r'   )r3   
user_agents     r   _apply_user_agentr8   Z   sA     7"%/#%58M%MGL! N %/L!Nr   c                    0 n [         R                  " U 5       H  u  p#[        U[         R                  5      (       d  [	        U5      n[        U[         R                  5      (       d  [	        U5      n[
        R                  " U5      U[
        R                  " U5      '   M     U$ ! [         a    SSKJ	n  U" WSW5      ef = f)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_4_0.clientr:   )r3   cleankvr:   s        r   clean_headersrF   o   s     E	.MM'*DAa11Fa11F+3+=+=a+@E($$Q'( + L  .?!!T1--.s   B B& &Cc                    ^ ^ UR                   mSSS[        R                  S4U U4S jjnX!l         T UR                   l        g)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           
        >^ TR                   (       d&  [        R                  S5        TR                  T5        [	        U5      nTR                  U5        [        UTR                  5        S n[        U4S j[         5       5      (       a  TR                  5       n[        TXT[        U5      XE5      u  pxSn	[        U	5       H  n
UR                  [        ;  a    Xx4$ [        R                  SUR                  U
S-   U	5        TR                  T5        TR                  U5        Ub  TR!                  U5        [        TXT[        U5      XE5      u  pxM     Xx4$ )Nz1Attempting refresh to obtain initial access_tokenc              3   >   >#    U  H  n[        TUS 5      v   M     g 7fr   )getattr).0stream_propbodys     r   	<genexpr>:wrap_http_for_auth.<locals>.new_request.<locals>.<genexpr>   s%      #! 5@wt[$//!s      z&Refreshing due to a %s (attempt %s/%s)   )access_token_LOGGERinfo_refreshr4   applyr8   r7   all_STREAM_PROPERTIESr   requestrF   rangestatusREFRESH_STATUS_CODESr   )urimethodrN   r3   redirectionsconnection_typebody_stream_positionrespcontentmax_refresh_attemptsrefresh_attemptcredentialsorig_request_methods     `        r   new_request'wrap_http_for_auth.<locals>.new_request   sS    ''LL 0 1  !45 &g.'"';#9#9:# #!# # ##'99;  3S$ -g 6 ,?  !$%9:O{{"66 } LLAo&9-/   !45g&#/		./#$7d$1'$:$0CMD'  ; }r   )rZ   r,   DEFAULT_MAX_REDIRECTSrg   )rg   httpri   rh   s   `  @r   wrap_http_for_authrm      sD     ,, !&D$!)!?!?$(* *Z L  +DLLr   c                    ^ ^^ UR                   m[        T U5        UR                   mSSS[        R                  S4UU U4S jjnX!l         T UR                   l        g)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.
rH   Nc           	      v  > ST
R                   ;   a>  T
R                  b  T
R                  (       a  T
R                  S 5        [	        T	U XX4U5      $ [        U5      n[        UT
R                  5        U R                  SS5      S   nT
R                  SU05      u  pxSU-   US'   [	        TXU[        U5      XE5      $ )Naud?rR   r   zBearer Authorization)_kwargsrS   access_token_expiredrefreshrZ   r4   r8   r7   split_create_tokenrF   )r^   r_   rN   r3   r`   ra   uri_roottokenunused_expiryauthenticated_request_methodrg   rh   s            r   ri   -wrap_http_for_jwt_access.<locals>.new_request   s     K'''((044##D)7!*, , *'2Gg{'='=>yya(+H#.#<#<eX=N#O E'05'8GO$.T(1': :r   )rZ   rm   r,   rk   rg   )rg   rl   ri   r{   rh   s   `  @@r   wrap_http_for_jwt_accessr}      sW     ,,{D)#'<<  !&D$!)!?!?$(: :2 L  +DLLr   rH   c           	      Z    [         R                  SX!5        [        U SU 5      nU" XX4UUS9$ )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.
zRequesting: %s %srZ   )r_   rN   r3   r`   ra   )rT   rU   rK   )rl   r^   r_   rN   r3   r`   ra   http_callables           r   rZ   rZ      s8    2 LL$f2D)T2M$&2)8: :r   )loggingr,   r;   	six.movesr   oauth2client_4_0r   	getLoggerr!   rT   rY   UNAUTHORIZEDr]   objectr	   r*   r0   r4   r8   rF   rm   r}   rk   rZ   r-   r)   r'   r   r   <module>r      s       
 ! % 

H
%-  $002 "& " 
*
4*6@+F/+d $$!77 :@ }}[]+r   