google.auth.transport.urllib3 module¶
Transport adapter for urllib3.
-
class
Request(http)[source]¶ Bases:
google.auth.transport.Requesturllib3 request adapter.
This class is used internally for making requests using various transports in a consistent way. If you use
AuthorizedHttpyou do not need to construct or use this class directly.This class can be useful if you want to manually refresh a
Credentialsinstance:import google.auth.transport.urllib3 import urllib3 http = urllib3.PoolManager() request = google.auth.transport.urllib3.Request(http) credentials.refresh(request)
- Parameters
http (urllib3.request.RequestMethods) – An instance of any urllib3 class that implements
RequestMethods, usuallyurllib3.PoolManager.
-
__call__(url, method='GET', body=None, headers=None, timeout=None, **kwargs)[source]¶ Make an HTTP request using urllib3.
- Parameters
url (str) – The URI to be requested.
method (str) – The HTTP method to use for the request. Defaults to ‘GET’.
body (bytes) – The payload / body in HTTP request.
timeout (
Optional[int]) – The number of seconds to wait for a response from the server. If not specified or if None, the urllib3 default timeout will be used.kwargs – Additional arguments passed throught to the underlying urllib3
urlopen()method.
- Returns
The HTTP response.
- Return type
- Raises
google.auth.exceptions.TransportError – If any exception occurred.
-
class
AuthorizedHttp(credentials, http=None, refresh_status_codes=(<HTTPStatus.UNAUTHORIZED: 401>, ), max_refresh_attempts=2)[source]¶ Bases:
urllib3.request.RequestMethodsA urllib3 HTTP class with credentials.
This class is used to perform requests to API endpoints that require authorization:
from google.auth.transport.urllib3 import AuthorizedHttp authed_http = AuthorizedHttp(credentials) response = authed_http.request( 'GET', 'https://www.googleapis.com/storage/v1/b')
This class implements
urllib3.request.RequestMethodsand can be used just like any otherurllib3.PoolManager.The underlying
urlopen()implementation handles adding the credentials’ headers to the request and refreshing credentials as needed.- Parameters
credentials (google.auth.credentials.Credentials) – The credentials to add to the request.
http (urllib3.PoolManager) – The underlying HTTP object to use to make requests. If not specified, a
urllib3.PoolManagerinstance will be constructed with sane defaults.refresh_status_codes (
Sequence[int]) – Which HTTP status codes indicate that credentials should be refreshed and the request should be retried.max_refresh_attempts (int) – The maximum number of times to attempt to refresh the credentials and retry the request.
-
urlopen(method, url, body=None, headers=None, **kwargs)[source]¶ Implementation of urllib3’s urlopen.
-
property
headers¶ Proxy to
self.http.
-
request(method, url, fields=None, headers=None, **urlopen_kw)¶ Make a request using
urlopen()with the appropriate encoding offieldsbased on themethodused.This is a convenience method that requires the least amount of manual effort. It can be used in most situations, while still having the option to drop down to more specific methods when necessary, such as
request_encode_url(),request_encode_body(), or even the lowest levelurlopen().
-
request_encode_body(method, url, fields=None, headers=None, encode_multipart=True, multipart_boundary=None, **urlopen_kw)¶ Make a request using
urlopen()with thefieldsencoded in the body. This is useful for request methods like POST, PUT, PATCH, etc.When
encode_multipart=True(default), thenurllib3.filepost.encode_multipart_formdata()is used to encode the payload with the appropriate content type. Otherwiseurllib.urlencode()is used with the ‘application/x-www-form-urlencoded’ content type.Multipart encoding must be used when posting files, and it’s reasonably safe to use it in other times too. However, it may break request signing, such as with OAuth.
Supports an optional
fieldsparameter of key/value strings AND key/filetuple. A filetuple is a (filename, data, MIME type) tuple where the MIME type is optional. For example:fields = { 'foo': 'bar', 'fakefile': ('foofile.txt', 'contents of foofile'), 'realfile': ('barfile.txt', open('realfile').read()), 'typedfile': ('bazfile.bin', open('bazfile').read(), 'image/jpeg'), 'nonamefile': 'contents of nonamefile field', }
When uploading a file, providing a filename (the first parameter of the tuple) is optional but recommended to best mimic behavior of browsers.
Note that if
headersare supplied, the ‘Content-Type’ header will be overwritten because it depends on the dynamic random boundary string which is used to compose the body of the request. The random boundary string can be explicitly set with themultipart_boundaryparameter.