OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 Google Inc. |
| 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 |
| 18 as urllib3 and requests. In order to work across these libraries with different |
| 19 interfaces some abstraction is needed. |
| 20 |
| 21 This module provides two interfaces that are implemented by transport adapters |
| 22 to support HTTP libraries. :class:`Request` defines the interface expected by |
| 23 :mod:`google.auth` to make requests. :class:`Response` defines the interface |
| 24 for the return value of :class:`Request`. |
| 25 """ |
| 26 |
| 27 import abc |
| 28 |
| 29 import six |
| 30 from six.moves import http_client |
| 31 |
| 32 DEFAULT_REFRESH_STATUS_CODES = (http_client.UNAUTHORIZED,) |
| 33 """Sequence[int]: Which HTTP status code indicate that credentials should be |
| 34 refreshed and a request should be retried. |
| 35 """ |
| 36 |
| 37 DEFAULT_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) |
| 42 class 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) |
| 62 class 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__(self, url, method='GET', body=None, headers=None, |
| 73 timeout=None, **kwargs): |
| 74 """Make an HTTP request. |
| 75 |
| 76 Args: |
| 77 url (str): The URI to be requested. |
| 78 method (str): The HTTP method to use for the request. Defaults |
| 79 to 'GET'. |
| 80 body (bytes): The payload / body in HTTP request. |
| 81 headers (Mapping[str, str]): Request headers. |
| 82 timeout (Optional[int]): The number of seconds to wait for a |
| 83 response from the server. If not specified or if None, the |
| 84 transport-specific default timeout will be used. |
| 85 kwargs: Additionally arguments passed on to the transport's |
| 86 request method. |
| 87 |
| 88 Returns: |
| 89 Response: The HTTP response. |
| 90 |
| 91 Raises: |
| 92 google.auth.exceptions.TransportError: If any exception occurred. |
| 93 """ |
| 94 # pylint: disable=redundant-returns-doc, missing-raises-doc |
| 95 # (pylint doesn't play well with abstract docstrings.) |
| 96 raise NotImplementedError('__call__ must be implemented.') |
OLD | NEW |