Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/auth/transport/__init__.py: 83%

24 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:25 +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. 

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 

32TOO_MANY_REQUESTS = 429 # Python 2.7 six is missing this status code. 

33 

34DEFAULT_RETRYABLE_STATUS_CODES = ( 

35 http_client.INTERNAL_SERVER_ERROR, 

36 http_client.SERVICE_UNAVAILABLE, 

37 http_client.REQUEST_TIMEOUT, 

38 TOO_MANY_REQUESTS, 

39) 

40"""Sequence[int]: HTTP status codes indicating a request can be retried. 

41""" 

42 

43 

44DEFAULT_REFRESH_STATUS_CODES = (http_client.UNAUTHORIZED,) 

45"""Sequence[int]: Which HTTP status code indicate that credentials should be 

46refreshed. 

47""" 

48 

49DEFAULT_MAX_REFRESH_ATTEMPTS = 2 

50"""int: How many times to refresh the credentials and retry a request.""" 

51 

52 

53@six.add_metaclass(abc.ABCMeta) 

54class Response(object): 

55 """HTTP Response data.""" 

56 

57 @abc.abstractproperty 

58 def status(self): 

59 """int: The HTTP status code.""" 

60 raise NotImplementedError("status must be implemented.") 

61 

62 @abc.abstractproperty 

63 def headers(self): 

64 """Mapping[str, str]: The HTTP response headers.""" 

65 raise NotImplementedError("headers must be implemented.") 

66 

67 @abc.abstractproperty 

68 def data(self): 

69 """bytes: The response body.""" 

70 raise NotImplementedError("data must be implemented.") 

71 

72 

73@six.add_metaclass(abc.ABCMeta) 

74class Request(object): 

75 """Interface for a callable that makes HTTP requests. 

76 

77 Specific transport implementations should provide an implementation of 

78 this that adapts their specific request / response API. 

79 

80 .. automethod:: __call__ 

81 """ 

82 

83 @abc.abstractmethod 

84 def __call__( 

85 self, url, method="GET", body=None, headers=None, timeout=None, **kwargs 

86 ): 

87 """Make an HTTP request. 

88 

89 Args: 

90 url (str): The URI to be requested. 

91 method (str): The HTTP method to use for the request. Defaults 

92 to 'GET'. 

93 body (bytes): The payload / body in HTTP request. 

94 headers (Mapping[str, str]): Request headers. 

95 timeout (Optional[int]): The number of seconds to wait for a 

96 response from the server. If not specified or if None, the 

97 transport-specific default timeout will be used. 

98 kwargs: Additionally arguments passed on to the transport's 

99 request method. 

100 

101 Returns: 

102 Response: The HTTP response. 

103 

104 Raises: 

105 google.auth.exceptions.TransportError: If any exception occurred. 

106 """ 

107 # pylint: disable=redundant-returns-doc, missing-raises-doc 

108 # (pylint doesn't play well with abstract docstrings.) 

109 raise NotImplementedError("__call__ must be implemented.")