Skip to content

Commit

Permalink
Removing factories from base Connection class.
Browse files Browse the repository at this point in the history
These are no longer needed since the factories are on
all clients.
  • Loading branch information
dhermes committed Aug 31, 2015
1 parent 51640a5 commit 8e86218
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 164 deletions.
89 changes: 0 additions & 89 deletions gcloud/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

import httplib2

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


Expand Down Expand Up @@ -122,92 +119,6 @@ def _create_scoped_credentials(credentials, scope):
credentials = credentials.create_scoped(scope)
return credentials

@classmethod
def from_service_account_json(cls, json_credentials_path, *args, **kwargs):
"""Factory to retrieve JSON credentials while creating connection.
:type json_credentials_path: string
:param json_credentials_path: The path to a private key file (this file
was given to you when you created the
service account). This file must contain
a JSON object with a private key and
other credentials information (downloaded
from the Google APIs console).
:type args: tuple
:param args: Remaining positional arguments to pass to constructor.
:type kwargs: dictionary
:param kwargs: Remaining keyword arguments to pass to constructor.
:rtype: :class:`gcloud.connection.Connection`
:returns: The connection created with the retrieved JSON credentials.
:raises: class:`TypeError` if there is a conflict with the kwargs
and the credentials created by the factory.
"""
if 'credentials' in kwargs:
raise TypeError('credentials must not be in keyword arguments')
credentials = get_for_service_account_json(json_credentials_path)
kwargs['credentials'] = credentials
return cls(*args, **kwargs)

@classmethod
def from_service_account_p12(cls, client_email, private_key_path,
*args, **kwargs):
"""Factory to retrieve P12 credentials while creating connection.
.. note::
Unless you have an explicit reason to use a PKCS12 key for your
service account, we recommend using a JSON key.
:type client_email: string
:param client_email: The e-mail attached to the service account.
:type private_key_path: string
:param private_key_path: The path to a private key file (this file was
given to you when you created the service
account). This file must be in P12 format.
:type args: tuple
:param args: Remaining positional arguments to pass to constructor.
:type kwargs: dictionary
:param kwargs: Remaining keyword arguments to pass to constructor.
:rtype: :class:`gcloud.connection.Connection`
:returns: The connection created with the retrieved P12 credentials.
:raises: class:`TypeError` if there is a conflict with the kwargs
and the credentials created by the factory.
"""
if 'credentials' in kwargs:
raise TypeError('credentials must not be in keyword arguments')
credentials = get_for_service_account_p12(client_email,
private_key_path)
kwargs['credentials'] = credentials
return cls(*args, **kwargs)

@classmethod
def from_environment(cls, *args, **kwargs):
"""Factory to retrieve implicit credentials while creating connection.
:type args: tuple
:param args: Remaining positional arguments to pass to constructor.
:type kwargs: dictionary
:param kwargs: Remaining keyword arguments to pass to constructor.
:rtype: :class:`gcloud.connection.Connection`
:returns: The connection created with the retrieved implicit
credentials.
:raises: class:`TypeError` if there is a conflict with the kwargs
and the credentials created by the factory.
"""
if 'credentials' in kwargs:
raise TypeError('credentials must not be in keyword arguments')
credentials = get_credentials()
kwargs['credentials'] = credentials
return cls(*args, **kwargs)


class JSONConnection(Connection):
"""A connection to a Google JSON-based API.
Expand Down
75 changes: 0 additions & 75 deletions gcloud/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,81 +66,6 @@ def test_user_agent_format(self):
conn = self._makeOne()
self.assertEqual(conn.USER_AGENT, expected_ua)

def _from_service_account_json_helper(self, **kwargs):
from gcloud._testing import _Monkey
from gcloud import connection

KLASS = self._getTargetClass()
CREDENTIALS = _Credentials()
_CALLED = []

def mock_creds(arg1):
_CALLED.append((arg1,))
return CREDENTIALS

FOO = object()
with _Monkey(connection, get_for_service_account_json=mock_creds):
conn = KLASS.from_service_account_json(FOO, **kwargs)

self.assertTrue(conn.credentials is CREDENTIALS)
self.assertEqual(_CALLED, [(FOO,)])

def test_from_service_account_json(self):
self._from_service_account_json_helper()

def test_from_service_account_json_fail(self):
with self.assertRaises(TypeError):
self._from_service_account_json_helper(credentials=None)

def _from_service_account_p12_helper(self, **kwargs):
from gcloud._testing import _Monkey
from gcloud import connection

KLASS = self._getTargetClass()
CREDENTIALS = _Credentials()
_CALLED = []

def mock_creds(arg1, arg2):
_CALLED.append((arg1, arg2))
return CREDENTIALS

FOO = object()
BAR = object()
with _Monkey(connection, get_for_service_account_p12=mock_creds):
conn = KLASS.from_service_account_p12(FOO, BAR, **kwargs)

self.assertTrue(conn.credentials is CREDENTIALS)
self.assertEqual(_CALLED, [(FOO, BAR)])

def test_from_service_account_p12(self):
self._from_service_account_p12_helper()

def test_from_service_account_p12_fail(self):
with self.assertRaises(TypeError):
self._from_service_account_p12_helper(credentials=None)

def _from_environment_helper(self, **kwargs):
from gcloud._testing import _Monkey
from gcloud import connection

KLASS = self._getTargetClass()
CREDENTIALS = _Credentials()

def mock_creds():
return CREDENTIALS

with _Monkey(connection, get_credentials=mock_creds):
conn = KLASS.from_environment(**kwargs)

self.assertTrue(conn.credentials is CREDENTIALS)

def test_from_environment(self):
self._from_environment_helper()

def test_from_environment_fail(self):
with self.assertRaises(TypeError):
self._from_environment_helper(credentials=None)


class TestJSONConnection(unittest2.TestCase):

Expand Down

0 comments on commit 8e86218

Please sign in to comment.