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

#741: hoist get_scoped_connection into connection #749

Merged
merged 4 commits into from
Mar 24, 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
18 changes: 18 additions & 0 deletions gcloud/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import httplib2

from gcloud.credentials import get_credentials
from gcloud.exceptions import make_exception


Expand Down Expand Up @@ -297,3 +298,20 @@ def api_request(self, method, path, query_params=None,
return json.loads(content)

return content


def get_scoped_connection(klass, scopes):
"""Create a scoped connection to GCloud.

:type klass: subclass of :class:`gcloud.connection.Connection`
:param klass: the specific ``Connection`` class to instantiate.

:type scopes: list of URLs
:param scopes: the effective service auth scopes for the connection.

:rtype: instance of ``klass``
:returns: A connection defined with the proper credentials.
"""
implicit_credentials = get_credentials()
scoped_credentials = implicit_credentials.create_scoped(scopes)
return klass(credentials=scoped_credentials)
6 changes: 2 additions & 4 deletions gcloud/datastore/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from gcloud._helpers import _app_engine_id
from gcloud._helpers import _compute_engine_id
from gcloud._helpers import _lazy_property_deco
from gcloud import credentials
from gcloud.connection import get_scoped_connection
from gcloud.datastore.connection import Connection


Expand Down Expand Up @@ -126,9 +126,7 @@ def get_connection():
:rtype: :class:`gcloud.datastore.connection.Connection`
:returns: A connection defined with the proper credentials.
"""
implicit_credentials = credentials.get_credentials()
scoped_credentials = implicit_credentials.create_scoped(SCOPE)
return Connection(credentials=scoped_credentials)
return get_scoped_connection(Connection, SCOPE)


def set_default_connection(connection=None):
Expand Down
2 changes: 2 additions & 0 deletions gcloud/datastore/test__implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ def _callFUT(self):

def test_it(self):
from gcloud import credentials
from gcloud.datastore._implicit_environ import SCOPE
from gcloud.datastore.connection import Connection
from gcloud.test_credentials import _Client
from gcloud._testing import _Monkey
Expand All @@ -314,6 +315,7 @@ def test_it(self):
found = self._callFUT()
self.assertTrue(isinstance(found, Connection))
self.assertTrue(found._credentials is client._signed)
self.assertEqual(found._credentials._scopes, SCOPE)
self.assertTrue(client._get_app_default_called)


Expand Down
5 changes: 2 additions & 3 deletions gcloud/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from gcloud import credentials
from gcloud._helpers import get_default_project
from gcloud._helpers import set_default_project
from gcloud.connection import get_scoped_connection
from gcloud.storage import _implicit_environ
from gcloud.storage._implicit_environ import get_default_bucket
from gcloud.storage._implicit_environ import get_default_connection
Expand Down Expand Up @@ -133,6 +134,4 @@ def get_connection():
:rtype: :class:`gcloud.storage.connection.Connection`
:returns: A connection defined with the proper credentials.
"""
implicit_credentials = credentials.get_credentials()
scoped_credentials = implicit_credentials.create_scoped(SCOPE)
return Connection(credentials=scoped_credentials)
return get_scoped_connection(Connection, SCOPE)
2 changes: 2 additions & 0 deletions gcloud/storage/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def _callFUT(self, *args, **kw):

def test_it(self):
from gcloud import credentials
from gcloud.storage import SCOPE
from gcloud.storage.connection import Connection
from gcloud.test_credentials import _Client
from gcloud._testing import _Monkey
Expand All @@ -31,6 +32,7 @@ def test_it(self):
found = self._callFUT()
self.assertTrue(isinstance(found, Connection))
self.assertTrue(found._credentials is client._signed)
self.assertEqual(found._credentials._scopes, SCOPE)
self.assertTrue(client._get_app_default_called)


Expand Down
28 changes: 28 additions & 0 deletions gcloud/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,31 @@ def __init__(self, headers, content):
def request(self, **kw):
self._called_with = kw
return self._response, self._content


class Test_get_scoped_connection(unittest2.TestCase):

def _callFUT(self, klass, scopes):
from gcloud.connection import get_scoped_connection
return get_scoped_connection(klass, scopes)

def test_it(self):
from gcloud import credentials
from gcloud.test_credentials import _Client
from gcloud._testing import _Monkey

class _Connection(object):
def __init__(self, credentials):
self._credentials = credentials

SCOPES = ('https://www.googleapis.com/auth/example',
'https://www.googleapis.com/auth/userinfo.email')

client = _Client()
with _Monkey(credentials, client=client):
found = self._callFUT(_Connection, SCOPES)

self.assertTrue(isinstance(found, _Connection))
self.assertTrue(found._credentials is client._signed)
self.assertEqual(found._credentials._scopes, SCOPES)
self.assertTrue(client._get_app_default_called)
10 changes: 5 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ commands =
deps =
nose
unittest2
protobuf==3.0.0-alpha-1
protobuf>=3.0.0-alpha-1

[testenv:cover]
basepython =
Expand All @@ -20,7 +20,7 @@ commands =
deps =
nose
unittest2
protobuf==3.0.0-alpha-1
protobuf>=3.0.0-alpha-1
coverage
nosexcover

Expand Down Expand Up @@ -56,7 +56,7 @@ deps =
pep8
pylint
unittest2
protobuf==3.0.0-alpha-1
protobuf>=3.0.0-alpha-1

[testenv:regression]
basepython =
Expand All @@ -65,7 +65,7 @@ commands =
{toxinidir}/scripts/run_regression.sh
deps =
unittest2
protobuf==3.0.0-alpha-1
protobuf>=3.0.0-alpha-1

[testenv:regression3]
basepython =
Expand All @@ -77,4 +77,4 @@ deps =
# Use a development checkout of oauth2client until a release is made
# which fixes https://github.com/google/oauth2client/issues/125
-egit+https://github.com/google/oauth2client.git#egg=oauth2client
protobuf==3.0.0-alpha-1
protobuf>=3.0.0-alpha-1