Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Simplifying Client constructor's for Bigtable and Spanner.
  • Loading branch information
dhermes committed Jul 26, 2017
commit a7bb93dca7edf0b34adab16fbcd5c8859f85cdeb
48 changes: 24 additions & 24 deletions bigtable/google/cloud/bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@

import os

import google.auth
import google.auth.credentials
from google.gax.utils import metrics
from google.longrunning import operations_grpc

from google.cloud._helpers import make_insecure_stub
from google.cloud._helpers import make_secure_stub
from google.cloud._http import DEFAULT_USER_AGENT
from google.cloud.client import _ClientFactoryMixin
from google.cloud.client import _ClientProjectMixin
from google.cloud.client import ClientWithProject
from google.cloud.environment_vars import BIGTABLE_EMULATOR

from google.cloud.bigtable import __version__
Expand Down Expand Up @@ -166,13 +164,13 @@ def _make_table_stub(client):
client.emulator_host)


class Client(_ClientFactoryMixin, _ClientProjectMixin):
class Client(ClientWithProject):
"""Client for interacting with Google Cloud Bigtable API.

.. note::

Since the Cloud Bigtable API requires the gRPC transport, no
``http`` argument is accepted by this class.
``_http`` argument is accepted by this class.

:type project: :class:`str` or :func:`unicode <unicode>`
:param project: (Optional) The ID of the project which owns the
Expand Down Expand Up @@ -209,31 +207,17 @@ class Client(_ClientFactoryMixin, _ClientProjectMixin):

def __init__(self, project=None, credentials=None,
read_only=False, admin=False, user_agent=DEFAULT_USER_AGENT):
_ClientProjectMixin.__init__(self, project=project)
if credentials is None:
credentials, _ = google.auth.default()

if read_only and admin:
raise ValueError('A read-only client cannot also perform'
'administrative actions.')

scopes = []
if read_only:
scopes.append(READ_ONLY_SCOPE)
else:
scopes.append(DATA_SCOPE)

# NOTE: This API has no use for the _http argument, but sending it
# will have no impact since the _http() @property only lazily
# creates a working HTTP object.
super(Client, self).__init__(
project=project, credentials=credentials, _http=None)
self._read_only = bool(read_only)

if admin:
scopes.append(ADMIN_SCOPE)

self._admin = bool(admin)

credentials = google.auth.credentials.with_scopes_if_required(
credentials, scopes)

self._credentials = credentials
self.user_agent = user_agent
self.emulator_host = os.getenv(BIGTABLE_EMULATOR)

Expand All @@ -244,6 +228,22 @@ def __init__(self, project=None, credentials=None,
self._operations_stub_internal = _make_operations_stub(self)
self._table_stub_internal = _make_table_stub(self)

self._set_scopes()

def _set_scopes(self):
"""Set the scopes on the current credentials."""
scopes = []
if self._read_only:
scopes.append(READ_ONLY_SCOPE)
else:
scopes.append(DATA_SCOPE)

if self._admin:
scopes.append(ADMIN_SCOPE)

self._credentials = google.auth.credentials.with_scopes_if_required(
self._credentials, scopes)

def copy(self):
"""Make a copy of this client.

Expand Down
28 changes: 11 additions & 17 deletions spanner/google/cloud/spanner/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
# pylint: enable=line-too-long

from google.cloud._http import DEFAULT_USER_AGENT
from google.cloud.client import _ClientFactoryMixin
from google.cloud.client import _ClientProjectMixin
from google.cloud.client import ClientWithProject
from google.cloud.iterator import GAXIterator
from google.cloud.spanner import __version__
from google.cloud.spanner._helpers import _options_with_prefix
Expand Down Expand Up @@ -73,13 +72,13 @@ def from_pb(cls, config_pb):
return cls(config_pb.name, config_pb.display_name)


class Client(_ClientFactoryMixin, _ClientProjectMixin):
class Client(ClientWithProject):
"""Client for interacting with Cloud Spanner API.

.. note::

Since the Cloud Spanner API requires the gRPC transport, no
``http`` argument is accepted by this class.
``_http`` argument is accepted by this class.

:type project: :class:`str` or :func:`unicode <unicode>`
:param project: (Optional) The ID of the project which owns the
Expand All @@ -104,21 +103,16 @@ class Client(_ClientFactoryMixin, _ClientProjectMixin):
_database_admin_api = None
_SET_PROJECT = True # Used by from_service_account_json()

SCOPE = (SPANNER_ADMIN_SCOPE,)

This comment was marked as spam.

"""The scopes required for Google Cloud Spanner."""

def __init__(self, project=None, credentials=None,
user_agent=DEFAULT_USER_AGENT):

_ClientProjectMixin.__init__(self, project=project)
if credentials is None:
credentials, _ = google.auth.default()

scopes = [
SPANNER_ADMIN_SCOPE,
]

credentials = google.auth.credentials.with_scopes_if_required(
credentials, scopes)

self._credentials = credentials
# NOTE: This API has no use for the _http argument, but sending it
# will have no impact since the _http() @property only lazily
# creates a working HTTP object.
super(Client, self).__init__(
project=project, credentials=credentials, _http=None)
self.user_agent = user_agent

@property
Expand Down