Skip to content
Closed
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
64 changes: 57 additions & 7 deletions google/cloud/bigquery_v2/services/centralized_service/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
DatasetIdentifier = Union[str, dataset_reference.DatasetReference]

# TODO: This variable is here to simplify prototyping, etc.
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
DEFAULT_RETRY: OptionalRetry = gapic_v1.method.DEFAULT
DEFAULT_TIMEOUT: Union[float, object] = gapic_v1.method.DEFAULT
DEFAULT_METADATA: Sequence[Tuple[str, Union[str, bytes]]] = ()
Expand All @@ -68,6 +67,7 @@ def __init__(
*,
credentials: Optional[auth_credentials.Credentials] = None,
client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None,
project: Optional[str] = None
):
"""
Initializes the BigQueryClient.
Expand All @@ -82,9 +82,59 @@ def __init__(
"""

self._clients: Dict[str, object] = {}
self._credentials = credentials
self._client_options = client_options
self.project = PROJECT_ID
self._properties: Dict[str, object] = {
"credentials": credentials,
"client_options": client_options,
"project": project,
}

@property
def credentials(self):
"""google.auth.credentials.Credentials: Credentials to use for queries
performed through the BigQueryClient.

Note:
These credentials do not need to be explicitly defined if you are
using Application Default Credentials. If you are not using
Application Default Credentials, manually construct a
:class:`google.auth.credentials.Credentials` object and set it as
the client credentials as demonstrated in the example below. See
`auth docs`_ for more information on obtaining credentials.

Example:
Manually setting the client credentials:

>>> from google.cloud.bigquery_v2 import BigQueryClient
>>> from google.oauth2 import service_account
>>> bqclient = BigQueryClient()
>>> credentials = (service_account
... .Credentials.from_service_account_file(
... '/path/to/key.json'))
>>> bqclient.credentials = credentials


.. _auth docs: http://google-auth.readthedocs.io
/en/latest/user-guide.html#obtaining-credentials
"""
if self._properties["credentials"] is None:
self._properties["credentials"], _ = google.auth.default()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

google.auth can be a relatively expensive call. Many years ago, it was regularly taking 30+ seconds. It's thankfully usually faster than that, but still is something we should aim to do once if possible.

return self._properties["credentials"]

@credentials.setter
def credentials(self, value):
self._properties["credentials"] = value

@property
def project(self) -> str:
"""Project bound to the table."""
if self._properties["project"] is None:
_, self._properties["project"] = google.auth.default()
return self._properties["project"]

@project.setter
def project(self, value):
self._properties["project"] = value


# --- HELPER METHODS ---
def _parse_dataset_path(self, dataset_path: str) -> Tuple[Optional[str], str]:
Expand Down Expand Up @@ -134,7 +184,7 @@ def _parse_project_id_to_dict(self, project_id: Optional[str] = None) -> dict:
def dataset_service_client(self):
if "dataset" not in self._clients:
self._clients["dataset"] = dataset_service.DatasetServiceClient(
credentials=self._credentials, client_options=self._client_options
credentials=self._properties["credentials"], client_options=self._properties["client_options"]
)
return self._clients["dataset"]

Expand All @@ -150,7 +200,7 @@ def dataset_service_client(self, value):
def job_service_client(self):
if "job" not in self._clients:
self._clients["job"] = job_service.JobServiceClient(
credentials=self._credentials, client_options=self._client_options
credentials=self._properties["credentials"], client_options=self._properties["client_options"]
)
return self._clients["job"]

Expand All @@ -164,7 +214,7 @@ def job_service_client(self, value):
def model_service_client(self):
if "model" not in self._clients:
self._clients["model"] = model_service.ModelServiceClient(
credentials=self._credentials, client_options=self._client_options
credentials=self._properties["credentials"], client_options=self._properties["client_options"]
)
return self._clients["model"]

Expand Down