• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
28
29import six
30from six.moves import http_client
31
32DEFAULT_REFRESH_STATUS_CODES = (http_client.UNAUTHORIZED,)
33"""Sequence[int]:  Which HTTP status code indicate that credentials should be
34refreshed and a request should be retried.
35"""
36
37DEFAULT_MAX_REFRESH_ATTEMPTS = 2
38"""int: How many times to refresh the credentials and retry a request."""
39
40
41@six.add_metaclass(abc.ABCMeta)
42class Response(object):
43    """HTTP Response data."""
44
45    @abc.abstractproperty
46    def status(self):
47        """int: The HTTP status code."""
48        raise NotImplementedError("status must be implemented.")
49
50    @abc.abstractproperty
51    def headers(self):
52        """Mapping[str, str]: The HTTP response headers."""
53        raise NotImplementedError("headers must be implemented.")
54
55    @abc.abstractproperty
56    def data(self):
57        """bytes: The response body."""
58        raise NotImplementedError("data must be implemented.")
59
60
61@six.add_metaclass(abc.ABCMeta)
62class Request(object):
63    """Interface for a callable that makes HTTP requests.
64
65    Specific transport implementations should provide an implementation of
66    this that adapts their specific request / response API.
67
68    .. automethod:: __call__
69    """
70
71    @abc.abstractmethod
72    def __call__(
73        self, url, method="GET", body=None, headers=None, timeout=None, **kwargs
74    ):
75        """Make an HTTP request.
76
77        Args:
78            url (str): The URI to be requested.
79            method (str): The HTTP method to use for the request. Defaults
80                to 'GET'.
81            body (bytes): The payload / body in HTTP request.
82            headers (Mapping[str, str]): Request headers.
83            timeout (Optional[int]): The number of seconds to wait for a
84                response from the server. If not specified or if None, the
85                transport-specific default timeout will be used.
86            kwargs: Additionally arguments passed on to the transport's
87                request method.
88
89        Returns:
90            Response: The HTTP response.
91
92        Raises:
93            google.auth.exceptions.TransportError: If any exception occurred.
94        """
95        # pylint: disable=redundant-returns-doc, missing-raises-doc
96        # (pylint doesn't play well with abstract docstrings.)
97        raise NotImplementedError("__call__ must be implemented.")
98