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

Remove connection from base Client class #2870

Merged
merged 2 commits into from
Dec 15, 2016
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
25 changes: 15 additions & 10 deletions bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,25 @@ class Client(JSONClient):
passed when creating a dataset / job. If not passed,
falls back to the default inferred from the environment.

:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for the connection
owned by this client. If not passed (and if no ``http``
object is passed), falls back to the default inferred
from the environment.

:type http: :class:`httplib2.Http` or class that defines ``request()``.
:param http: An optional HTTP object to make requests. If not passed, an
:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
passed), falls back to the default inferred from the
environment.

:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
"""

This comment was marked as spam.

This comment was marked as spam.


_connection_class = Connection
def __init__(self, project=None, credentials=None, http=None):
super(Client, self).__init__(
project=project, credentials=credentials, http=http)
self._connection = Connection(
credentials=self._credentials, http=self._http)

def list_projects(self, max_results=None, page_token=None):
"""List projects for the project associated with this client.
Expand Down
45 changes: 22 additions & 23 deletions core/google/cloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import six

from google.cloud._helpers import _determine_default_project
from google.cloud._http import Connection
from google.cloud.credentials import get_credentials


Expand Down Expand Up @@ -72,33 +71,32 @@ def from_service_account_json(cls, json_credentials_path, *args, **kwargs):
class Client(_ClientFactoryMixin):
"""Client to bundle configuration needed for API requests.

Assumes that the associated ``_connection_class`` only accepts
``http`` and ``credentials`` in its constructor.
Stores ``credentials`` and ``http`` object so that subclasses
can pass them along to a connection class.

:type credentials: :class:`google.auth.credentials.Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for the connection
owned by this client. If not passed (and if no ``http``
object is passed), falls back to the default inferred
from the environment.
:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
passed), falls back to the default inferred from the
environment.

:type http: :class:`httplib2.Http` or class that defines ``request()``.
:param http: An optional HTTP object to make requests. If not passed, an
:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
"""

_connection_class = Connection

def __init__(self, credentials=None, http=None):
if (credentials is not None and
not isinstance(
credentials, google.auth.credentials.Credentials)):
raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
if credentials is None and http is None:
credentials = get_credentials()
self._connection = self._connection_class(
credentials=credentials, http=http)
self._credentials = credentials
self._http = http

This comment was marked as spam.

This comment was marked as spam.



class _ClientProjectMixin(object):
Expand Down Expand Up @@ -142,15 +140,16 @@ class JSONClient(Client, _ClientProjectMixin):
passed falls back to the default inferred from the
environment.

