From ae2d90c49335d185468893841cd3828afc5fe3ef Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Wed, 14 Dec 2016 23:05:25 -0800 Subject: [PATCH 1/2] Removing _connection_class from base client. --- core/google/cloud/client.py | 45 ++++++++++++++++----------------- core/tox.ini | 2 +- core/unit_tests/test_client.py | 46 ++++++++-------------------------- 3 files changed, 33 insertions(+), 60 deletions(-) diff --git a/core/google/cloud/client.py b/core/google/cloud/client.py index ab413f91a652..338642b263dc 100644 --- a/core/google/cloud/client.py +++ b/core/google/cloud/client.py @@ -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 @@ -72,24 +71,23 @@ 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( @@ -97,8 +95,8 @@ def __init__(self, credentials=None, http=None): 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 class _ClientProjectMixin(object): @@ -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. diff --git a/core/tox.ini b/core/tox.ini index 48e8a517f057..156ffc07e00e 100644 --- a/core/tox.ini +++ b/core/tox.ini @@ -4,7 +4,7 @@ envlist = [testing] deps = - grpcio >= 1.0.2rc0 + grpcio >= 1.0.2 mock pytest covercmd = diff --git a/core/unit_tests/test_client.py b/core/unit_tests/test_client.py index 21d036c06ad0..dd1075aae5f8 100644 --- a/core/unit_tests/test_client.py +++ b/core/unit_tests/test_client.py @@ -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 @@ -67,8 +58,8 @@ 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): @@ -76,9 +67,8 @@ def test_ctor_explicit(self): 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() @@ -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): @@ -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 @@ -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']) @@ -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' @@ -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 From 2c42b7cc8bd7ae7f30df3888213345a5aa568c58 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Wed, 14 Dec 2016 23:43:05 -0800 Subject: [PATCH 2/2] Manually creating Client._connection in subclasses. --- bigquery/google/cloud/bigquery/client.py | 25 +++++++++------ core/google/cloud/client.py | 8 ++--- datastore/google/cloud/datastore/client.py | 25 ++++++++------- datastore/unit_tests/test_client.py | 12 ++++--- dns/google/cloud/dns/client.py | 25 +++++++++------ language/google/cloud/language/client.py | 24 ++++++++------ logging/google/cloud/logging/client.py | 29 ++++++++++------- monitoring/google/cloud/monitoring/client.py | 25 +++++++++------ pubsub/google/cloud/pubsub/client.py | 32 +++++++++++-------- .../google/cloud/resource_manager/client.py | 25 +++++++++------ .../google/cloud/runtimeconfig/client.py | 29 ++++++++++------- speech/google/cloud/speech/client.py | 27 +++++++++------- storage/google/cloud/storage/client.py | 23 ++++++------- translate/google/cloud/translate/client.py | 25 ++++++++------- vision/google/cloud/vision/client.py | 25 +++++++++------ 15 files changed, 209 insertions(+), 150 deletions(-) diff --git a/bigquery/google/cloud/bigquery/client.py b/bigquery/google/cloud/bigquery/client.py index 0c01578f5ffe..e98f390ff616 100644 --- a/bigquery/google/cloud/bigquery/client.py +++ b/bigquery/google/cloud/bigquery/client.py @@ -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. """ - _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. diff --git a/core/google/cloud/client.py b/core/google/cloud/client.py index 338642b263dc..7a14e03f763a 100644 --- a/core/google/cloud/client.py +++ b/core/google/cloud/client.py @@ -82,8 +82,8 @@ class Client(_ClientFactoryMixin): :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 + 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. """ @@ -148,8 +148,8 @@ class JSONClient(Client, _ClientProjectMixin): :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 + 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. diff --git a/datastore/google/cloud/datastore/client.py b/datastore/google/cloud/datastore/client.py index 809c39a902ed..8473a08c6f65 100644 --- a/datastore/google/cloud/datastore/client.py +++ b/datastore/google/cloud/datastore/client.py @@ -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) + _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): diff --git a/datastore/unit_tests/test_client.py b/datastore/unit_tests/test_client.py index f6e016d03712..ef198b98b19a 100644 --- a/datastore/unit_tests/test_client.py +++ b/datastore/unit_tests/test_client.py @@ -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(): diff --git a/dns/google/cloud/dns/client.py b/dns/google/cloud/dns/client.py index 0cd2577bc775..429ebe941c04 100644 --- a/dns/google/cloud/dns/client.py +++ b/dns/google/cloud/dns/client.py @@ -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. diff --git a/language/google/cloud/language/client.py b/language/google/cloud/language/client.py index 14a4e6444a65..bad3ac7be918 100644 --- a/language/google/cloud/language/client.py +++ b/language/google/cloud/language/client.py @@ -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. diff --git a/logging/google/cloud/logging/client.py b/logging/google/cloud/logging/client.py index c92f177eaac6..77e762e6c808 100644 --- a/logging/google/cloud/logging/client.py +++ b/logging/google/cloud/logging/client.py @@ -67,15 +67,16 @@ class Client(JSONClient): 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. @@ -86,12 +87,16 @@ class Client(JSONClient): variable """ - _connection_class = Connection - _logging_api = _sinks_api = _metrics_api = None + _logging_api = None + _sinks_api = None + _metrics_api = None def __init__(self, project=None, credentials=None, http=None, use_gax=None): - super(Client, self).__init__(project, credentials, http) + super(Client, self).__init__( + project=project, credentials=credentials, http=http) + self._connection = Connection( + credentials=self._credentials, http=self._http) if use_gax is None: self._use_gax = _USE_GAX else: diff --git a/monitoring/google/cloud/monitoring/client.py b/monitoring/google/cloud/monitoring/client.py index de686127c737..ccf3d0866d86 100644 --- a/monitoring/google/cloud/monitoring/client.py +++ b/monitoring/google/cloud/monitoring/client.py @@ -54,20 +54,25 @@ class Client(JSONClient): :param project: The target project. 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 query(self, metric_type=Query.DEFAULT_METRIC_TYPE, diff --git a/pubsub/google/cloud/pubsub/client.py b/pubsub/google/cloud/pubsub/client.py index dd323aa25fe5..271d231c2329 100644 --- a/pubsub/google/cloud/pubsub/client.py +++ b/pubsub/google/cloud/pubsub/client.py @@ -51,15 +51,16 @@ class Client(JSONClient): passed when creating a topic. 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. @@ -69,17 +70,22 @@ class Client(JSONClient): falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment variable """ + + _publisher_api = None + _subscriber_api = None + _iam_policy_api = None + def __init__(self, project=None, credentials=None, http=None, use_gax=None): - super(Client, self).__init__(project, credentials, http) + super(Client, self).__init__( + project=project, credentials=credentials, http=http) + self._connection = Connection( + credentials=self._credentials, http=self._http) if use_gax is None: self._use_gax = _USE_GAX else: self._use_gax = use_gax - _connection_class = Connection - _publisher_api = _subscriber_api = _iam_policy_api = None - @property def publisher_api(self): """Helper for publisher-related API calls.""" diff --git a/resource_manager/google/cloud/resource_manager/client.py b/resource_manager/google/cloud/resource_manager/client.py index dca9446acbb6..a9f78d6a0cb4 100644 --- a/resource_manager/google/cloud/resource_manager/client.py +++ b/resource_manager/google/cloud/resource_manager/client.py @@ -33,20 +33,25 @@ class Client(BaseClient): >>> from google.cloud import resource_manager >>> client = resource_manager.Client() - :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, credentials=None, http=None): + super(Client, self).__init__( + credentials=credentials, http=http) + self._connection = Connection( + credentials=self._credentials, http=self._http) def new_project(self, project_id, name=None, labels=None): """Create a project bound to the current client. diff --git a/runtimeconfig/google/cloud/runtimeconfig/client.py b/runtimeconfig/google/cloud/runtimeconfig/client.py index e6fd120f9ca3..d74e5349db3e 100644 --- a/runtimeconfig/google/cloud/runtimeconfig/client.py +++ b/runtimeconfig/google/cloud/runtimeconfig/client.py @@ -28,20 +28,25 @@ class Client(JSONClient): (Optional) The project which the client acts on behalf of. If not passed, falls back to the default inferred from the environment. - :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: - (Optional) An HTTP object to make requests. If not passed, an ``http`` - object is created that is bound to the ``credentials`` for the current - object. + :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 config(self, config_name): """Factory constructor for config object. diff --git a/speech/google/cloud/speech/client.py b/speech/google/cloud/speech/client.py index fde4ce89309f..828e74c119b1 100644 --- a/speech/google/cloud/speech/client.py +++ b/speech/google/cloud/speech/client.py @@ -35,15 +35,16 @@ class Client(BaseClient): """Client to bundle configuration needed for API requests. - :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. @@ -53,16 +54,18 @@ class Client(BaseClient): falls back to the ``GOOGLE_CLOUD_DISABLE_GRPC`` environment variable """ + + _speech_api = None + def __init__(self, credentials=None, http=None, use_gax=None): super(Client, self).__init__(credentials=credentials, http=http) + self._connection = Connection( + credentials=self._credentials, http=self._http) if use_gax is None: self._use_gax = _USE_GAX else: self._use_gax = use_gax - _connection_class = Connection - _speech_api = None - def sample(self, content=None, source_uri=None, encoding=None, sample_rate=None): """Factory: construct Sample to use when making recognize requests. diff --git a/storage/google/cloud/storage/client.py b/storage/google/cloud/storage/client.py index 166f702f309d..d7697a4323eb 100644 --- a/storage/google/cloud/storage/client.py +++ b/storage/google/cloud/storage/client.py @@ -32,25 +32,26 @@ class Client(JSONClient): passed when creating a topic. 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): self._base_connection = None super(Client, self).__init__(project=project, credentials=credentials, http=http) + self._connection = Connection( + credentials=self._credentials, http=self._http) self._batch_stack = _LocalStack() @property diff --git a/translate/google/cloud/translate/client.py b/translate/google/cloud/translate/client.py index b105575944a3..c8c915118cab 100644 --- a/translate/google/cloud/translate/client.py +++ b/translate/google/cloud/translate/client.py @@ -40,23 +40,26 @@ class Client(BaseClient): translations and language names. (Defaults to :data:`ENGLISH_ISO_639`.) - :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: (Optional) HTTP object to make requests. If not - passed, an :class:`httplib.Http` object is created. + :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, target_language=ENGLISH_ISO_639, credentials=None, http=None): self.target_language = target_language super(Client, self).__init__(credentials=credentials, http=http) + self._connection = Connection( + credentials=self._credentials, http=self._http) def get_languages(self, target_language=None): """Get list of supported languages for translation. diff --git a/vision/google/cloud/vision/client.py b/vision/google/cloud/vision/client.py index 4d071bf8ad32..8e83d2e11af9 100644 --- a/vision/google/cloud/vision/client.py +++ b/vision/google/cloud/vision/client.py @@ -67,20 +67,25 @@ class Client(JSONClient): 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 annotate(self, image, features): """Annotate an image to discover it's attributes.