googleapiclient.http: guard when importing ssl
Fix #191 by adding a guard when importing `ssl`, and replace all direct
references to `SSLError` (the sole member of `ssl` being used) with a
shim, `_ssl_SSLError`. It is prefixed with a '_' to caution users
against relying on the name when importing `googleclient.http`.
`httplib2` also takes a similar approach (see `ssl_SSLError` in
`httplib2/__init__.py`).
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
index ed074cb..f9d6a55 100644
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -38,11 +38,18 @@
import os
import random
import socket
-import ssl
import sys
import time
import uuid
+# TODO(issue 221): Remove this conditional import jibbajabba.
+try:
+ import ssl
+except ImportError:
+ _ssl_SSLError = object()
+else:
+ _ssl_SSLError = ssl.SSLError
+
from email.generator import Generator
from email.mime.multipart import MIMEMultipart
from email.mime.nonmultipart import MIMENonMultipart
@@ -146,7 +153,7 @@
exception = None
resp, content = http.request(uri, method, *args, **kwargs)
# Retry on SSL errors and socket timeout errors.
- except ssl.SSLError as ssl_error:
+ except _ssl_SSLError as ssl_error:
exception = ssl_error
except socket.error as socket_error:
# errno's contents differ by platform, so we have to match by name.