8000 Merge pull request #749 from tseaver/741-get_scoped_connection · googleapis/google-cloud-python@3b04fdb · GitHub
[go: up one dir, main page]

Skip to content

Commit 3b04fdb

Browse files
committed
Merge pull request #749 from tseaver/741-get_scoped_connection
#741: hoist `get_scoped_connection` into `connection`
2 parents d51f29f + 83d4e89 commit 3b04fdb

File tree

7 files changed

+59
-12
lines changed

7 files changed

+59
-12
lines changed

gcloud/connection.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import httplib2
2222

23+
from gcloud.credentials import get_credentials
2324
from gcloud.exceptions import make_exception
2425

2526

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

299300
return content
301+
302+
303+
def get_scoped_connection(klass, scopes):
304+
"""Create a scoped connection to GCloud.
305+
306+
:type klass: subclass of :class:`gcloud.connection.Connection`
307+
:param klass: the specific ``Connection`` class to instantiate.
308+
309+
:type scopes: list of URLs
310+
:param scopes: the effective service auth scopes for the connection.
311+
312+
:rtype: instance of ``klass``
313+
:returns: A connection defined with the proper credentials.
314+
"""
315+
implicit_credentials = get_credentials()
316+
scoped_credentials = implicit_credentials.create_scoped(scopes)
317+
return klass(credentials=scoped_credentials)

gcloud/datastore/_implicit_environ.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from gcloud._helpers import _app_engine_id
2424
from gcloud._helpers import _compute_engine_id
2525
from gcloud._helpers import _lazy_property_deco
26-
from gcloud import credentials
26+
from gcloud.connection import get_scoped_connection
2727
from gcloud.datastore.connection import Connection
2828

2929

@@ -126,9 +126,7 @@ def get_connection():
126126
:rtype: :class:`gcloud.datastore.connection.Connection`
127127
:returns: A connection defined with the proper credentials.
128128
"""
129-
implicit_credentials = credentials.get_credentials()
130-
scoped_credentials = implicit_credentials.create_scoped(SCOPE)
131-
return Connection(credentials=scoped_credentials)
129+
return get_scoped_connection(Connection, SCOPE)
132130

133131

134132
def set_default_connection(connection=None):

gcloud/datastore/test__implicit_environ.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ def _callFUT(self):
305305

306306
def test_it(self):
307307
from gcloud import credentials
308+
from gcloud.datastore._implicit_environ import SCOPE
308309
from gcloud.datastore.connection import Connection
309310
from gcloud.test_credentials import _Client
310311
from gcloud._testing import _Monkey
@@ -314,6 +315,7 @@ def test_it(self):
314315
found = self._callFUT()
315316
self.assertTrue(isinstance(found, Connection))
316317
self.assertTrue(found._credentials is client._signed)
318+
self.assertEqual(found._credentials._scopes, SCOPE)
317319
self.assertTrue(client._get_app_default_called)
318320

319321

gcloud/storage/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from gcloud import credentials
4444
from gcloud._helpers import get_default_project
4545
from gcloud._helpers import set_default_project
46+
from gcloud.connection import get_scoped_connection
4647
from gcloud.storage import _implicit_environ
4748
from gcloud.storage._implicit_environ import get_default_bucket
4849
from gcloud.storage._implicit_environ import get_default_connection
@@ -133,6 +134,4 @@ def get_connection():
133134
:rtype: :class:`gcloud.storage.connection.Connection`
134135
:returns: A connection defined with the proper credentials.
135136
"""
136-
implicit_credentials = credentials.get_credentials()
137-
scoped_credentials = implicit_credentials.create_scoped(SCOPE)
138-
return Connection(credentials=scoped_credentials)
137+
return get_scoped_connection(Connection, SCOPE)

gcloud/storage/test___init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def _callFUT(self, *args, **kw):
2323

2424
def test_it(self):
2525
from gcloud import credentials
26+
from gcloud.storage import SCOPE
2627
from gcloud.storage.connection import Connection
2728
from gcloud.test_credentials import _Client
2829
from gcloud._testing import _Monkey
@@ -31,6 +32,7 @@ def test_it(self):
3132
found = self._callFUT()
3233
self.assertTrue(isinstance(found, Connection))
3334
self.assertTrue(found._credentials is client._signed)
35+
self.assertEqual(found._credentials._scopes, SCOPE)
3436
self.assertTrue(client._get_app_default_called)
3537

3638

gcloud/test_connection.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,31 @@ def __init__(self, headers, content):
347347
def request(self, **kw):
348348
self._called_with = kw
349349
return self._response, self._content
350+
351+
352+
class Test_get_scoped_connection(unittest2.TestCase):
353+
354+
def _callFUT(self, klass, scopes):
355+
from gcloud.connection import get_scoped_connection
356+
return get_scoped_connection(klass, scopes)
357+
358+
def test_it(self):
359+
from gcloud import credentials
360+
from gcloud.test_credentials import _Client
361+
from gcloud._testing import _Monkey
362+
363+
class _Connection(object):
364+
def __init__(self, credentials):
365+
self._credentials = credentials
366+
367+
SCOPES = ('https://www.googleapis.com/auth/example',
368+
'https://www.googleapis.com/auth/userinfo.email')
369+
370+
client = _Client()
371+
with _Monkey(credentials, client=client):
372+
found = self._callFUT(_Connection, SCOPES)
373+
374+
self.assertTrue(isinstance(found, _Connection))
375+
self.assertTrue(found._credentials is client._signed)
376+
self.assertEqual(found._credentials._scopes, SCOPES)
377+
self.assertTrue(client._get_app_default_called)

tox.ini

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ commands =
1010
deps =
1111
nose
1212
unittest2
13-
protobuf==3.0.0-alpha-1
13+
protobuf>=3.0.0-alpha-1
1414

1515
[testenv:cover]
1616
basepython =
@@ -20,7 +20,7 @@ commands =
2020
deps =
2121
nose
2222
unittest2
23-
protobuf==3.0.0-alpha-1
23+
protobuf>=3.0.0-alpha-1
2424
coverage
2525
nosexcover
2626

@@ -56,7 +56,7 @@ deps =
5656
pep8
5757
pylint
5858
unittest2
59-
protobuf==3.0.0-alpha-1
59+
protobuf>=3.0.0-alpha-1
6060

6161
[testenv:regression]
6262
basepython =
@@ -65,7 +65,7 @@ commands =
6565
{toxinidir}/scripts/run_regression.sh
6666
deps =
6767
unittest2
68-
protobuf==3.0.0-alpha-1
68+
protobuf>=3.0.0-alpha-1
6969

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

0 commit comments

Comments
 (0)
0