Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/auth/transport/_http_client.py: 44%
43 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:30 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:30 +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 adapter for http.client, for internal use only."""
17import logging
18import socket
20import six
21from six.moves import http_client
22from six.moves import urllib
24from google.auth import exceptions
25from google.auth import transport
27_LOGGER = logging.getLogger(__name__)
30class Response(transport.Response):
31 """http.client transport response adapter.
33 Args:
34 response (http.client.HTTPResponse): The raw http client response.
35 """
37 def __init__(self, response):
38 self._status = response.status
39 self._headers = {key.lower(): value for key, value in response.getheaders()}
40 self._data = response.read()
42 @property
43 def status(self):
44 return self._status
46 @property
47 def headers(self):
48 return self._headers
50 @property
51 def data(self):
52 return self._data
55class Request(transport.Request):
56 """http.client transport request adapter."""
58 def __call__(
59 self, url, method="GET", body=None, headers=None, timeout=None, **kwargs
60 ):
61 """Make an HTTP request using http.client.
63 Args:
64 url (str): The URI to be requested.
65 method (str): The HTTP method to use for the request. Defaults
66 to 'GET'.
67 body (bytes): The payload / body in HTTP request.
68 headers (Mapping): Request headers.
69 timeout (Optional(int)): The number of seconds to wait for a
70 response from the server. If not specified or if None, the
71 socket global default timeout will be used.
72 kwargs: Additional arguments passed throught to the underlying
73 :meth:`~http.client.HTTPConnection.request` method.
75 Returns:
76 Response: The HTTP response.
78 Raises:
79 google.auth.exceptions.TransportError: If any exception occurred.
80 """
81 # socket._GLOBAL_DEFAULT_TIMEOUT is the default in http.client.
82 if timeout is None:
83 timeout = socket._GLOBAL_DEFAULT_TIMEOUT
85 # http.client doesn't allow None as the headers argument.
86 if headers is None:
87 headers = {}
89 # http.client needs the host and path parts specified separately.
90 parts = urllib.parse.urlsplit(url)
91 path = urllib.parse.urlunsplit(
92 ("", "", parts.path, parts.query, parts.fragment)
93 )
95 if parts.scheme != "http":
96 raise exceptions.TransportError(
97 "http.client transport only supports the http scheme, {}"
98 "was specified".format(parts.scheme)
99 )
101 connection = http_client.HTTPConnection(parts.netloc, timeout=timeout)
103 try:
104 _LOGGER.debug("Making request: %s %s", method, url)
106 connection.request(method, path, body=body, headers=headers, **kwargs)
107 response = connection.getresponse()
108 return Response(response)
110 except (http_client.HTTPException, socket.error) as caught_exc:
111 new_exc = exceptions.TransportError(caught_exc)
112 six.raise_from(new_exc, caught_exc)
114 finally:
115 connection.close()