U
    Sd'                     @   s   d Z ddlmZ ddlZddlmZ ddlmZ ddlZddlm	Z	 e
eZdZG dd	 d	ejZG d
d dejZdd ZG dd deZdS )zTransport adapter for httplib2.    )absolute_importN)
exceptions)	transport)http_client)readseektellc                   @   s<   e Zd ZdZdd Zedd Zedd Zedd	 Zd
S )	_Responsezhttplib2 transport response adapter.

    Args:
        response (httplib2.Response): The raw httplib2 response.
        data (bytes): The response body.
    c                 C   s   || _ || _d S N)	_response_data)selfresponsedata r   8/tmp/pip-unpacked-wheel-h6l5luug/google_auth_httplib2.py__init__(   s    z_Response.__init__c                 C   s   | j jS )zint: The HTTP status code.)r   statusr   r   r   r   r   ,   s    z_Response.statusc                 C   s
   t | jS )z-Mapping[str, str]: The HTTP response headers.)dictr   r   r   r   r   headers1   s    z_Response.headersc                 C   s   | j S )zbytes: The response body.)r   r   r   r   r   r   6   s    z_Response.dataN)	__name__
__module____qualname____doc__r   propertyr   r   r   r   r   r   r   r	       s   

r	   c                   @   s"   e Zd ZdZdd ZdddZdS )	Requesta  httplib2 request adapter.

    This class is used internally for making requests using various transports
    in a consistent way. If you use :class:`AuthorizedHttp` you do not need
    to construct or use this class directly.

    This class can be useful if you want to manually refresh a
    :class:`~google.auth.credentials.Credentials` instance::

        import google_auth_httplib2
        import httplib2

        http = httplib2.Http()
        request = google_auth_httplib2.Request(http)

        credentials.refresh(request)

    Args:
        http (httplib2.Http): The underlying http object to use to make
            requests.

    .. automethod:: __call__
    c                 C   s
   || _ d S r
   )http)r   r   r   r   r   r   U   s    zRequest.__init__GETNc           
   
   K   s   |dk	rt d z:t d|| | jj|f|||d|\}}t||W S  tjtj	fk
r }	 zt
|	W 5 d}	~	X Y nX dS )a3  Make an HTTP request using httplib2.

        Args:
            url (str): The URI to be requested.
            method (str): The HTTP method to use for the request. Defaults
                to 'GET'.
            body (bytes): The payload / body in HTTP request.
            headers (Mapping[str, str]): Request headers.
            timeout (Optional[int]): The number of seconds to wait for a
                response from the server. This is ignored by httplib2 and will
                issue a warning.
            kwargs: Additional arguments passed throught to the underlying
                :meth:`httplib2.Http.request` method.

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

        Raises:
            google.auth.exceptions.TransportError: If any exception occurred.
        Nzvhttplib2 transport does not support per-request timeout. Set the timeout when constructing the httplib2.Http instance.zMaking request: %s %s)methodbodyr   )_LOGGERwarningdebugr   requestr	   httplib2ZHttpLib2Errorr   HTTPExceptionr   ZTransportError)
