aboutsummaryrefslogtreecommitdiff
path: root/google/auth/transport
diff options
context:
space:
mode:
Diffstat (limited to 'google/auth/transport')
-rw-r--r--google/auth/transport/__init__.py12
-rw-r--r--google/auth/transport/_aiohttp_requests.py5
-rw-r--r--google/auth/transport/_http_client.py12
-rw-r--r--google/auth/transport/_mtls_helper.py6
-rw-r--r--google/auth/transport/grpc.py16
-rw-r--r--google/auth/transport/mtls.py6
-rw-r--r--google/auth/transport/requests.py19
-rw-r--r--google/auth/transport/urllib3.py19
8 files changed, 60 insertions, 35 deletions
diff --git a/google/auth/transport/__init__.py b/google/auth/transport/__init__.py
index d1b035d..374e7b4 100644
--- a/google/auth/transport/__init__.py
+++ b/google/auth/transport/__init__.py
@@ -25,9 +25,11 @@ for the return value of :class:`Request`.
"""
import abc
-import http.client
-DEFAULT_REFRESH_STATUS_CODES = (http.client.UNAUTHORIZED,)
+import six
+from six.moves import http_client
+
+DEFAULT_REFRESH_STATUS_CODES = (http_client.UNAUTHORIZED,)
"""Sequence[int]: Which HTTP status code indicate that credentials should be
refreshed and a request should be retried.
"""
@@ -36,7 +38,8 @@ DEFAULT_MAX_REFRESH_ATTEMPTS = 2
"""int: How many times to refresh the credentials and retry a request."""
-class Response(object, metaclass=abc.ABCMeta):
+@six.add_metaclass(abc.ABCMeta)
+class Response(object):
"""HTTP Response data."""
@abc.abstractproperty
@@ -55,7 +58,8 @@ class Response(object, metaclass=abc.ABCMeta):
raise NotImplementedError("data must be implemented.")
-class Request(object, metaclass=abc.ABCMeta):
+@six.add_metaclass(abc.ABCMeta)
+class Request(object):
"""Interface for a callable that makes HTTP requests.
Specific transport implementations should provide an implementation of
diff --git a/google/auth/transport/_aiohttp_requests.py b/google/auth/transport/_aiohttp_requests.py
index ee94043..ab7dfef 100644
--- a/google/auth/transport/_aiohttp_requests.py
+++ b/google/auth/transport/_aiohttp_requests.py
@@ -24,6 +24,7 @@ import asyncio
import functools
import aiohttp
+import six
import urllib3
from google.auth import exceptions
@@ -190,11 +191,11 @@ class Request(transport.Request):
except aiohttp.ClientError as caught_exc:
new_exc = exceptions.TransportError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
except asyncio.TimeoutError as caught_exc:
new_exc = exceptions.TransportError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
class AuthorizedSession(aiohttp.ClientSession):
diff --git a/google/auth/transport/_http_client.py b/google/auth/transport/_http_client.py
index 679087f..c153763 100644
--- a/google/auth/transport/_http_client.py
+++ b/google/auth/transport/_http_client.py
@@ -14,10 +14,12 @@
"""Transport adapter for http.client, for internal use only."""
-import http.client
import logging
import socket
-import urllib
+
+import six
+from six.moves import http_client
+from six.moves import urllib
from google.auth import exceptions
from google.auth import transport
@@ -96,7 +98,7 @@ class Request(transport.Request):
"was specified".format(parts.scheme)
)
- connection = http.client.HTTPConnection(parts.netloc, timeout=timeout)
+ connection = http_client.HTTPConnection(parts.netloc, timeout=timeout)
try:
_LOGGER.debug("Making request: %s %s", method, url)
@@ -105,9 +107,9 @@ class Request(transport.Request):
response = connection.getresponse()
return Response(response)
- except (http.client.HTTPException, socket.error) as caught_exc:
+ except (http_client.HTTPException, socket.error) as caught_exc:
new_exc = exceptions.TransportError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
finally:
connection.close()
diff --git a/google/auth/transport/_mtls_helper.py b/google/auth/transport/_mtls_helper.py
index 1b9b9c2..4dccb10 100644
--- a/google/auth/transport/_mtls_helper.py
+++ b/google/auth/transport/_mtls_helper.py
@@ -20,6 +20,8 @@ from os import path
import re
import subprocess
+import six
+
from google.auth import exceptions
CONTEXT_AWARE_METADATA_PATH = "~/.secureConnect/context_aware_metadata.json"
@@ -80,7 +82,7 @@ def _read_dca_metadata_file(metadata_path):
metadata = json.load(f)
except ValueError as caught_exc:
new_exc = exceptions.ClientCertError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
return metadata
@@ -108,7 +110,7 @@ def _run_cert_provider_command(command, expect_encrypted_key=False):
stdout, stderr = process.communicate()
except OSError as caught_exc:
new_exc = exceptions.ClientCertError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
# Check cert provider command execution error.
if process.returncode != 0:
diff --git a/google/auth/transport/grpc.py b/google/auth/transport/grpc.py
index 160dc94..c47cb3d 100644
--- a/google/auth/transport/grpc.py
+++ b/google/auth/transport/grpc.py
@@ -19,6 +19,8 @@ from __future__ import absolute_import
import logging
import os
+import six
+
from google.auth import environment_vars
from google.auth import exceptions
from google.auth.transport import _mtls_helper
@@ -27,11 +29,13 @@ from google.oauth2 import service_account
try:
import grpc
except ImportError as caught_exc: # pragma: NO COVER
- new_exc = ImportError(
- "gRPC is not installed, please install the grpcio package "
- "to use the gRPC transport."
+ six.raise_from(
+ ImportError(
+ "gRPC is not installed, please install the grpcio package "
+ "to use the gRPC transport."
+ ),
+ caught_exc,
)
- raise new_exc from caught_exc
_LOGGER = logging.getLogger(__name__)
@@ -84,7 +88,7 @@ class AuthMetadataPlugin(grpc.AuthMetadataPlugin):
self._request, context.method_name, context.service_url, headers
)
- return list(headers.items())
+ return list(six.iteritems(headers))
def __call__(self, context, callback):
"""Passes authorization metadata into the given callback.
@@ -333,7 +337,7 @@ class SslCredentials:
)
except exceptions.ClientCertError as caught_exc:
new_exc = exceptions.MutualTLSChannelError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
else:
self._ssl_credentials = grpc.ssl_channel_credentials()
diff --git a/google/auth/transport/mtls.py b/google/auth/transport/mtls.py
index c570761..b40bfbe 100644
--- a/google/auth/transport/mtls.py
+++ b/google/auth/transport/mtls.py
@@ -14,6 +14,8 @@
"""Utilites for mutual TLS."""
+import six
+
from google.auth import exceptions
from google.auth.transport import _mtls_helper
@@ -51,7 +53,7 @@ def default_client_cert_source():
_, cert_bytes, key_bytes = _mtls_helper.get_client_cert_and_key()
except (OSError, RuntimeError, ValueError) as caught_exc:
new_exc = exceptions.MutualTLSChannelError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
return cert_bytes, key_bytes
@@ -96,7 +98,7 @@ def default_client_encrypted_cert_source(cert_path, key_path):
key_file.write(key_bytes)
except (exceptions.ClientCertError, OSError) as caught_exc:
new_exc = exceptions.MutualTLSChannelError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
return cert_path, key_path, passphrase_bytes
diff --git a/google/auth/transport/requests.py b/google/auth/transport/requests.py
index 2cb6942..817176b 100644
--- a/google/auth/transport/requests.py
+++ b/google/auth/transport/requests.py
@@ -25,16 +25,21 @@ import time
try:
import requests
except ImportError as caught_exc: # pragma: NO COVER
- new_exc = ImportError(
- "The requests library is not installed, please install the "
- "requests package to use the requests transport."
+ import six
+
+ six.raise_from(
+ ImportError(
+ "The requests library is not installed, please install the "
+ "requests package to use the requests transport."
+ ),
+ caught_exc,
)
- raise new_exc from caught_exc
import requests.adapters # pylint: disable=ungrouped-imports
import requests.exceptions # pylint: disable=ungrouped-imports
from requests.packages.urllib3.util.ssl_ import (
create_urllib3_context,
) # pylint: disable=ungrouped-imports
+import six # pylint: disable=ungrouped-imports
from google.auth import environment_vars
from google.auth import exceptions
@@ -181,7 +186,7 @@ class Request(transport.Request):
return _Response(response)
except requests.exceptions.RequestException as caught_exc:
new_exc = exceptions.TransportError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
class _MutualTlsAdapter(requests.adapters.HTTPAdapter):
@@ -391,7 +396,7 @@ class AuthorizedSession(requests.Session):
import OpenSSL
except ImportError as caught_exc:
new_exc = exceptions.MutualTLSChannelError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
try:
(
@@ -411,7 +416,7 @@ class AuthorizedSession(requests.Session):
OpenSSL.crypto.Error,
) as caught_exc:
new_exc = exceptions.MutualTLSChannelError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
def request(
self,
diff --git a/google/auth/transport/urllib3.py b/google/auth/transport/urllib3.py
index aa7188c..6a2504d 100644
--- a/google/auth/transport/urllib3.py
+++ b/google/auth/transport/urllib3.py
@@ -34,11 +34,16 @@ except ImportError: # pragma: NO COVER
try:
import urllib3
except ImportError as caught_exc: # pragma: NO COVER
- new_exc = ImportError(
- "The urllib3 library is not installed, please install the "
- "urllib3 package to use the urllib3 transport."
+ import six
+
+ six.raise_from(
+ ImportError(
+ "The urllib3 library is not installed, please install the "
+ "urllib3 package to use the urllib3 transport."
+ ),
+ caught_exc,
)
- raise new_exc from caught_exc
+import six
import urllib3.exceptions # pylint: disable=ungrouped-imports
from google.auth import environment_vars
@@ -137,7 +142,7 @@ class Request(transport.Request):
return _Response(response)
except urllib3.exceptions.HTTPError as caught_exc:
new_exc = exceptions.TransportError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
def _make_default_http():
@@ -329,7 +334,7 @@ class AuthorizedHttp(urllib3.request.RequestMethods):
import OpenSSL
except ImportError as caught_exc:
new_exc = exceptions.MutualTLSChannelError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
try:
found_cert_key, cert, key = transport._mtls_helper.get_client_cert_and_key(
@@ -346,7 +351,7 @@ class AuthorizedHttp(urllib3.request.RequestMethods):
OpenSSL.crypto.Error,
) as caught_exc:
new_exc = exceptions.MutualTLSChannelError(caught_exc)
- raise new_exc from caught_exc
+ six.raise_from(new_exc, caught_exc)
if self._has_user_provided_http:
self._has_user_provided_http = False