8000 Simplifying Client constructor's for Bigtable and Spanner. (#3672) · landrito/google-cloud-python@1c68bda · GitHub < 8000 link rel="alternate icon" class="js-site-favicon" type="image/png" href="https://github.githubassets.com/favicons/favicon.png">
[go: up one dir, main page]

Skip to content

Commit 1c68bda

Browse files
dhermeslandrito
authored andcommitted
Simplifying Client constructor's for Bigtable and Spanner. (googleapis#3672)
* Simplifying Client constructor's for Bigtable and Spanner. * Fixing Bigtable unit tests after Client re-factor. Also slightly changing the Client constructor so that it only called `with_scopes()` one time on the credentials (was previously calling with `SCOPE=None` and then again with the custom scope for the instance) * Fixing Spanner unit tests after Client re-factor. Also slightly changing the `copy()` method so that it just passes the same credentials instance. Also updating `nox` config to allow session `posargs`. * Removing unused imports after Bigtable/Spanner Client re-factor.
1 parent f0c2fae commit 1c68bda

File tree

6 files changed

+297
-267
lines changed

6 files changed

+297
-267
lines changed

bigtable/google/cloud/bigtable/client.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,13 @@
3131

3232
import os
3333

34-
import google.auth
35-
import google.auth.credentials
3634
from google.gax.utils import metrics
3735
from google.longrunning import operations_grpc
3836

3937
from google.cloud._helpers import make_insecure_stub
4038
from google.cloud._helpers import make_secure_stub
4139
from google.cloud._http import DEFAULT_USER_AGENT
42-
from google.cloud.client import _ClientFactoryMixin
43-
from google.cloud.client import _ClientProjectMixin
40+
from google.cloud.client import ClientWithProject
4441
from google.cloud.environment_vars import BIGTABLE_EMULATOR
4542

4643
from google.cloud.bigtable import __version__
@@ -166,13 +163,13 @@ def _make_table_stub(client):
166163
client.emulator_host)
167164

168165

169-
class Client(_ClientFactoryMixin, _ClientProjectMixin):
166+
class Client(ClientWithProject):
170167
"""Client for interacting with Google Cloud Bigtable API.
171168
172169
.. note::
173170
174171
Since the Cloud Bigtable API requires the gRPC transport, no
175-
``http`` argument is accepted by this class.
172+
``_http`` argument is accepted by this class.
176173
177174
:type project: :class:`str` or :func:`unicode <unicode>`
178175
:param project: (Optional) The ID of the project which owns the
@@ -209,31 +206,21 @@ class Client(_ClientFactoryMixin, _ClientProjectMixin):
209206

210207
def __init__(self, project=None, credentials=None,
211208
read_only=False, admin=False, user_agent=DEFAULT_USER_AGENT):
212-
_ClientProjectMixin.__init__(self, project=project)
213-
if credentials is None:
214-
credentials, _ = google.auth.default()
215-
216209
if read_only and admin:
217210
raise ValueError('A read-only client cannot also perform'
218211
'administrative actions.')
219212

220-
scopes = []
221-
if read_only:
222-
scopes.append(READ_ONLY_SCOPE)
223-
else:
224-
scopes.append(DATA_SCOPE)
225-
213+
# NOTE: We set the scopes **before** calling the parent constructor.
214+
# It **may** use those scopes in ``with_scopes_if_required``.
226215
self._read_only = bool(read_only)
227-
228-
if admin:
229-
scopes.append(ADMIN_SCOPE)
230-
231216
self._admin = bool(admin)
217+
self.SCOPE = self._get_scopes()
232218

233-
credentials = google.auth.credentials.with_scopes_if_required(
234-
credentials, scopes)
235-
236-
self._credentials = credentials
219+
# NOTE: This API has no use for the _http argument, but sending it
220+
# will have no impact since the _http() @property only lazily
221+
# creates a working HTTP object.
222+
super(Client, self).__init__(
223+
project=project, credentials=credentials, _http=None)
237224
self.user_agent = user_agent
238225
self.emulator_host = os.getenv(BIGTABLE_EMULATOR)
239226

@@ -244,6 +231,22 @@ def __init__(self, project=None, credentials=None,
244231
self._operations_stub_internal = _make_operations_stub(self)
245232
self._table_stub_internal = _make_table_stub(self)
246233

234+
def _get_scopes(self):
235+
"""Get the scopes corresponding to admin / read-only state.
236+
237+
Returns:
238+
Tuple[str, ...]: The tuple of scopes.
239+
"""
240+
if self._read_only:
241+
scopes = (READ_ONLY_SCOPE,)
242+
else:
243+
scopes = (DATA_SCOPE,)
244+
245+
if self._admin:
246+
scopes += (ADMIN_SCOPE,)
247+
248+
return scopes
249+
247250
def copy(self):
248251
"""Make a copy of this client.
249252

bigtable/nox.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,17 @@ def unit_tests(session, python_version):
3737
session.install('-e', '.')
3838

3939
# Run py.test against the unit tests.
40-
session.run('py.test', '--quiet',
41-
'--cov=google.cloud.bigtable', '--cov=tests.unit', '--cov-append',
42-
'--cov-config=.coveragerc', '--cov-report=', '--cov-fail-under=97',
40+
session.run(
41+
'py.test',
42+
'--quiet',
43+
'--cov=google.cloud.bigtable',
44+
'--cov=tests.unit',
45+
'--cov-append',
46+
'--cov-config=.coveragerc',
47+
'--cov-report=',
48+
'--cov-fail-under=97',
4349
'tests/unit',
50+
*session.posargs
4451
)
4552

4653

0 commit comments

Comments
 (0)
0