r   urlr   r    r   timeoutkwargsr   r   excr   r   r   __call__X   s$      
zRequest.__call__)r   NNN)r   r   r   r   r   r+   r   r   r   r   r   <   s          r   c                   C   s   t  S )z)Returns a default httplib2.Http instance.)r%   ZHttpr   r   r   r   _make_default_http   s    r,   c                   @   s   e Zd ZdZdejejfddZdd Zddde	j
dfdd	Zdd
dZedd Zejdd Zedd Zejdd Zedd Zejdd Zedd Zejdd ZdS )AuthorizedHttpa  A httplib2 HTTP class with credentials.

    This class is used to perform requests to API endpoints that require
    authorization::

        from google.auth.transport._httplib2 import AuthorizedHttp

        authed_http = AuthorizedHttp(credentials)

        response = authed_http.request(
            'https://www.googleapis.com/storage/v1/b')

    This class implements :meth:`request` in the same way as
    :class:`httplib2.Http` and can usually be used just like any other
    instance of :class:``httplib2.Http`.

    The underlying :meth:`request` implementation handles adding the
    credentials' headers to the request and refreshing credentials as needed.
    Nc                 C   s6   |dkrt  }|| _|| _|| _|| _t| j| _dS )a  
        Args:
            credentials (google.auth.credentials.Credentials): The credentials
                to add to the request.
            http (httplib2.Http): The underlying HTTP object to
                use to make requests. If not specified, a
                :class:`httplib2.Http` instance will be constructed.
            refresh_status_codes (Sequence[int]): Which HTTP status codes
                indicate that credentials should be refreshed and the request
                should be retried.
            max_refresh_attempts (int): The maximum number of times to attempt
                to refresh the credentials and retry the request.
        N)r,   r   credentials_refresh_status_codes_max_refresh_attemptsr   _request)r   r.   r   Zrefresh_status_codesZmax_refresh_attemptsr   r   r   r      s    zAuthorizedHttp.__init__c                 C   s   | j   dS )zCalls httplib2's Http.closeN)r   closer   r   r   r   r2      s    zAuthorizedHttp.closer   c                    s   | dd}|dk	r| ni }	| j| j|||	 d}
t fddtD rV  }
| jj	||f |	||d|\}}|j
| jkr|| jk rtd|j
|d | j | j| j |
dk	rȈ |
 | j	||f ||||d d	|S ||fS )
z*Implementation of httplib2's Http.request._credential_refresh_attemptr   Nc                 3   s   | ]}t  |d V  qd S r
   )getattr).0Zstream_propr    r   r   	<genexpr>   s     z)AuthorizedHttp.request.<locals>.<genexpr>)r    r   redirectionsconnection_typez;Refreshing credentials due to a %s response. Attempt %s/%s.   )r    r   r8   r9   r3   )popcopyr.   Zbefore_requestr1   all_STREAM_PROPERTIESr   r   r$   r   r/   r0   r!   infoZrefreshr   )r   urir   r    r   r8   r9   r)   r3   Zrequest_headersZbody_stream_positionr   contentr   r6   r   r$      sV    


zAuthorizedHttp.requestc                 C   s   | j j||||d dS )z'Proxy to httplib2.Http.add_certificate.)passwordN)r   add_certificate)r   keycertdomainrB   r   r   r   rC   	  s    zAuthorizedHttp.add_certificatec                 C   s   | j jS )#Proxy to httplib2.Http.connections.r   connectionsr   r   r   r   rI     s    zAuthorizedHttp.connectionsc                 C   s   || j _dS )rG   NrH   r   valuer   r   r   rI     s    c                 C   s   | j jS )(Proxy to httplib2.Http.follow_redirects.r   follow_redirectsr   r   r   r   rN     s    zAuthorizedHttp.follow_redirectsc                 C   s   || j _dS )rL   NrM   rJ   r   r   r   rN     s    c                 C   s   | j jS )Proxy to httplib2.Http.timeout.r   r(   r   r   r   r   r(   !  s    zAuthorizedHttp.timeoutc                 C   s   || j _dS )rO   NrP   rJ   r   r   r   r(   &  s    c                 C   s   | j jS )&Proxy to httplib2.Http.redirect_codes.r   redirect_codesr   r   r   r   rS   +  s    zAuthorizedHttp.redirect_codesc                 C   s   || j _dS )rQ   NrR   rJ   r   r   r   rS   0  s    )N)r   r   r   r   r   ZDEFAULT_REFRESH_STATUS_CODESZDEFAULT_MAX_REFRESH_ATTEMPTSr   r2   r%   ZDEFAULT_MAX_REDIRECTSr$   rC   r   rI   setterrN   r(   rS   r   r   r   r   r-      s:   
 
J







r-   )r   
__future__r   loggingZgoogle.authr   r   r%   Z	six.movesr   	getLoggerr   r!   r>   Responser	   r   r,   objectr-   r   r   r   r   <module>   s   
E