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