Skip to content

Commit 06078cb

Browse files
committed
Merge pull request #608 from dhermes/fix-551
Allow http as an argument to base Connection class.
2 parents c88ba72 + 5921313 commit 06078cb

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

gcloud/connection.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ class Connection(object):
2424
2525
Subclasses should understand only the basic types in method arguments,
2626
however they should be capable of returning advanced types.
27+
28+
If no value is passed in for ``http``, a :class:`httplib2.Http` object
29+
will be created and authorized with the ``credentials``. If not, the
30+
``credentials`` and ``http`` need not be related.
31+
32+
Subclasses may seek to use the private key from ``credentials`` to sign
33+
data.
34+
35+
A custom (non-``httplib2``) HTTP object must have a ``request`` method
36+
which accepts the following arguments:
37+
38+
* ``uri``
39+
* ``method``
40+
* ``body``
41+
* ``headers``
42+
43+
In addition, ``redirections`` and ``connection_type`` may be used.
44+
45+
Without the use of ``credentials.authorize(http)``, a custom ``http``
46+
object will also need to be able to add a bearer token to API
47+
requests and handle token refresh on 401 errors.
48+
49+
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
50+
:class:`NoneType`
51+
:param credentials: The OAuth2 Credentials to use for this connection.
52+
53+
:type http: :class:`httplib2.Http` or class that defines ``request()``.
54+
:param http: An optional HTTP object to make requests.
2755
"""
2856

2957
API_BASE_URL = 'https://www.googleapis.com'
@@ -35,14 +63,8 @@ class Connection(object):
3563
USER_AGENT = "gcloud-python/{0}".format(get_distribution('gcloud').version)
3664
"""The user agent for gcloud-python requests."""
3765

38-
def __init__(self, credentials=None):
39-
"""Constructor for Connection.
40-
41-
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
42-
:class:`NoneType`
43-
:param credentials: The OAuth2 Credentials to use for this connection.
44-
"""
45-
self._http = None
66+
def __init__(self, credentials=None, http=None):
67+
self._http = http
4668
self._credentials = credentials
4769

4870
@property

gcloud/test_connection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ def test_ctor_explicit(self):
3232
creds = object()
3333
conn = self._makeOne(creds)
3434
self.assertTrue(conn.credentials is creds)
35+
self.assertEqual(conn._http, None)
36+
37+
def test_ctor_explicit_http(self):
38+
http = object()
39+
conn = self._makeOne(http=http)
40+
self.assertEqual(conn.credentials, None)
41+
self.assertTrue(conn.http is http)
3542

3643
def test_http_w_existing(self):
3744
conn = self._makeOne()

0 commit comments

Comments
 (0)