:type credentials: :class:`google.auth.credentials.Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for the connection
owned by this client. If not passed (and if no ``http``
object is passed), falls back to the default inferred
from the environment.
:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
passed), falls back to the default inferred from the
environment.

:type http: :class:`httplib2.Http` or class that defines ``request()``.
:param http: An optional HTTP object to make requests. If not passed, an
:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.

Expand Down
2 changes: 1 addition & 1 deletion core/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ envlist =

[testing]
deps =
grpcio >= 1.0.2rc0
grpcio >= 1.0.2
mock
pytest
covercmd =
Expand Down
46 changes: 10 additions & 36 deletions core/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ def test_virtual(self):

class TestClient(unittest.TestCase):

def setUp(self):
KLASS = self._get_target_class()
self.original_cnxn_class = KLASS._connection_class
KLASS._connection_class = _MockConnection

def tearDown(self):
KLASS = self._get_target_class()
KLASS._connection_class = self.original_cnxn_class

@staticmethod
def _get_target_class():
from google.cloud.client import Client
Expand All @@ -67,18 +58,17 @@ def mock_get_credentials():
with _Monkey(client, get_credentials=mock_get_credentials):
client_obj = self._make_one()

self.assertIsInstance(client_obj._connection, _MockConnection)
self.assertIs(client_obj._connection.credentials, CREDENTIALS)
self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIsNone(client_obj._http)
self.assertEqual(FUNC_CALLS, ['get_credentials'])

def test_ctor_explicit(self):
CREDENTIALS = _make_credentials()
HTTP = object()
client_obj = self._make_one(credentials=CREDENTIALS, http=HTTP)

self.assertIsInstance(client_obj._connection, _MockConnection)
self.assertIs(client_obj._connection.credentials, CREDENTIALS)
self.assertIs(client_obj._connection.http, HTTP)
self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIs(client_obj._http, HTTP)

def test_ctor_bad_credentials(self):
CREDENTIALS = object()
Expand All @@ -99,7 +89,8 @@ def test_from_service_account_json(self):
mock.sentinel.filename)

self.assertIs(
client_obj._connection.credentials, constructor.return_value)
client_obj._credentials, constructor.return_value)
self.assertIsNone(client_obj._http)
constructor.assert_called_once_with(mock.sentinel.filename)

def test_from_service_account_json_bad_args(self):
Expand All @@ -112,15 +103,6 @@ def test_from_service_account_json_bad_args(self):

class TestJSONClient(unittest.TestCase):

def setUp(self):
KLASS = self._get_target_class()
self.original_cnxn_class = KLASS._connection_class
KLASS._connection_class = _MockConnection

def tearDown(self):
KLASS = self._get_target_class()
KLASS._connection_class = self.original_cnxn_class

@staticmethod
def _get_target_class():
from google.cloud.client import JSONClient
Expand Down Expand Up @@ -150,8 +132,8 @@ def mock_get_credentials():
client_obj = self._make_one()

self.assertEqual(client_obj.project, PROJECT)
self.assertIsInstance(client_obj._connection, _MockConnection)
self.assertIs(client_obj._connection.credentials, CREDENTIALS)
self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIsNone(client_obj._http)
self.assertEqual(
FUNC_CALLS,
[(None, '_determine_default_project'), 'get_credentials'])
Expand Down Expand Up @@ -191,9 +173,8 @@ def _explicit_ctor_helper(self, project):
self.assertEqual(client_obj.project, project.decode('utf-8'))
else:
self.assertEqual(client_obj.project, project)
self.assertIsInstance(client_obj._connection, _MockConnection)
self.assertIs(client_obj._connection.credentials, CREDENTIALS)
self.assertIs(client_obj._connection.http, HTTP)
self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIs(client_obj._http, HTTP)

def test_ctor_explicit_bytes(self):
PROJECT = b'PROJECT'
Expand All @@ -202,10 +183,3 @@ def test_ctor_explicit_bytes(self):
def test_ctor_explicit_unicode(self):
PROJECT = u'PROJECT'
self._explicit_ctor_helper(PROJECT)


class _MockConnection(object):

def __init__(self, credentials=None, http=None):
self.credentials = credentials
self.http = http
25 changes: 14 additions & 11 deletions datastore/google/cloud/datastore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,26 +157,29 @@ class Client(_BaseClient, _ClientProjectMixin):
:type namespace: str
:param namespace: (optional) namespace to pass to proxied API methods.

:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for the connection
owned by this client. If not passed (and if no ``http``
object is passed), falls back to the default inferred
from the environment.

:type http: :class:`httplib2.Http` or class that defines ``request()``.
:param http: An optional HTTP object to make requests. If not passed, an
:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
passed), falls back to the default inferred from the
environment.

:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
"""
_connection_class = Connection

def __init__(self, project=None, namespace=None,
credentials=None, http=None):
_ClientProjectMixin.__init__(self, project=project)

This comment was marked as spam.

_BaseClient.__init__(self, credentials=credentials, http=http)
self._connection = Connection(
credentials=self._credentials, http=self._http)

self.namespace = namespace
self._batch_stack = _LocalStack()
super(Client, self).__init__(credentials, http)

@staticmethod
def _determine_default(project):
Expand Down
12 changes: 7 additions & 5 deletions datastore/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,15 @@ class TestClient(unittest.TestCase):
PROJECT = 'PROJECT'

def setUp(self):
KLASS = self._get_target_class()
self.original_cnxn_class = KLASS._connection_class
KLASS._connection_class = _MockConnection
from google.cloud.datastore import client as MUT

self.original_cnxn_class = MUT.Connection
MUT.Connection = _MockConnection

def tearDown(self):
KLASS = self._get_target_class()
KLASS._connection_class = self.original_cnxn_class
from google.cloud.datastore import client as MUT

MUT.Connection = self.original_cnxn_class

@staticmethod
def _get_target_class():
Expand Down
25 changes: 15 additions & 10 deletions dns/google/cloud/dns/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@ class Client(JSONClient):
passed when creating a zone. If not passed,
falls back to the default inferred from the environment.

:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for the connection
owned by this client. If not passed (and if no ``http``
object is passed), falls back to the default inferred
from the environment.

:type http: :class:`httplib2.Http` or class that defines ``request()``.
:param http: An optional HTTP object to make requests. If not passed, an
:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
passed), falls back to the default inferred from the
environment.

:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
"""

_connection_class = Connection
def __init__(self, project=None, credentials=None, http=None):
super(Client, self).__init__(
project=project, credentials=credentials, http=http)
self._connection = Connection(
credentials=self._credentials, http=self._http)

def quotas(self):
"""Return DNS quotas for the project associated with this client.
Expand Down
24 changes: 15 additions & 9 deletions language/google/cloud/language/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@
class Client(client_module.Client):
"""Client to bundle configuration needed for API requests.

:type credentials: :class:`~oauth2client.client.OAuth2Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for the
connection owned by this client. If not passed (and
if no ``http`` object is passed), falls back to the
default inferred from the environment.

:type http: :class:`httplib2.Http` or class that defines ``request()``.
:param http: An optional HTTP object to make requests. If not passed, an
:type credentials: :class:`~google.auth.credentials.Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for this
client. If not passed (and if no ``http`` object is
passed), falls back to the default inferred from the
environment.

:type http: :class:`~httplib2.Http`
:param http: (Optional) HTTP object to make requests. Can be any object
that defines ``request()`` with the same interface as
:meth:`~httplib2.Http.request`. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.
"""

_connection_class = Connection
def __init__(self, credentials=None, http=None):
super(Client, self).__init__(
credentials=credentials, http=http)
self._connection = Connection(
credentials=self._credentials, http=self._http)

def document_from_text(self, content, **kwargs):
"""Create a plain text document bound to this client.
Expand Down
Loading