1# Copyright 2016 Google LLC 
    2# 
    3# Licensed under the Apache License, Version 2.0 (the "License"); 
    4# you may not use this file except in compliance with the License. 
    5# You may obtain a copy of the License at 
    6# 
    7#      http://www.apache.org/licenses/LICENSE-2.0 
    8# 
    9# Unless required by applicable law or agreed to in writing, software 
    10# distributed under the License is distributed on an "AS IS" BASIS, 
    11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    12# See the License for the specific language governing permissions and 
    13# limitations under the License. 
    14 
    15"""Transport - HTTP client library support. 
    16 
    17:mod:`google.auth` is designed to work with various HTTP client libraries such 
    18as urllib3 and requests. In order to work across these libraries with different 
    19interfaces some abstraction is needed. 
    20 
    21This module provides two interfaces that are implemented by transport adapters 
    22to support HTTP libraries. :class:`Request` defines the interface expected by 
    23:mod:`google.auth` to make requests. :class:`Response` defines the interface 
    24for the return value of :class:`Request`. 
    25""" 
    26 
    27import abc 
    28import http.client as http_client 
    29 
    30DEFAULT_RETRYABLE_STATUS_CODES = ( 
    31    http_client.INTERNAL_SERVER_ERROR, 
    32    http_client.SERVICE_UNAVAILABLE, 
    33    http_client.GATEWAY_TIMEOUT, 
    34    http_client.REQUEST_TIMEOUT, 
    35    http_client.TOO_MANY_REQUESTS, 
    36) 
    37"""Sequence[int]:  HTTP status codes indicating a request can be retried. 
    38""" 
    39 
    40 
    41DEFAULT_REFRESH_STATUS_CODES = (http_client.UNAUTHORIZED,) 
    42"""Sequence[int]:  Which HTTP status code indicate that credentials should be 
    43refreshed. 
    44""" 
    45 
    46DEFAULT_MAX_REFRESH_ATTEMPTS = 2 
    47"""int: How many times to refresh the credentials and retry a request.""" 
    48 
    49 
    50class Response(metaclass=abc.ABCMeta): 
    51    """HTTP Response data.""" 
    52 
    53    @abc.abstractproperty 
    54    def status(self): 
    55        """int: The HTTP status code.""" 
    56        raise NotImplementedError("status must be implemented.") 
    57 
    58    @abc.abstractproperty 
    59    def headers(self): 
    60        """Mapping[str, str]: The HTTP response headers.""" 
    61        raise NotImplementedError("headers must be implemented.") 
    62 
    63    @abc.abstractproperty 
    64    def data(self): 
    65        """bytes: The response body.""" 
    66        raise NotImplementedError("data must be implemented.") 
    67 
    68 
    69class Request(metaclass=abc.ABCMeta): 
    70    """Interface for a callable that makes HTTP requests. 
    71 
    72    Specific transport implementations should provide an implementation of 
    73    this that adapts their specific request / response API. 
    74 
    75    .. automethod:: __call__ 
    76    """ 
    77 
    78    @abc.abstractmethod 
    79    def __call__( 
    80        self, url, method="GET", body=None, headers=None, timeout=None, **kwargs 
    81    ): 
    82        """Make an HTTP request. 
    83 
    84        Args: 
    85            url (str): The URI to be requested. 
    86            method (str): The HTTP method to use for the request. Defaults 
    87                to 'GET'. 
    88            body (bytes): The payload / body in HTTP request. 
    89            headers (Mapping[str, str]): Request headers. 
    90            timeout (Optional[int]): The number of seconds to wait for a 
    91                response from the server. If not specified or if None, the 
    92                transport-specific default timeout will be used. 
    93            kwargs: Additionally arguments passed on to the transport's 
    94                request method. 
    95 
    96        Returns: 
    97            Response: The HTTP response. 
    98 
    99        Raises: 
    100            google.auth.exceptions.TransportError: If any exception occurred. 
    101        """ 
    102        # pylint: disable=redundant-returns-doc, missing-raises-doc 
    103        # (pylint doesn't play well with abstract docstrings.) 
    104        raise NotImplementedError("__call__ must be implemented.")