| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
"""Transport adapter for httplib2.""" |
| 16 |
|
| 17 |
from __future__ import absolute_import |
| 18 |
|
| 19 |
import http.client |
| 20 |
import logging |
| 21 |
|
| 22 |
from google.auth import exceptions, transport |
| 23 |
import httplib2 |
| 24 |
|
| 25 |
_LOGGER = logging.getLogger(__name__) |
| 26 |
|
| 27 |
_STREAM_PROPERTIES = ("read", "seek", "tell") |
| 28 |
|
| 29 |
|
| 30 |
class _Response(transport.Response): |
| 31 |
"""httplib2 transport response adapter. |
| 32 |
|
| 33 |
Args: |
| 34 |
response (httplib2.Response): The raw httplib2 response. |
| 35 |
data (bytes): The response body. |
| 36 |
""" |
| 37 |
|
| 38 |
def __init__(self, response, data): |
| 39 |
self._response = response |
| 40 |
self._data = data |
| 41 |
|
| 42 |
@property |
| 43 |
def status(self): |
| 44 |
"""int: The HTTP status code.""" |
| 45 |
return self._response.status |
| 46 |
|
| 47 |
@property |
| 48 |
def headers(self): |
| 49 |
"""Mapping[str, str]: The HTTP response headers.""" |
| 50 |
return dict(self._response) |
| 51 |
|
| 52 |
@property |
| 53 |
def data(self): |
| 54 |
"""bytes: The response body.""" |
| 55 |
return self._data |
| 56 |
|
| 57 |
|
| 58 |
class Request(transport.Request): |
| 59 |
"""httplib2 request adapter. |
| 60 |
|
| 61 |
This class is used internally for making requests using various transports |
| 62 |
in a consistent way. If you use :class:`AuthorizedHttp` you do not need |
| 63 |
to construct or use this class directly. |
| 64 |
|
| 65 |
This class can be useful if you want to manually refresh a |
| 66 |
:class:`~google.auth.credentials.Credentials` instance:: |
| 67 |
|
| 68 |
import google_auth_httplib2 |
| 69 |
import httplib2 |
| 70 |
|
| 71 |
http = httplib2.Http() |
| 72 |
request = google_auth_httplib2.Request(http) |
| 73 |
|
| 74 |
credentials.refresh(request) |
| 75 |
|
| 76 |
Args: |
| 77 |
http (httplib2.Http): The underlying http object to use to make |
| 78 |
requests. |
| 79 |
""" |
| 80 |
|
| 81 |
def __init__(self, http): |
| 82 |
self.http = http |
| 83 |
|
| 84 |
def __call__( |
| 85 |
self, url, method="GET", body=None, headers=None, timeout=None, **kwargs |
| 86 |
): |
| 87 |
"""Make an HTTP request using httplib2. |
| 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. This is ignored by httplib2 and will |
| 97 |
issue a warning. |
| 98 |
kwargs: Additional arguments passed throught to the underlying |
| 99 |
:meth:`httplib2.Http.request` method. |
| 100 |
|
| 101 |
Returns: |
| 102 |
google.auth.transport.Response: The HTTP response. |
| 103 |
|
| 104 |
Raises: |
| 105 |
google.auth.exceptions.TransportError: If any exception occurred. |
| 106 |
""" |
| 107 |
if timeout is not None: |
| 108 |
_LOGGER.warning( |
| 109 |
"httplib2 transport does not support per-request timeout. " |
| 110 |
"Set the timeout when constructing the httplib2.Http instance." |
| 111 |
) |
| 112 |
|
| 113 |
try: |
| 114 |
_LOGGER.debug("Making request: %s %s", method, url) |
| 115 |
response, data = self.http.request( |
| 116 |
url, method=method, body=body, headers=headers, **kwargs |
| 117 |
) |
| 118 |
return _Response(response, data) |
| 119 |
|
| 120 |
|
| 121 |
except (httplib2.HttpLib2Error, http.client.HTTPException) as exc: |
| 122 |
raise exceptions.TransportError(exc) |
| 123 |
|
| 124 |
|
| 125 |
def _make_default_http(): |
| 126 |
"""Returns a default httplib2.Http instance.""" |
| 127 |
return httplib2.Http() |
| 128 |
|
| 129 |
|
| 130 |
class AuthorizedHttp(object): |
| 131 |
"""A httplib2 HTTP class with credentials. |
| 132 |
|
| 133 |
This class is used to perform requests to API endpoints that require |
| 134 |
authorization:: |
| 135 |
|
| 136 |
from google.auth.transport._httplib2 import AuthorizedHttp |
| 137 |
|
| 138 |
authed_http = AuthorizedHttp(credentials) |
| 139 |
|
| 140 |
response = authed_http.request( |
| 141 |
'https://www.googleapis.com/storage/v1/b') |
| 142 |
|
| 143 |
This class implements :meth:`request` in the same way as |
| 144 |
:class:`httplib2.Http` and can usually be used just like any other |
| 145 |
instance of :class:`httplib2.Http`. |
| 146 |
|
| 147 |
The underlying :meth:`request` implementation handles adding the |
| 148 |
credentials' headers to the request and refreshing credentials as needed. |
| 149 |
""" |
| 150 |
|
| 151 |
def __init__( |
| 152 |
self, |
| 153 |
credentials, |
| 154 |
http=None, |
| 155 |
refresh_status_codes=transport.DEFAULT_REFRESH_STATUS_CODES, |
| 156 |
max_refresh_attempts=transport.DEFAULT_MAX_REFRESH_ATTEMPTS, |
| 157 |
): |
| 158 |
""" |
| 159 |
Args: |
| 160 |
credentials (google.auth.credentials.Credentials): The credentials |
| 161 |
to add to the request. |
| 162 |
http (httplib2.Http): The underlying HTTP object to |
| 163 |
use to make requests. If not specified, a |
| 164 |
:class:`httplib2.Http` instance will be constructed. |
| 165 |
refresh_status_codes (Sequence[int]): Which HTTP status codes |
| 166 |
indicate that credentials should be refreshed and the request |
| 167 |
should be retried. |
| 168 |
max_refresh_attempts (int): The maximum number of times to attempt |
| 169 |
to refresh the credentials and retry the request. |
| 170 |
""" |
| 171 |
|
| 172 |
if http is None: |
| 173 |
http = _make_default_http() |
| 174 |
|
| 175 |
self.http = http |
| 176 |
self.credentials = credentials |
| 177 |
self._refresh_status_codes = refresh_status_codes |
| 178 |
self._max_refresh_attempts = max_refresh_attempts |
| 179 |
|
| 180 |
|
| 181 |
self._request = Request(self.http) |
| 182 |
|
| 183 |
def close(self): |
| 184 |
"""Calls httplib2's Http.close""" |
| 185 |
self.http.close() |
| 186 |
|
| 187 |
def request( |
| 188 |
self, |
| 189 |
uri, |
| 190 |
method="GET", |
| 191 |
body=None, |
| 192 |
headers=None, |
| 193 |
redirections=httplib2.DEFAULT_MAX_REDIRECTS, |
| 194 |
connection_type=None, |
| 195 |
**kwargs |
| 196 |
): |
| 197 |
"""Implementation of httplib2's Http.request.""" |
| 198 |
|
| 199 |
_credential_refresh_attempt = kwargs.pop("_credential_refresh_attempt", 0) |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
request_headers = headers.copy() if headers is not None else {} |
| 204 |
|
| 205 |
self.credentials.before_request(self._request, method, uri, request_headers) |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
body_stream_position = None |
| 210 |
if all(getattr(body, stream_prop, None) for stream_prop in _STREAM_PROPERTIES): |
| 211 |
body_stream_position = body.tell() |
| 212 |
|
| 213 |
|
| 214 |
response, content = self.http.request( |
| 215 |
uri, |
| 216 |
method, |
| 217 |
body=body, |
| 218 |
headers=request_headers, |
| 219 |
redirections=redirections, |
| 220 |
connection_type=connection_type, |
| 221 |
**kwargs |
| 222 |
) |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if ( |
| 230 |
response.status in self._refresh_status_codes |
| 231 |
and _credential_refresh_attempt < self._max_refresh_attempts |
| 232 |
): |
| 233 |
_LOGGER.info( |
| 234 |
"Refreshing credentials due to a %s response. Attempt %s/%s.", |
| 235 |
response.status, |
| 236 |
_credential_refresh_attempt + 1, |
| 237 |
self._max_refresh_attempts, |
| 238 |
) |
| 239 |
|
| 240 |
self.credentials.refresh(self._request) |
| 241 |
|
| 242 |
|
| 243 |
if body_stream_position is not None: |
| 244 |
body.seek(body_stream_position) |
| 245 |
|
| 246 |
|
| 247 |
return self.request( |
| 248 |
uri, |
| 249 |
method, |
| 250 |
body=body, |
| 251 |
headers=headers, |
| 252 |
redirections=redirections, |
| 253 |
connection_type=connection_type, |
| 254 |
_credential_refresh_attempt=_credential_refresh_attempt + 1, |
| 255 |
**kwargs |
| 256 |
) |
| 257 |
|
| 258 |
return response, content |
| 259 |
|
| 260 |
def add_certificate(self, key, cert, domain, password=None): |
| 261 |
"""Proxy to httplib2.Http.add_certificate.""" |
| 262 |
self.http.add_certificate(key, cert, domain, password=password) |
| 263 |
|
| 264 |
@property |
| 265 |
def connections(self): |
| 266 |
"""Proxy to httplib2.Http.connections.""" |
| 267 |
return self.http.connections |
| 268 |
|
| 269 |
@connections.setter |
| 270 |
def connections(self, value): |
| 271 |
"""Proxy to httplib2.Http.connections.""" |
| 272 |
self.http.connections = value |
| 273 |
|
| 274 |
@property |
| 275 |
def follow_redirects(self): |
| 276 |
"""Proxy to httplib2.Http.follow_redirects.""" |
| 277 |
return self.http.follow_redirects |
| 278 |
|
| 279 |
@follow_redirects.setter |
| 280 |
def follow_redirects(self, value): |
| 281 |
"""Proxy to httplib2.Http.follow_redirects.""" |
| 282 |
self.http.follow_redirects = value |
| 283 |
|
| 284 |
@property |
| 285 |
def timeout(self): |
| 286 |
"""Proxy to httplib2.Http.timeout.""" |
| 287 |
return self.http.timeout |
| 288 |
|
| 289 |
@timeout.setter |
| 290 |
def timeout(self, value): |
| 291 |
"""Proxy to httplib2.Http.timeout.""" |
| 292 |
self.http.timeout = value |
| 293 |
|
| 294 |
@property |
| 295 |
def redirect_codes(self): |
| 296 |
"""Proxy to httplib2.Http.redirect_codes.""" |
| 297 |
return self.http.redirect_codes |
| 298 |
|
| 299 |
@redirect_codes.setter |
| 300 |
def redirect_codes(self, value): |
| 301 |
"""Proxy to httplib2.Http.redirect_codes.""" |
| 302 |
self.http.redirect_codes = value |