Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow http as an argument to base Connection class. #608

Merged
merged 2 commits into from
Feb 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions gcloud/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ class Connection(object):

Subclasses should understand only the basic types in method arguments,
however they should be capable of returning advanced types.

If no value is passed in for ``http``, a :class:`httplib2.Http` object
will be created and authorized with the ``credentials``. If not, the
``credentials`` and ``http`` need not be related.

Subclasses may seek to use the private key from ``credentials`` to sign
data.

A custom (non-``httplib2``) HTTP object must have a ``request`` method
which accepts the following arguments:

* ``uri``
* ``method``
* ``body``
* ``headers``

In addition, ``redirections`` and ``connection_type`` may be used.

Without the use of ``credentials.authorize(http)``, a custom ``http``
object will also need to be able to add a bearer token to API
requests and handle token refresh on 401 errors.

:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for this connection.

:type http: :class:`httplib2.Http` or class that defines ``request()``.
:param http: An optional HTTP object to make requests.
"""

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

def __init__(self, credentials=None):
"""Constructor for Connection.

:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for this connection.
"""
self._http = None
def __init__(self, credentials=None, http=None):
self._http = http
self._credentials = credentials

@property
Expand Down
7 changes: 7 additions & 0 deletions gcloud/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def test_ctor_explicit(self):
creds = object()
conn = self._makeOne(creds)
self.assertTrue(conn.credentials is creds)
self.assertEqual(conn._http, None)

def test_ctor_explicit_http(self):
http = object()
conn = self._makeOne(http=http)
self.assertEqual(conn.credentials, None)
self.assertTrue(conn.http is http)

def test_http_w_existing(self):
conn = self._makeOne()
Expand Down