ó
‚¾^Yc           @   së   d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l m Z e  j e ƒ Z	 d Z
 e j f Z d e f d „  ƒ  YZ d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d e j d d „ Z e j e ƒ  ƒ Z d S(   iÿÿÿÿN(   t   http_client(   t   _helperst   readt   seekt   tellt   MemoryCachec           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s8   httplib2 Cache implementation which only caches locally.c         C   s   i  |  _  d  S(   N(   t   cache(   t   self(    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   __init__#   s    c         C   s   |  j  j | ƒ S(   N(   R   t   get(   R   t   key(    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyR	   &   s    c         C   s   | |  j  | <d  S(   N(   R   (   R   R
   t   value(    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   set)   s    c         C   s   |  j  j | d  ƒ d  S(   N(   R   t   popt   None(   R   R
   (    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   delete,   s    (   t   __name__t
   __module__t   __doc__R   R	   R   R   (    (    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyR       s
   			c           C   s   t  S(   s  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
    (   t   _CACHED_HTTP(    (    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   get_cached_http0   s    
c          O   s   t  j |  | Ž  S(   s>  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.
    (   t   httplib2t   Http(   t   argst   kwargs(    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   get_http_object=   s    c         C   s   |  d k r i  St |  ƒ S(   sÃ   Creates 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(   R   t   dict(   t   headers(    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   _initialize_headersL   s    
c         C   sB   | d k	 r> d |  k r1 | d |  d |  d <q> | |  d <n  |  S(   s,  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.
    s
   user-agentt    N(   R   (   R   t
   user_agent(    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   _apply_user_agentY   s
    c         C   sÂ   i  } y‚ x{ t  j |  ƒ D]j \ } } t | t  j ƒ sF t | ƒ } n  t | t  j ƒ sg t | ƒ } n  t j | ƒ | t j | ƒ <q WWn3 t k
 r½ d d l m	 } | | d | ƒ ‚ n X| S(   s½  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.
    iÿÿÿÿ(   t   NonAsciiHeaderErrors   : (
   t   sixt	   iteritemst
   isinstancet   binary_typet   strR   t	   _to_bytest   UnicodeEncodeErrort   oauth2client.clientR    (   R   t   cleant   kt   vR    (    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   clean_headersn   s    $c            sF   | j  ‰ d d d t j d ‡  ‡ f d † } | | _  ˆ  | j  _ d S(   sÝ  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.
    t   GETc      	      sa  ˆ j  s& t j d ƒ ˆ j ˆ ƒ n  t | ƒ } ˆ j | ƒ t | ˆ j ƒ d  } t	 ‡  f d †  t
 Dƒ ƒ r€ ˆ  j ƒ  } n  t ˆ |  | ˆ  t | ƒ | | ƒ \ } } d }	 x¤ t |	 ƒ D]– }
 | j t k rÖ Pn  t j d | j |
 d |	 ƒ ˆ j ˆ ƒ ˆ j | ƒ | d  k	 r)ˆ  j | ƒ n  t ˆ |  | ˆ  t | ƒ | | ƒ \ } } q½ W| | f S(   Ns1   Attempting refresh to obtain initial access_tokenc         3   s!   |  ] } t  ˆ  | d  ƒ Vq d  S(   N(   t   getattrR   (   t   .0t   stream_prop(   t   body(    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pys	   <genexpr>©   s    i   s&   Refreshing due to a %s (attempt %s/%s)i   (   t   access_tokent   _LOGGERt   infot   _refreshR   t   applyR   R   R   t   allt   _STREAM_PROPERTIESR   t   requestR,   t   ranget   statust   REFRESH_STATUS_CODESR   (   t   urit   methodR1   R   t   redirectionst   connection_typet   body_stream_positiont   respt   contentt   max_refresh_attemptst   refresh_attempt(   t   credentialst   orig_request_method(   R1   s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   new_request™   s8    				N(   R9   R   R   t   DEFAULT_MAX_REDIRECTSRF   (   RF   t   httpRH   (    (   RF   RG   s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   wrap_http_for_auth‰   s    		+	c            s_   | j  ‰ t ˆ | ƒ | j  ‰  d d d t j d ‡  ‡ ‡ f d † } | | _  ˆ | j  _ d S(   s  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.
    R-   c   	         sÕ   d ˆ j  k rS ˆ j d  k s' ˆ j r7 ˆ j d  ƒ n  t ˆ  |  | | | | | ƒ St | ƒ } t | ˆ j ƒ |  j	 d d ƒ d } ˆ j
 i | d 6ƒ \ } } d | | d <t ˆ |  | | t | ƒ | | ƒ Sd  S(   Nt   audt   ?i   i    s   Bearer t   Authorization(   t   _kwargsR2   R   t   access_token_expiredt   refreshR9   R   R   R   t   splitt   _create_tokenR,   (	   R=   R>   R1   R   R?   R@   t   uri_roott   tokent   unused_expiry(   t   authenticated_request_methodRF   RG   (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyRH   ß   s    			N(   R9   RK   R   R   RI   RF   (   RF   RJ   RH   (    (   RW   RF   RG   s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   wrap_http_for_jwt_accessÌ   s    				R-   c         C   s:   t  |  d |  ƒ } | | d | d | d | d | d | ƒS(   sÌ  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.
    R9   R>   R1   R   R?   R@   (   R.   (   RJ   R=   R>   R1   R   R?   R@   t   http_callable(    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyR9   þ   s    (   s   reads   seeks   tell(   t   loggingR   R!   t	   six.movesR    t   oauth2clientR   t	   getLoggerR   R3   R8   t   UNAUTHORIZEDR<   t   objectR   R   R   R   R   R,   RK   RX   R   RI   R9   R   R   (    (    (    s<   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/transport.pyt   <module>   s&   						C	2	