Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/auth/transport/__init__.py: 80%
20 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
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.
15"""Transport - HTTP client library support.
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.
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"""
27import abc
28import http.client as http_client
30DEFAULT_RETRYABLE_STATUS_CODES = (
31 http_client.INTERNAL_SERVER_ERROR,
32 http_client.SERVICE_UNAVAILABLE,
33 http_client.REQUEST_TIMEOUT,
34 http_client.TOO_MANY_REQUESTS,
35)
36"""Sequence[int]: HTTP status codes indicating a request can be retried.
37"""
40DEFAULT_REFRESH_STATUS_CODES = (http_client.UNAUTHORIZED,)
41"""Sequence[int]: Which HTTP status code indicate that credentials should be
42refreshed.
43"""
45DEFAULT_MAX_REFRESH_ATTEMPTS = 2
46"""int: How many times to refresh the credentials and retry a request."""
49class Response(metaclass=abc.ABCMeta):
50 """HTTP Response data."""
52 @abc.abstractproperty
53 def status(self):
54 """int: The HTTP status code."""
55 raise NotImplementedError("status must be implemented.")
57 @abc.abstractproperty
58 def headers(self):
59 """Mapping[str, str]: The HTTP response headers."""
60 raise NotImplementedError("headers must be implemented.")
62 @abc.abstractproperty
63 def data(self):
64 """bytes: The response body."""
65 raise NotImplementedError("data must be implemented.")
68class Request(metaclass=abc.ABCMeta):
69 """Interface for a callable that makes HTTP requests.
71 Specific transport implementations should provide an implementation of
72 this that adapts their specific request / response API.
74 .. automethod:: __call__
75 """
77 @abc.abstractmethod
78 def __call__(
79 self, url, method="GET", body=None, headers=None, timeout=None, **kwargs
80 ):
81 """Make an HTTP request.
83 Args:
84 url (str): The URI to be requested.
85 method (str): The HTTP method to use for the request. Defaults
86 to 'GET'.
87 body (bytes): The payload / body in HTTP request.
88 headers (Mapping[str, str]): Request headers.
89 timeout (Optional[int]): The number of seconds to wait for a
90 response from the server. If not specified or if None, the
91 transport-specific default timeout will be used.
92 kwargs: Additionally arguments passed on to the transport's
93 request method.
95 Returns:
96 Response: The HTTP response.
98 Raises:
99 google.auth.exceptions.TransportError: If any exception occurred.
100 """
101 # pylint: disable=redundant-returns-doc, missing-raises-doc
102 # (pylint doesn't play well with abstract docstrings.)
103 raise NotImplementedError("__call__ must be implemented.")