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 adapter for http.client, for internal use only.""" |
| 16 |
| 17 import logging |
| 18 import socket |
| 19 |
| 20 from six.moves import http_client |
| 21 from six.moves import urllib |
| 22 |
| 23 from google.auth import exceptions |
| 24 from google.auth import transport |
| 25 |
| 26 _LOGGER = logging.getLogger(__name__) |
| 27 |
| 28 |
| 29 class Response(transport.Response): |
| 30 """http.client transport response adapter. |
| 31 |
| 32 Args: |
| 33 response (http.client.HTTPResponse): The raw http client response. |
| 34 """ |
| 35 def __init__(self, response): |
| 36 self._status = response.status |
| 37 self._headers = { |
| 38 key.lower(): value for key, value in response.getheaders()} |
| 39 self._data = response.read() |
| 40 |
| 41 @property |
| 42 def status(self): |
| 43 return self._status |
| 44 |
| 45 @property |
| 46 def headers(self): |
| 47 return self._headers |
| 48 |
| 49 @property |
| 50 def data(self): |
| 51 return self._data |
| 52 |
| 53 |
| 54 class Request(transport.Request): |
| 55 """http.client transport request adapter.""" |
| 56 |
| 57 def __call__(self, url, method='GET', body=None, headers=None, |
| 58 timeout=None, **kwargs): |
| 59 """Make an HTTP request using http.client. |
| 60 |
| 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. |
| 72 |
| 73 Returns: |
| 74 Response: The HTTP response. |
| 75 |
| 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 |
| 82 |
| 83 # http.client doesn't allow None as the headers argument. |
| 84 if headers is None: |
| 85 headers = {} |
| 86 |
| 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 |
| 92 if parts.scheme != 'http': |
| 93 raise exceptions.TransportError( |
| 94 'http.client transport only supports the http scheme, {}' |
| 95 'was specified'.format(parts.scheme)) |
| 96 |
| 97 connection = http_client.HTTPConnection(parts.netloc) |
| 98 |
| 99 try: |
| 100 _LOGGER.debug('Making request: %s %s', method, url) |
| 101 |
| 102 connection.request( |
| 103 method, path, body=body, headers=headers, **kwargs) |
| 104 response = connection.getresponse() |
| 105 return Response(response) |
| 106 |
| 107 except (http_client.HTTPException, socket.error) as exc: |
| 108 raise exceptions.TransportError(exc) |
| 109 |
| 110 finally: |
| 111 connection.close() |
OLD | NEW |