Skip to content

Commit

Permalink
Move '_helpers.get_scoped_connection' -> 'connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Mar 20, 2015
1 parent 7c51273 commit b93a661
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 49 deletions.
19 changes: 0 additions & 19 deletions gcloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class Local(object):
except ImportError:
app_identity = None

from gcloud import credentials


class _LocalStack(Local):
"""Manage a thread-local LIFO stack of resources.
Expand Down Expand Up @@ -238,20 +236,3 @@ def __init__(self, project=None, implicit=False):


_DEFAULTS = _DefaultsContainer(implicit=True)


def get_scoped_connection(klass, scopes):
"""Create a scoped connection to GCloud.
:type klass: type
: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 = credentials.get_credentials()
scoped_credentials = implicit_credentials.create_scoped(scopes)
return klass(credentials=scoped_credentials)
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: type
: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)
2 changes: 1 addition & 1 deletion 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._helpers import get_scoped_connection
from gcloud.connection import get_scoped_connection
from gcloud.datastore.connection import Connection


Expand Down
2 changes: 1 addition & 1 deletion gcloud/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
import os

from gcloud import credentials
from gcloud._helpers import get_scoped_connection
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
28 changes: 0 additions & 28 deletions gcloud/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,34 +302,6 @@ def test_descriptor_for_project(self):
self.assertTrue('project' in _helpers._DEFAULTS.__dict__)


class Test_get_scoped_connection(unittest2.TestCase):

def _callFUT(self, klass, scopes):
from gcloud._helpers 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)


class _AppIdentity(object):

def __init__(self, app_id):
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)

0 comments on commit b93a661

Please sign in to comment.